mirror of
https://gitee.com/replugin/RePlugin.git
synced 2024-12-02 11:48:02 +08:00
处理项目目录结构外的jar
This commit is contained in:
parent
b87cbc7bf8
commit
1fa61089b4
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user