Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import com.qihoo360.replugin.gradle.plugin.AppConstant
/**
* @author 247321453
*/
public class InjectorVersion {
public class JarPatchInfo {

def String jarMd5

def String pluginVersion

InjectorVersion(){
JarPatchInfo(){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无用的import和方法,变量声明等,请移除


}

InjectorVersion(String jarMd5, String pluginVersion) {
JarPatchInfo(String jarMd5, String pluginVersion) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无用的import和方法,变量声明等,请移除

this.jarMd5 = jarMd5
this.pluginVersion = pluginVersion
}

InjectorVersion(File jar){
JarPatchInfo(File jar){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无用的import和方法,变量声明等,请移除

this.jarMd5 = Util.md5File(jar)
this.pluginVersion = AppConstant.VER
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class ReClassTransform extends Transform {
// Compatible with path separators for window and Linux, and fit split param based on 'Pattern.quote'
def variantDir = rootLocation.absolutePath.split(getName() + Pattern.quote(File.separator))[1]
println ">>> variantDir: ${variantDir}"
String buildType = variantDir;

CommonData.appModule = config.appModule
CommonData.ignoredActivities = config.ignoredActivities
Expand All @@ -91,7 +92,7 @@ public class ReClassTransform extends Transform {
if (injectors.isEmpty()) {
copyResult(inputs, outputProvider) // 跳过 reclass
} else {
doTransform(inputs, outputProvider, config, injectors) // 执行 reclass
doTransform(inputs, buildType, outputProvider, config, injectors) // 执行 reclass
}
}

Expand All @@ -116,12 +117,13 @@ public class ReClassTransform extends Transform {
* 执行 Transform
*/
def doTransform(Collection<TransformInput> inputs,
buildType,
TransformOutputProvider outputProvider,
Object config,
def injectors) {

/* 初始化 ClassPool */
Object pool = initClassPool(inputs)
Object pool = initClassPool(inputs, buildType)

/* 进行注入操作 */
Util.newSection()
Expand Down Expand Up @@ -207,11 +209,11 @@ public class ReClassTransform extends Transform {
/**
* 初始化 ClassPool
*/
def initClassPool(Collection<TransformInput> inputs) {
def initClassPool(Collection<TransformInput> inputs,String buildType) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此行代码格式化一下

Util.newSection()
def pool = new ClassPool(true)
// 添加编译时需要引用的到类到 ClassPool, 同时记录要修改的 jar 到 includeJars
Util.getClassPaths(project, globalScope, inputs, includeJars, map).each {
Util.getClassPaths(project, buildType, globalScope, inputs, includeJars, map).each {
println " $it"
pool.insertClassPath(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ public class Util {

/** 生成 ClassPool 使用的 ClassPath 集合,同时将要处理的 jar 写入 includeJars */
def
static getClassPaths(Project project, GlobalScope globalScope, Collection<TransformInput> inputs, Set<String> includeJars, Map<String, String> map) {
static getClassPaths(Project project, String buildType,GlobalScope globalScope, Collection<TransformInput> inputs, Set<String> includeJars, Map<String, String> map) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码格式化

def classpathList = []

includeJars.clear()

// android.jar
classpathList.add(getAndroidJarPath(globalScope))

// 原始项目中引用的 classpathList
getProjectClassPath(project, inputs, includeJars, map).each {
getProjectClassPath(project, buildType, inputs, includeJars, map).each {
classpathList.add(it)
}

Expand All @@ -64,15 +66,17 @@ public class Util {
}

/** 获取原始项目中的 ClassPath */
def private static getProjectClassPath(Project project,
def private static getProjectClassPath(Project project,String buildType,
Collection<TransformInput> inputs,
Set<String> includeJars, Map<String, String> map) {
def classPath = []
def visitor = new ClassFileVisitor()
def projectDir = project.getRootDir().absolutePath

println ">>> Unzip Jar ..."
Map<String, InjectorVersion> injectorMap = readJarInjectorHistory(project)
Map<String, JarPatchInfo> infoMap = readJarInjectorHistory(project, buildType)
final String injectDir = project.getBuildDir().path +
File.separator + FD_INTERMEDIATES + File.separator + "replugin-jar"+ File.separator + buildType;
boolean needSave = false
inputs.each { TransformInput input ->

Expand All @@ -87,9 +91,9 @@ public class Util {
input.jarInputs.each { JarInput jarInput ->
File jar = jarInput.file
def jarPath = jar.absolutePath

if (!jarPath.contains(projectDir)) {

if (jarPath.contains(File.separator + FD_INTERMEDIATES + File.separator + "replugin-jar")) {
//
}else if (!jarPath.contains(projectDir)) {
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";
Expand All @@ -101,70 +105,62 @@ public class Util {
Files.walkFileTree(Paths.get(jarZipDir), visitor)
map.put(jarPath, jarZip)
}

} else {
//重定向jar

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里严格说,不是重定向文件,只是将文件解压到临时目录。请移除该注释。

if (jarPath.contains(File.separator + FD_INTERMEDIATES + File.separator + "replugin-jar")) {
//
} else {
String md5 = md5File(jar);
File reJar = new File(project.getBuildDir().path +
File.separator + FD_INTERMEDIATES + File.separator + "replugin-jar"
+ File.separator + md5 + ".jar");
String reJarPath = reJar.getAbsolutePath()

boolean needInject = false
if (reJar.exists()) {
//检查修改插件版本
InjectorVersion injectorVersion = injectorMap.get(jar.getAbsolutePath());
if (injectorVersion != null) {
if (!AppConstant.VER.equals(injectorVersion.pluginVersion)) {
//版本变化了
String md5 = md5File(jar);
File reJar = new File(injectDir + File.separator + md5 + ".jar");
jarPath = reJar.getAbsolutePath()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不要复用之前的jarPath 变量,重新定义个reJarPath


boolean needInject = false
if (reJar.exists()) {
//检查修改插件版本
JarPatchInfo info = infoMap.get(jar.getAbsolutePath());
if (info != null) {
if (!AppConstant.VER.equals(info.pluginVersion)) {
//版本变化了
needInject = true
} else {
if (!md5.equals(info.jarMd5)) {
//原始jar内容变化
needInject = true
} else {
if (!md5.equals(injectorVersion.jarMd5)) {
//原始jar内容变化
needInject = true
}
}
} else {
//无记录
needInject = true
}
} else {
FileUtils.copyFile(jar, reJar)
needInject = true;
//无记录
needInject = true
}
//设置重定向jar
setJarInput(jarInput, reJar)
if (needInject) {
includeJars << reJarPath
map.put(reJarPath, reJarPath)

/* 将 jar 包解压,并将解压后的目录加入 classpath */
// println ">>> 解压Jar${jarPath}"
String jarZipDir = reJar.getParent() + File.separatorChar + reJar.getName().replace('.jar', '')
if (unzip(jarPath, jarZipDir)) {
classPath << jarZipDir
//保存修改的插件版本号
needSave = true
injectorMap.put(jar.getAbsolutePath(), new InjectorVersion(jar))

visitor.setBaseDir(jarZipDir)
Files.walkFileTree(Paths.get(jarZipDir), visitor)
}
// 删除 jar
} else {
needInject = true;
}
//设置重定向jar
setJarInput(jarInput, reJar)
if (needInject) {
/* 将 jar 包解压,并将解压后的目录加入 classpath */
// println ">>> 解压Jar${jarPath}"
String jarZipDir = reJar.getParent() + File.separatorChar + reJar.getName().replace('.jar', '')
if (unzip(jar.getAbsolutePath(), jarZipDir)) {

includeJars << jarPath
classPath << jarZipDir
//保存修改的插件版本号
needSave = true
infoMap.put(jar.getAbsolutePath(), new JarPatchInfo(jar))

visitor.setBaseDir(jarZipDir)
Files.walkFileTree(Paths.get(jarZipDir), visitor)

map.put(jarPath, jarPath)
}
// 删除 jar
if (reJar.exists()) {
FileUtils.forceDelete(reJar)
} else {
map.remove(reJarPath)
includeJars.remove(reJarPath)
}
}
}
}
}
if (needSave) {
saveJarInjectorHistory(project, injectorMap)
saveJarInjectorHistory(project, buildType, infoMap)
}
return classPath
}
Expand All @@ -182,40 +178,40 @@ public class Util {
/**
* 读取修改jar的记录
*/
def static readJarInjectorHistory(Project project) {
File file = new File(project.getBuildDir(), FD_INTERMEDIATES
+ File.separator + "replugin-jar" + File.separator + "version.json");
def static readJarInjectorHistory(Project project, String buildType) {
final String dir = FD_INTERMEDIATES + File.separator + "replugin-jar"+ File.separator + buildType;
File file = new File(project.getBuildDir(), dir + File.separator + "version.json");
if (!file.exists()) {
return new HashMap<String, InjectorVersion>();
return new HashMap<String, JarPatchInfo>();
}
Gson gson = new GsonBuilder()
.create();
FileReader fileReader = new FileReader(file)
JsonReader jsonReader = new JsonReader(fileReader);
Map<String, InjectorVersion> injectorMap = gson.fromJson(jsonReader, new TypeToken<Map<String, InjectorVersion>>() {
Map<String, JarPatchInfo> injectorMap = gson.fromJson(jsonReader, new TypeToken<Map<String, JarPatchInfo>>() {
}.getType());
jsonReader.close()
if (injectorMap == null) {
injectorMap = new HashMap<String, InjectorVersion>();
injectorMap = new HashMap<String, JarPatchInfo>();
}
return injectorMap;
}

/**
* 保存修改jar的记录
*/
def static saveJarInjectorHistory(Project project, Map<String, InjectorVersion> injectorMap) {
def static saveJarInjectorHistory(Project project,String buildType, Map<String, JarPatchInfo> injectorMap) {
Gson gson = new GsonBuilder()
.setPrettyPrinting()
.create();
File file = new File(project.getBuildDir(), FD_INTERMEDIATES
+ File.separator + "replugin-jar" + File.separator + "version.json");
final String dir = FD_INTERMEDIATES + File.separator + "replugin-jar"+ File.separator + buildType;
File file = new File(project.getBuildDir(), dir + File.separator + "version.json");
if (file.exists()) {
file.delete()
} else {
File dir = file.getParentFile();
if (!dir.exists()) {
dir.mkdirs()
File p = file.getParentFile();
if (!p.exists()) {
p.mkdirs()
}
}
file.createNewFile()
Expand Down