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自带的协议配置进来

  1. # This is a configuration file for ProGuard.  
  2. # http://proguard.sourceforge.net/index.html#manual/usage.html  
  3. #  
  4. # Starting with version 2.2 of the Android plugin for Gradle, these files are no longer used. Newer  
  5. # versions are distributed with the plugin and unpacked at build time. Files in this directory are  
  6. # no longer maintained.  
  7.   
  8. #表示混淆时不使用大小写混合类名  
  9. -dontusemixedcaseclassnames  
  10. #表示不跳过library中的非public的类  
  11. -dontskipnonpubliclibraryclasses  
  12. #打印混淆的详细信息  
  13. -verbose  
  14.   
  15. # Optimization is turned off by default. Dex does not like code run  
  16. # through the ProGuard optimize and preverify steps (and performs some  
  17. # of these optimizations on its own).  
  18. -dontoptimize  
  19. ##表示不进行校验,这个校验作用 在java平台上的  
  20. -dontpreverify  
  21. # Note that if you want to enable optimization, you cannot just  
  22. # include optimization flags in your own project configuration file;  
  23. # instead you will need to point to the  
  24. "proguard-android-optimize.txt" file instead of this one from your  
  25. # project.properties file.  
  26.   
  27. -keepattributes *Annotation*  
  28. -keep public class com.google.vending.licensing.ILicensingService  
  29. -keep public class com.android.vending.licensing.ILicensingService  
  30.   
  31. # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native  
  32. -keepclasseswithmembernames class * {  
  33.     native <methods>;  
  34. }  
  35.   
  36. # keep setters in Views so that animations can still work.  
  37. # see http://proguard.sourceforge.net/manual/examples.html#beans  
  38. -keepclassmembers public class * extends android.view.View {  
  39.    void set*(***);  
  40.    *** get*();  
  41. }  
  42.   
  43. # We want to keep methods in Activity that could be used in the XML attribute onClick  
  44. -keepclassmembers class * extends android.app.Activity {  
  45.    public void *(android.view.View);  
  46. }  
  47.   
  48. # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations  
  49. -keepclassmembers enum * {  
  50.     public static **[] values();  
  51.     public static ** valueOf(java.lang.String);  
  52. }  
  53.   
  54. -keepclassmembers class * implements android.os.Parcelable {  
  55.   public static final android.os.Parcelable$Creator CREATOR;  
  56. }  
  57.   
  58. -keepclassmembers class **.R$* {  
  59.     public static <fields>;  
  60. }  
  61.   
  62. # The support library contains references to newer platform versions.  
  63. # Don't warn about those in case this app is linking against an older  
  64. # platform version.  We know about them, and they are safe.  
  65. -dontwarn android.support.**  
  66.   
  67. # Understand the @Keep support annotation.  
  68. -keep class android.support.annotation.Keep  
  69.   
  70. -keep @android.support.annotation.Keep class * {*;}  
  71.   
  72. -keepclasseswithmembers class * {  
  73.     @android.support.annotation.Keep <methods>;  
  74. }  
  75.   
  76. -keepclasseswithmembers class * {  
  77.     @android.support.annotation.Keep <fields>;  
  78. }  
  79.   
  80. -keepclasseswithmembers class * {  
  81.     @android.support.annotation.Keep <init>(...);  
  82. }

(2)引入依赖包路径

  1. #引入依赖包rt.jar(jdk路径)  
  2. -libraryjars 'D:\Android_Studio\Android_Studio_Install\jre\jre\lib\rt.jar'  
  3. #引入依赖包android.jar(android SDK路径)  
  4. -libraryjars 'D:\Android_Studio\Android_SDK\platforms\android-25\android.jar'  
  5. #如果用到其他包,需要引入  
  6.   
  7. #忽略警告  
  8. -ignorewarnings  
  9. #保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar  
  10. -dontshrink  
  11. #保护泛型  
  12. -keepattributes Signature

(3)加入自己不想混淆的配置

根据实际情况选择性配置

  1. #自己不想混淆的配置,保证com下的类名不被混淆  
  2. -keep class com.csdn.info.**{*;}  
3: 执行打包命令

在Terminal 窗口输入下面代码:

gradlew makeJar

提示BUILD SUCCESSFUL表示打包成功!

4:jar包路径

你的Module/build/outputs/jar/SystemInfo.jar(你的jar包名称)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值