Skip to content
Open
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 @@ -22,13 +22,17 @@ import com.android.build.api.transform.JarInput
import com.android.build.api.transform.TransformInput
import com.android.build.gradle.internal.scope.GlobalScope
import com.android.sdklib.IAndroidTarget
import com.qihoo360.replugin.gradle.plugin.AppConstant
import org.apache.commons.codec.digest.DigestUtils
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.lang.reflect.Field
import java.nio.file.Files
import java.nio.file.Paths
import java.util.zip.ZipEntry
import java.util.zip.ZipFile

import static com.android.builder.model.AndroidProject.FD_INTERMEDIATES;
Expand Down Expand Up @@ -95,26 +99,125 @@ public class Util {
}

} else {
//重定向jar
File reJar = new File(project.getBuildDir().path +
File.separator + FD_INTERMEDIATES + File.separator + "replugin-jar"
+ File.separator + md5File(jar) + ".jar");
String reJarPath = reJar.getAbsolutePath()
boolean needInject = false
if (reJar.exists()) {
//检查修改插件版本
needInject = checkJarInjectorVersion(reJarPath)
} else {
FileUtils.copyFile(jar, reJar)
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
//保存修改的插件版本号
saveJarInjectorVersion(jarZipDir)
visitor.setBaseDir(jarZipDir)
Files.walkFileTree(Paths.get(jarZipDir), visitor)
}
// 删除 jar
FileUtils.forceDelete(reJar)
}
}
}
}
return classPath
}

includeJars << jarPath
map.put(jarPath, jarPath)
def static md5File(File jar){
FileInputStream fileInputStream = new FileInputStream(jar);
String md5 = DigestUtils.md5Hex(fileInputStream);
fileInputStream.close()
return md5
}

/* 将 jar 包解压,并将解压后的目录加入 classpath */
// println ">>> 解压Jar${jarPath}"
String jarZipDir = jar.getParent() + File.separatorChar + jar.getName().replace('.jar', '')
if (unzip(jarPath, jarZipDir)) {
classPath << jarZipDir
def static setJarInput(JarInput jarInput, File rejar) {
Field fileField = null;
Class<?> clazz = jarInput.getClass();
while(fileField == null && clazz != Object.class){
try {
fileField = clazz.getDeclaredField("file");
} catch (Exception e) {
//ignore
clazz = clazz.getSuperclass();
}
}
if(fileField != null){
fileField.setAccessible(true);
fileField.set(jarInput, rejar);
}
}

visitor.setBaseDir(jarZipDir)
Files.walkFileTree(Paths.get(jarZipDir), visitor)
}
/**
* 通过META-INF/replugin_version.txt判断是否需要修改
*/
def static checkJarInjectorVersion(String jar) {
boolean needInjector = true;
ZipFile zf = null;
ZipEntry ze = null;
InputStream inputStream = null;
try{
zf = new ZipFile(jar);
ze = zf.getEntry("META-INF/replugin_version.txt");
if(ze != null){
byte[] data = new byte[jar.length()];
inputStream = zf.getInputStream(ze);
int len = inputStream.read(data, 0, data.length);
String ver = new String(data, "utf-8").trim();
needInjector = !AppConstant.VER.equals(ver);
}
}catch (Throwable e){
//
}finally{
if(inputStream != null){
inputStream.close();
}
if(zf != null){
zf.close();
}
}
return needInjector;
}

// 删除 jar
FileUtils.forceDelete(jar)
}
/**
* 记录版本号到META-INF/replugin_version.txt
*/
def static saveJarInjectorVersion(String jarZipDir) {
File verFile = new File(jarZipDir, "META-INF/replugin_version.txt");
if(verFile.exists()){
verFile.delete();
}else{
File dir = verFile.getParentFile();
if(!dir.exists()){
dir.mkdirs();
}
}
verFile.createNewFile();
FileOutputStream fileOutputStream = null;
try{
fileOutputStream = new FileOutputStream(verFile);
fileOutputStream.write(AppConstant.VER.getBytes("utf-8"));
fileOutputStream.flush();
}catch (Throwable e){
//
}finally{
if(fileOutputStream!=null){
fileOutputStream.close();
}
}
return classPath
}

/**
Expand Down