Skip to content

Commit

Permalink
Merge pull request #167 from zzz40500/master
Browse files Browse the repository at this point in the history
处理项目目录结构外的jar
  • Loading branch information
erhu authored Jul 11, 2017
2 parents 2d2a7b2 + 1fa6108 commit 325bb4e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ReClassTransform extends Transform {

/* 需要处理的 jar 包 */
def includeJars = [] as Set
def map = [:]

public ReClassTransform(Project p) {
this.project = p
Expand Down Expand Up @@ -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)
}
Expand All @@ -222,14 +223,18 @@ 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)
if (destName.endsWith('.jar')) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 325bb4e

Please sign in to comment.