一. Gradle编译混销魂的jar包,流程:
开发编译环境为Android studio 3.6.2,其他版本可能会有不一样的地方,
1. 增加编译task
在需要编译jar包的build.gradle文件的根中增加task :
//app/build.gradle:
//apply plugin: 'com.android.application'
apply plugin: 'com.android.library'
.........................
//自定义编译任务名称
task makeMyJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") {
// 未混淆的jar: compile_library_classes为android studio 3.6.2版本编译产出
// 其他版本可能不一样,可打开build/intermediates/目录查找,低版本的事bundle
injars 'build/intermediates/compile_library_classes/release/classes.jar'
// 混淆后的jar路径
outjars 'build/My-sdk.jar'
// 混淆规则文件,具体需要keep的类
configuration 'proguard-rules.pro'
}
2. 执行makeMyJar编译任务:
(1). 直接在Android studio右上角的task中找到makeMyJar双击即可开始编译;
(2). 项目根目录下打开终端Terminal,输入 ./gradlew makeMyjar 回车即可.
二 解决lint错误 Lint found errors in the project; aborting build.
1. 错误信息
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':MyApp:lint'.
> Lint found errors in the project; aborting build.
Fix the issues identified by lint, or add the following to your build script to proceed with errors:
...
android {
lintOptions {
abortOnError false
}
}
...
The first 3 errors (out of 4) were:
原因: 以上错误原因是执行lint检查事出现错误;
2. 解决方案:
按照提示信息,在build.gradle的android{}中增加配置禁用即可:
lintOptions {
abortOnError false
}
三. 解决警告过多错误 java.io.IOException: Please correct the above warnings first.
编译过程中出现很多警告,最后错误
1. 错误信息
Warning: com.hulk.demo.User: can't find referenced class java.lang.Class
Warning: com.hulk.demo.UserManager: can't find referenced class java.lang.Class
很多警告......................................
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':MyApp:makeMyJar'.
> java.io.IOException: Please correct the above warnings first.
................
原因: 警告过多导致错误
2. 解决方案:
在proguard-rules.pro(可以使其他自定义名字的文件)中增加不警告声明:
#不警告编译过程中的警告:Please correct the above warnings first
#-dontwarn com.mobile.emm.**
如proguard-rules.pro:
#不警告编译过程中的警告:Please correct the above warnings first
#-dontwarn com.mobile.emm.**
#keep user
-keep class com.hulk.demo.User** {*;}
.......................
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
总结:
错误一般会有提示信息,仔细分级一般都能找到解决办法