处理项目目录结构外的jar

This commit is contained in:
dim 2017-07-10 00:02:07 +08:00
parent b87cbc7bf8
commit 1fa61089b4
2 changed files with 25 additions and 8 deletions

View File

@ -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

View File

@ -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<TransformInput> inputs, Set<String> includeJars) {
static getClassPaths(Project project, GlobalScope globalScope, Collection<TransformInput> inputs, Set<String> includeJars, Map<String, String> 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<TransformInput> inputs,
Set<String> includeJars) {
Set<String> includeJars, Map<String, String> 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