配置buidl.gradle文件 > 配置混淆规则 > 生成ja..._android studio aar 混淆">
当前位置:   article > 正文

AndroidStudio 生成jar 和 aar(混淆)_android studio aar 混淆

android studio aar 混淆

首先看下效果图,左边是封装之前的代码,右边是生成混淆jar包 供"兄弟公司"使用. 

目前最常见的有*.so,*.jar,*.aar三种(.so一般C或者C++使用,我们说下后两者) 

做之前感觉有点神秘且高大上,其实很简单,大致分为四步:

创建好moudle将需要生成jar的类准备好  >  配置buidl.gradle文件  >    配置混淆规则  >  生成jar包

moudle准备好以后先配置buidl.gradle

打开minifyEnabled开关,然后将下面代码复制到dependencies同级别下(AndroidStudio3.0以下需要更改路径):

  1. def SDK_BASENAME = "roy"
  2. def SDK_VERSION = "1.0.0"
  3. def sdkDestinationPath = "build/outputs/jar/"
  4. def zipFile = file('build/intermediates/packaged-classes/release/classes.jar')
  5. task deleteBuild(type: Delete) {
  6. delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"
  7. }
  8. task makeJar(type: Jar) {
  9. from zipTree(zipFile)
  10. // 打包assets目录下的所有文件
  11. from fileTree(dir: 'src/main', includes: ['assets/**'])
  12. baseName = SDK_BASENAME + SDK_VERSION
  13. destinationDir = file(sdkDestinationPath)
  14. }
  15. makeJar.dependsOn(deleteBuild, build)
 

配置proguard-rules.pro

混淆规则分为两部分,部分代码需要自己手动配置,先看下需要手动配置的代码,博客最后会粘贴整个文件

红框内容需要手动配置本地路径,包名,类名

生成jar包

打开Terminal控制台,输入gradlew makeJar运行,出现BUILD SUCCESSFUL代表成功

注释:如果失败,仔细查看失败提示,一步一步解决;如果出现空包,代表混淆规则有问题。

复制到需要使用的项目中可以看到:

最后附上proguard-rules.pro文件全部代码:

  1. # Add project specific ProGuard rules here.
  2. # You can control the set of applied configuration files using the
  3. # proguardFiles setting in build.gradle.
  4. #
  5. # For more details, see
  6. # http://developer.android.com/guide/developing/tools/proguard.html
  7. # If your project uses WebView with JS, uncomment the following
  8. # and specify the fully qualified class name to the JavaScript interface
  9. # class:
  10. #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
  11. # public *;
  12. #}
  13. # Uncomment this to preserve the line number information for
  14. # debugging stack traces.
  15. #-keepattributes SourceFile,LineNumberTable
  16. # If you keep the line number information, uncomment this to
  17. # hide the original source file name.
  18. #-renamesourcefileattribute SourceFile
  19. # 表示混淆时不使用大小写混合类名
  20. -dontusemixedcaseclassnames
  21. # 表示不跳过library中的非public的类
  22. -dontskipnonpubliclibraryclasses
  23. # 打印混淆的详细信息
  24. -verbose
  25. # Optimization is turned off by default. Dex does not like code run
  26. # through the ProGuard optimize and preverify steps (and performs some
  27. # of these optimizations on its own).
  28. -dontoptimize
  29. # 表示不进行校验,这个校验作用 在java平台上的
  30. -dontpreverify
  31. # Note that if you want to enable optimization, you cannot just
  32. # include optimization flags in your own project configuration file;
  33. # instead you will need to point to the
  34. # "proguard-android-optimize.txt" file instead of this one from your
  35. # project.properties file.
  36. #使用注解需要添加
  37. -keepattributes *Annotation*
  38. -keep public class com.google.vending.licensing.ILicensingService
  39. -keep public class com.android.vending.licensing.ILicensingService
  40. # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
  41. #指定不混淆所有的JNI方法
  42. -keepclasseswithmembernames class * {
  43. native <methods>;
  44. }
  45. # keep setters in Views so that animations can still work.
  46. # see http://proguard.sourceforge.net/manual/examples.html#beans
  47. #所有View的子类及其子类的get、set方法都不进行混淆
  48. -keepclassmembers public class * extends android.view.View {
  49. void set*(***);
  50. *** get*();
  51. }
  52. # We want to keep methods in Activity that could be used in the XML attribute onClick
  53. # 不混淆Activity中参数类型为View的所有方法
  54. -keepclassmembers class * extends android.app.Activity {
  55. public void *(android.view.View);
  56. }
  57. # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
  58. # 不混淆Enum类型的指定方法
  59. -keepclassmembers enum * {
  60. public static **[] values();
  61. public static ** valueOf(java.lang.String);
  62. }
  63. # 不混淆Parcelable和它的子类,还有Creator成员变量
  64. -keepclassmembers class * implements android.os.Parcelable {
  65. public static final android.os.Parcelable$Creator CREATOR;
  66. }
  67. # 不混淆R类里及其所有内部static类中的所有static变量字段
  68. -keepclassmembers class **.R$* {
  69. public static <fields>;
  70. }
  71. # The support library contains references to newer platform versions.
  72. # Don't warn about those in case this app is linking against an older
  73. # platform version. We know about them, and they are safe.
  74. # 不提示兼容库的错误警告
  75. -dontwarn android.support.**
  76. # Understand the @Keep support annotation.
  77. -keep class android.support.annotation.Keep
  78. -keep @android.support.annotation.Keep class * {*;}
  79. -keepclasseswithmembers class * {
  80. @android.support.annotation.Keep <methods>;
  81. }
  82. -keepclasseswithmembers class * {
  83. @android.support.annotation.Keep <fields>;
  84. }
  85. -keepclasseswithmembers class * {
  86. @android.support.annotation.Keep <init>(...);
  87. }
  88. ###########################以下是需要手动的混淆配置协议###############################
  89. # 注意:以下两个路径是本地jar包的位置
  90. -libraryjars "C:\Program Files\Java\jre1.8.0_181\lib\rt.jar"
  91. -libraryjars "C:\Users\THINKPAD\AppData\Local\Android\Sdk\platforms\android-23\android.jar"
  92. #代码迭代优化的次数,默认5
  93. -optimizationpasses 5
  94. #混淆时不会产生形形色色的类名
  95. -dontusemixedcaseclassnames
  96. #忽略警告
  97. -ignorewarnings
  98. #以下是不需要混淆的文件,需要将用到的类和方法暴露出去供兄弟公司使用
  99. -keep class com.example.jarlibrary.SocketUtil{
  100. # 保持了类mylibrary里面public 修饰的成员变量和public修饰的方法。
  101. public <fields>;
  102. public <methods>;
  103. }

生成aar包

aar相比jar包有所区别,包含所有资源文件全部打包,打包方式及其简单,如上面jar一样,生成位置:

aar包中引入的三方库不会打进去,所以在使用的项目中需重新引入,使用方式和jar有所不同(两步);

第一步:将aar包拷贝到libs目录下

第二部:配置build.gradle

  1. repositories {                          
  2. flatDir {                             
  3.   dirs'libs'                        
  4.   }                    
  5. }
  6.   
  7. dependencies {
  8.     compile(name:'aar名字', ext:'aar')                     

  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/331479
推荐阅读
相关标签
  

闽ICP备14008679号