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. -dontusemixedcaseclassnames
  9. #表示不跳过library中的非public的类
  10. -dontskipnonpubliclibraryclasses
  11. #打印混淆的详细信息
  12. -verbose
  13. # Optimization is turned off by default. Dex does not like code run
  14. # through the ProGuard optimize and preverify steps (and performs some
  15. # of these optimizations on its own).
  16. -dontoptimize
  17. ##表示不进行校验,这个校验作用 在java平台上的
  18. -dontpreverify
  19. # Note that if you want to enable optimization, you cannot just
  20. # include optimization flags in your own project configuration file;
  21. # instead you will need to point to the
  22. # "proguard-android-optimize.txt" file instead of this one from your
  23. # project.properties file.
  24. -keepattributes *Annotation*
  25. -keep public class com.google.vending.licensing.ILicensingService
  26. -keep public class com.android.vending.licensing.ILicensingService
  27. # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
  28. -keepclasseswithmembernames class * {
  29. native ;
  30. }
  31. # keep setters in Views so that animations can still work.
  32. # see http://proguard.sourceforge.net/manual/examples.html#beans
  33. -keepclassmembers public class * extends android.view.View {
  34. void set*(***);
  35. *** get*();
  36. }
  37. # We want to keep methods in Activity that could be used in the XML attribute onClick
  38. -keepclassmembers class * extends android.app.Activity {
  39. public void *(android.view.View);
  40. }
  41. # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
  42. -keepclassmembers enum * {
  43. public static **[] values();
  44. public static ** valueOf(java.lang.String);
  45. }
  46. -keepclassmembers class * implements android.os.Parcelable {
  47. public static final android.os.Parcelable$Creator CREATOR;
  48. }
  49. -keepclassmembers class **.R$* {
  50. public static ;
  51. }
  52. # The support library contains references to newer platform versions.
  53. # Don't warn about those in case this app is linking against an older
  54. # platform version.We know about them, and they are safe.
  55. -dontwarn android.support.**
  56. # Understand the @Keep support annotation.
  57. -keep class android.support.annotation.Keep
  58. -keep @android.support.annotation.Keep class * {*; }
  59. -keepclasseswithmembers class * {
  60. @android.support.annotation.Keep ;
  61. }
  62. -keepclasseswithmembers class * {
  63. @android.support.annotation.Keep ;
  64. }
  65. -keepclasseswithmembers class * {
  66. @android.support.annotation.Keep (...);
  67. }
(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. -ignorewarnings
  8. #保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar
  9. -dontshrink
  10. #保护泛型
  11. -keepattributes Signature
(3)加入自己不想混淆的配置
【Android Studio 打包成jar文件并混淆代码】根据实际情况选择性配置

  1. #自己不想混淆的配置,保证com下的类名不被混淆
  2. -keep class com.csdn.info.**{*; }
3: 执行打包命令 在Terminal 窗口输入下面代码:
gradlew makeJar

提示BUILD SUCCESSFUL表示打包成功!

4:jar包路径
你的Module/build/outputs/jar/SystemInfo.jar(你的jar包名称)

    推荐阅读