Android Studio 打包成jar文件并混淆代码
在Studio中混淆打包需要修改模块下的build.gradle文件和proguard-rules.pro文件
1:首先修改指定模块的build.gradle文件,在末尾加上下述代码
task makeJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") { //删除之前编译混淆jar包 delete'build/outputs/jar/SystemInfo.jar' // 未混淆的jar injars 'build/intermediates/bundles/release/classes.jar' // 混淆后的jar路径 outjars 'build/outputs/jar/SystemInfo.jar' // 混淆文件 configuration 'proguard-rules.pro' }
说明:AS会自动对模块进行打包,即build/intermediates/bundles/release/classes.jar,只是未进行混淆工作而已;
2:配置proguard-rules.pro文件
(1)把AS自带的协议配置进来
- # This is a configuration file for ProGuard.
- # http://proguard.sourceforge.net/index.html#manual/usage.html
- #
- # Starting with version 2.2 of the Android plugin for Gradle, these files are no longer used. Newer
- # versions are distributed with the plugin and unpacked at build time. Files in this directory are
- # no longer maintained.
- #表示混淆时不使用大小写混合类名
- -dontusemixedcaseclassnames
- #表示不跳过library中的非public的类
- -dontskipnonpubliclibraryclasses
- #打印混淆的详细信息
- -verbose
- # Optimization is turned off by default. Dex does not like code run
- # through the ProGuard optimize and preverify steps (and performs some
- # of these optimizations on its own).
- -dontoptimize
- ##表示不进行校验,这个校验作用 在java平台上的
- -dontpreverify
- # Note that if you want to enable optimization, you cannot just
- # include optimization flags in your own project configuration file;
- # instead you will need to point to the
- # "proguard-android-optimize.txt" file instead of this one from your
- # project.properties file.
- -keepattributes *Annotation*
- -keep public class com.google.vending.licensing.ILicensingService
- -keep public class com.android.vending.licensing.ILicensingService
- # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
- -keepclasseswithmembernames class * {
- native
; - }
- # keep setters in Views so that animations can still work.
- # see http://proguard.sourceforge.net/manual/examples.html#beans
- -keepclassmembers public class * extends android.view.View {
- void set*(***);
- *** get*();
- }
- # We want to keep methods in Activity that could be used in the XML attribute onClick
- -keepclassmembers class * extends android.app.Activity {
- public void *(android.view.View);
- }
- # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
- -keepclassmembers enum * {
- public static **[] values();
- public static ** valueOf(java.lang.String);
- }
- -keepclassmembers class * implements android.os.Parcelable {
- public static final android.os.Parcelable$Creator CREATOR;
- }
- -keepclassmembers class **.R$* {
- public static
; - }
- # The support library contains references to newer platform versions.
- # Don't warn about those in case this app is linking against an older
- # platform version.We know about them, and they are safe.
- -dontwarn android.support.**
- # Understand the @Keep support annotation.
- -keep class android.support.annotation.Keep
- -keep @android.support.annotation.Keep class * {*; }
- -keepclasseswithmembers class * {
- @android.support.annotation.Keep
; - }
- -keepclasseswithmembers class * {
- @android.support.annotation.Keep
; - }
- -keepclasseswithmembers class * {
- @android.support.annotation.Keep
(...); - }
- #引入依赖包rt.jar(jdk路径)
- -libraryjars 'D:\Android_Studio\Android_Studio_Install\jre\jre\lib\rt.jar'
- #引入依赖包android.jar(android SDK路径)
- -libraryjars 'D:\Android_Studio\Android_SDK\platforms\android-25\android.jar'
- #如果用到其他包,需要引入
- #忽略警告
- -ignorewarnings
- #保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
- -dontshrink
- #保护泛型
- -keepattributes Signature
【Android Studio 打包成jar文件并混淆代码】根据实际情况选择性配置
- #自己不想混淆的配置,保证com下的类名不被混淆
- -keep class com.csdn.info.**{*; }
gradlew makeJar
提示BUILD SUCCESSFUL表示打包成功!
4:jar包路径
你的Module/build/outputs/jar/SystemInfo.jar(你的jar包名称)
推荐阅读
- android第三方框架(五)ButterKnife
- 遇到一哭二闹三打滚的孩子,怎么办┃山伯教育
- Ⅴ爱阅读,亲子互动——打卡第178天
- 我错了,余生不再打扰
- Android中的AES加密-下
- 2018-3-24
- 日志打卡
- 以读攻“毒”唤新活动曹彦斌打卡第二天
- 带有Hilt的Android上的依赖注入
- 如何在Mac中的文件选择框中打开系统隐藏文件夹