Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复第三方库混淆出r文件与R文件冲突导致报错问题。eg:Caused by: java.lang.RuntimeException: ca… #465

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ dependencies {
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.android.support:design:25.4.0'
compile 'org.javassist:javassist:3.20.0-GA'
compile 'org.javassist:javassist:3.22.0-GA'
compile project(path: ':patch')
}
2 changes: 1 addition & 1 deletion auto-patch-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
compile gradleApi()
compile localGroovy()
compile 'com.android.tools.build:gradle:2.1.0'
compile 'org.javassist:javassist:3.20.0-GA'
compile 'org.javassist:javassist:3.22.0-GA'
// compile 'com.meituan.robust:autopatchbase:' + VERSION_NAME
compile project(':autopatchbase')
}
Expand Down
2 changes: 1 addition & 1 deletion gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {
compile gradleApi()
compile localGroovy()
compile 'com.android.tools.build:gradle:2.1.0'
compile 'org.javassist:javassist:3.20.0-GA'
compile 'org.javassist:javassist:3.22.0-GA'
compile fileTree(dir: "./src/main/libs", include: ['*.jar'])
compile project(':autopatchbase')
// compile 'com.meituan.robust:autopatchbase:0.4.93'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,65 @@ import java.util.regex.Matcher
* Created by mivanzhang on 16/11/3.
*/
class ConvertUtils {
static List<CtClass> toCtClasses(Collection<TransformInput> inputs, ClassPool classPool) {
List<String> classNames = new ArrayList<>()
static List<CtClass> toCtClasses(List<String> exceptCtClassNameList, Collection<TransformInput> inputs, ClassPool directoryClassPool, ClassPool jarClassPool) {
List<String> directoryClassNames = new ArrayList<>()
List<String> jarClassNames = new ArrayList<>()
List<CtClass> allClass = new ArrayList<>();
def startTime = System.currentTimeMillis()
inputs.each {
it.directoryInputs.each {
def dirPath = it.file.absolutePath
classPool.insertClassPath(it.file.absolutePath)
directoryClassPool.insertClassPath(it.file.absolutePath)
org.apache.commons.io.FileUtils.listFiles(it.file, null, true).each {
if (it.absolutePath.endsWith(SdkConstants.DOT_CLASS)) {
def className = it.absolutePath.substring(dirPath.length() + 1, it.absolutePath.length() - SdkConstants.DOT_CLASS.length()).replaceAll(Matcher.quoteReplacement(File.separator), '.')
if(classNames.contains(className)){
throw new RuntimeException("You have duplicate classes with the same name : "+className+" please remove duplicate classes ")
if(!isExceptClassNameClass(exceptCtClassNameList, className)) {
if (directoryClassNames.contains(className)) {
throw new RuntimeException("You have duplicate classes with the same name : " + className + " please remove duplicate classes ")
}
directoryClassNames.add(className)
}
classNames.add(className)
}
}
}

it.jarInputs.each {
classPool.insertClassPath(it.file.absolutePath)
jarClassPool.insertClassPath(it.file.absolutePath)
def jarFile = new JarFile(it.file)
Enumeration<JarEntry> classes = jarFile.entries();
while (classes.hasMoreElements()) {
JarEntry libClass = classes.nextElement();
String className = libClass.getName();
if (className.endsWith(SdkConstants.DOT_CLASS)) {
className = className.substring(0, className.length() - SdkConstants.DOT_CLASS.length()).replaceAll('/', '.')
if(classNames.contains(className)){
throw new RuntimeException("You have duplicate classes with the same name : "+className+" please remove duplicate classes ")
if(!isExceptClassNameClass(exceptCtClassNameList, className)) {
if (jarClassNames.contains(className) || directoryClassNames.contains(className)) {
throw new RuntimeException("You have duplicate classes with the same name : " + className + " please remove duplicate classes ")
}
jarClassNames.add(className)
}
classNames.add(className)
}
}
}
}
def cost = (System.currentTimeMillis() - startTime) / 1000
println "read all class file cost $cost second"
classNames.each {
directoryClassNames.each {
try {
allClass.add(classPool.get(it));
allClass.add(directoryClassPool.get(it));
} catch (javassist.NotFoundException e) {
println "class not found exception class name: $it "

}
}

jarClassNames.each {
try {
allClass.add(jarClassPool.get(it));
} catch (javassist.NotFoundException e) {
println "class not found exception class name: $it "

}
}

Collections.sort(allClass, new Comparator<CtClass>() {
Expand All @@ -69,5 +82,15 @@ class ConvertUtils {
return allClass;
}

protected static boolean isExceptClassNameClass(List<String> exceptCtClassNameList, String className) {
if(exceptCtClassNameList != null) {
for (String exceptName : exceptCtClassNameList) {
if (className.startsWith(exceptName)) {
return true;
}
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class RobustTransform extends Transform implements Plugin<Project> {
private static List<String> hotfixMethodList = new ArrayList<>();
private static List<String> exceptPackageList = new ArrayList<>();
private static List<String> exceptMethodList = new ArrayList<>();
private static List<String> exceptCtClassNameList = new ArrayList<>();
private static boolean isHotfixMethodLevel = false;
private static boolean isExceptMethodLevel = false;
// private static boolean isForceInsert = true;
Expand Down Expand Up @@ -76,6 +77,7 @@ class RobustTransform extends Transform implements Plugin<Project> {
hotfixMethodList = new ArrayList<>()
exceptPackageList = new ArrayList<>()
exceptMethodList = new ArrayList<>()
exceptCtClassNameList = new ArrayList<>()
isHotfixMethodLevel = false;
isExceptMethodLevel = false;
/*对文件进行解析*/
Expand All @@ -91,7 +93,9 @@ class RobustTransform extends Transform implements Plugin<Project> {
for (name in robust.exceptMethod.name) {
exceptMethodList.add(name.text());
}

for(name in robust.exceptCtClassName.name) {
exceptCtClassNameList.add(name.text());
}
if (null != robust.switch.filterMethod && "true".equals(String.valueOf(robust.switch.turnOnHotfixMethod.text()))) {
isHotfixMethodLevel = true;
}
Expand Down Expand Up @@ -153,12 +157,18 @@ class RobustTransform extends Transform implements Plugin<Project> {
jarFile.delete();
}

ClassPool classPool = new ClassPool()
ClassPool directoryClassPool = new ClassPool()
project.android.bootClasspath.each {
directoryClassPool.appendClassPath((String) it.absolutePath)
}

ClassPool jarClassPool = new ClassPool()
project.android.bootClasspath.each {
classPool.appendClassPath((String) it.absolutePath)
jarClassPool.appendClassPath((String) it.absolutePath)
}

def box = ConvertUtils.toCtClasses(inputs, classPool)
def box = ConvertUtils.toCtClasses(exceptCtClassNameList, inputs, directoryClassPool, jarClassPool)

def cost = (System.currentTimeMillis() - startTime) / 1000
// logger.quiet "check all class cost $cost second, class count: ${box.size()}"
if (useASM) {
Expand Down