From 1fa61089b4d5fe01d8b7b08ee4f0401263640929 Mon Sep 17 00:00:00 2001 From: dim Date: Mon, 10 Jul 2017 00:02:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E9=A1=B9=E7=9B=AE=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=BB=93=E6=9E=84=E5=A4=96=E7=9A=84jar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/inner/ReClassTransform.groovy | 9 +++++-- .../replugin/gradle/plugin/inner/Util.groovy | 24 ++++++++++++++----- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/replugin-plugin-gradle/src/main/groovy/com/qihoo360/replugin/gradle/plugin/inner/ReClassTransform.groovy b/replugin-plugin-gradle/src/main/groovy/com/qihoo360/replugin/gradle/plugin/inner/ReClassTransform.groovy index efbda21..274e30c 100644 --- a/replugin-plugin-gradle/src/main/groovy/com/qihoo360/replugin/gradle/plugin/inner/ReClassTransform.groovy +++ b/replugin-plugin-gradle/src/main/groovy/com/qihoo360/replugin/gradle/plugin/inner/ReClassTransform.groovy @@ -37,6 +37,7 @@ public class ReClassTransform extends Transform { /* 需要处理的 jar 包 */ def includeJars = [] as Set + def map = [:] public ReClassTransform(Project p) { this.project = p @@ -198,7 +199,7 @@ public class ReClassTransform extends Transform { Util.newSection() def pool = new ClassPool(true) // 添加编译时需要引用的到类到 ClassPool, 同时记录要修改的 jar 到 includeJars - Util.getClassPaths(project, globalScope, inputs, includeJars).each { + Util.getClassPaths(project, globalScope, inputs, includeJars, map).each { println " $it" pool.insertClassPath(it) } @@ -222,6 +223,10 @@ public class ReClassTransform extends Transform { */ def copyJar(TransformOutputProvider output, JarInput input) { File jar = input.file + String jarPath = map.get(jar.absolutePath); + if (jarPath != null) { + jar = new File(jarPath) + } String destName = input.name def hexName = DigestUtils.md5Hex(jar.absolutePath) @@ -229,7 +234,7 @@ public class ReClassTransform extends Transform { destName = destName.substring(0, destName.length() - 4) } File dest = output.getContentLocation(destName + '_' + hexName, input.contentTypes, input.scopes, Format.JAR) - FileUtils.copyFile(input.file, dest) + FileUtils.copyFile(jar, dest) /* def path = jar.absolutePath diff --git a/replugin-plugin-gradle/src/main/groovy/com/qihoo360/replugin/gradle/plugin/inner/Util.groovy b/replugin-plugin-gradle/src/main/groovy/com/qihoo360/replugin/gradle/plugin/inner/Util.groovy index 9bbdfb8..7fcf798 100644 --- a/replugin-plugin-gradle/src/main/groovy/com/qihoo360/replugin/gradle/plugin/inner/Util.groovy +++ b/replugin-plugin-gradle/src/main/groovy/com/qihoo360/replugin/gradle/plugin/inner/Util.groovy @@ -23,10 +23,13 @@ import com.android.build.api.transform.TransformInput import com.android.build.gradle.internal.scope.GlobalScope import com.android.sdklib.IAndroidTarget import org.apache.commons.io.FileUtils +import com.google.common.base.Charsets +import com.google.common.hash.Hashing import org.gradle.api.Project import java.nio.file.Files import java.nio.file.Paths +import static com.android.builder.model.AndroidProject.FD_INTERMEDIATES; /** * @author RePlugin Team @@ -35,14 +38,14 @@ public class Util { /** 生成 ClassPool 使用的 ClassPath 集合,同时将要处理的 jar 写入 includeJars */ def - static getClassPaths(Project project, GlobalScope globalScope, Collection inputs, Set includeJars) { + static getClassPaths(Project project, GlobalScope globalScope, Collection inputs, Set includeJars, Map map) { def classpathList = [] // android.jar classpathList.add(getAndroidJarPath(globalScope)) // 原始项目中引用的 classpathList - getProjectClassPath(project, inputs, includeJars).each { + getProjectClassPath(project, inputs, includeJars, map).each { classpathList.add(it) } @@ -54,7 +57,7 @@ public class Util { /** 获取原始项目中的 ClassPath */ def private static getProjectClassPath(Project project, Collection inputs, - Set includeJars) { + Set includeJars, Map map) { def classPath = [] def visitor = new ClassFileVisitor() def projectDir = project.getRootDir().absolutePath @@ -75,10 +78,19 @@ public class Util { File jar = jarInput.file def jarPath = jar.absolutePath - // 不处理 Project 之外的文件 if (!jarPath.contains(projectDir)) { - classPath << jarPath - println ">>> Skip ${jarPath}" + + String jarZipDir = project.getBuildDir().path + + File.separator + FD_INTERMEDIATES + File.separator + "exploded-aar" + + File.separator + Hashing.sha1().hashString(jarPath, Charsets.UTF_16LE).toString() + File.separator + "class"; + unzip(jarPath, jarZipDir) + def jarZip = jarZipDir + ".jar" + includeJars << jarZip + classPath << jarZipDir + visitor.setBaseDir(jarZipDir) + Files.walkFileTree(Paths.get(jarZipDir), visitor) + map.put(jarPath, jarZip) + } else { includeJars << jarPath