AndroidStudio如何生成混淆jar包

时间:2023-01-25 09:14:21

首先把module设置为library

修改对应module下面的build.gradle,把apply plugin后面的值修改为1apply plugin: 'com.android.library'并且删除
applicationId 
 
apply plugin: 'com.android.library'//apply plugin: 'com.android.application'android {    compileSdkVersion 23    buildToolsVersion "23.0.3"    defaultConfig {//        applicationId "com.east.xg.usbdata"        minSdkVersion 15        targetSdkVersion 23        versionCode 1        versionName "1.0"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:23.3.0'    compile files('libs/xUtils-2.6.14.jar')    compile files('libs/Bughd_android_sdk_v1.3.7.jar')}task makeJar(type: Copy) {    delete 'build/libs/mysdk.jar'    from('build/intermediates/bundles/release/')    into('build/libs/')    include('classes.jar')    rename ('classes.jar', 'mysdk.jar')}makeJar.dependsOn(build)//在终端执行生成JAR包// gradlew makeJar
Android Studio中对于library类型的Moudle,默认打出来的是AAR包, 
但有时候我们的SDK还需要共享给一些其他eclipse的项目使用,这样我们就需要输出JAR包, 
可以通过在Moudle中的build.gradle加入task来实现
task makeJar(type: Copy) {    delete 'build/libs/mysdk.jar' from('build/intermediates/bundles/release/')    into('build/libs/')    include('classes.jar')    rename ('classes.jar', 'mysdk.jar')}makeJar.dependsOn(build)//在终端执行生成JAR包// gradlew makeJar


在终端执行生成JAR包

gradview makeJar

在以下目录就可以找到我们生成的JAR包

AndroidStudio如何生成混淆jar包 
声明:打出来的jar只有源代码的.class 文件,不包含资源文件

然后再在module的gradle中配置如下
 
def SDK_BASENAME = "hibotlibs";def SDK_VERSION = "_v1.0";def sdkDestinationPath = "build/libs/";def zipFile = file('build/intermediates/bundles/release/classes.jar')task deleteBuild(type: Delete) {    delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"}task makeJar(type: Jar) {    from zipTree(zipFile)    from fileTree(dir: 'src/main', includes: ['assets/**']) // 打包assets目录下的所有文件    baseName = SDK_BASENAME + SDK_VERSION    destinationDir = file(sdkDestinationPath)}makeJar.dependsOn(deleteBuild, build)

 

  上面的配置具体我就不讲解他们是什么意思了。

     因为我们是自己手动混淆了,所以要指定混淆规则,然后打开module的proguard-rules.pro文件,将AndroidStudio默认的混淆文件复制、粘贴到proguard-rules.pro中。

全部proguard-rules.pro配置如下
###########################以下是AndroidStudio自带的混淆配置协议################################ 表示混淆时不使用大小写混合类名-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#指定不混淆所有的JNI方法-keepclasseswithmembernames class * {    native <methods>;}# keep setters in Views so that animations can still work.# see http://proguard.sourceforge.net/manual/examples.html#beans#所有View的子类及其子类的get、set方法都不进行混淆-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# 不混淆Activity中参数类型为View的所有方法-keepclassmembers class * extends android.app.Activity {   public void *(android.view.View);}# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations# 不混淆Enum类型的指定方法-keepclassmembers enum * {    public static **[] values();    public static ** valueOf(java.lang.String);}# 不混淆Parcelable和它的子类,还有Creator成员变量-keepclassmembers class * implements android.os.Parcelable {  public static final android.os.Parcelable$Creator CREATOR;}# 不混淆R类里及其所有内部static类中的所有static变量字段-keepclassmembers class **.R$* {    public static <fields>;}# 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 <methods>;}-keepclasseswithmembers class * {    @android.support.annotation.Keep <fields>;}-keepclasseswithmembers class * {    @android.support.annotation.Keep <init>(...);}#################################以下是自己添加的混淆协议####################################下面代码中的路径配置,你要修改成与你相对应的路径#引入依赖包rt.jar(jdk路径)(注意:如在makeJar的时候提示指定了两次,可以将其注释掉)#-libraryjars 'C:\Android_Develop_Tools\Java\jdk1.8.0_101\jre\lib\rt.jar'#引入依赖包android.jar(android SDK路径)(注意:如在makeJar的时候提示指定了两次,可以将其注释掉)#-libraryjars 'C:\Android_Develop_Tools\sdk\platforms\android-23\android.jar'#如果用到Appcompat包,需要引入(注意:如在makeJar的时候提示指定了两次,可以将其注释掉)#-libraryjars 'D:\AndroidStudioProjects\MyApplication\mylibrary\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.4.0\jars\classes.jar'#-libraryjars 'D:\AndroidStudioProjects\MyApplication\mylibrary\build\intermediates\exploded-aar\com.android.support\support-v4\23.4.0\jars\classes.jar'#忽略警告-ignorewarnings#保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码是无用的,从而把所有的代码压缩掉,导出一个空的jar-dontshrink#保护泛型-keepattributes Signature#下面的Test类将不会被混淆,这样的类是需要被jar包使用者直接调用的-keep class com.example.mylibrary.usbSendType.ClassicType{    public *;}-keep class com.example.mylibrary.usbSendType.EleType{    public *;}-keep class com.example.mylibrary.usbSendType.LightType{    public *;}-keep class com.example.mylibrary.usbSendType.MotorPType{    public *;}
然后你就在Terminal中输入
gradlew makeJar
或者是双击 makeJar
我们将生成的jar包解压可以看到ClassicType和EleType、LightType、MotorPType没有被混淆,这就说明成功了。
生成自定义的混淆jar包,也可以参考
http://blog.csdn.net/lsyz0021/article/details/53107595

 

当然我们不希望自己提供的jar 也给其他开发者带来同样的困扰,所以我们期待可以有选择性的选择需要打包的class,排除不需要的class ,如BuildConfig.class、R.class 等

task makeJar(type: Jar) {    from file('build/intermediates/classes/release')    archiveName = 'sdk.jar'    destinationDir = file('build/libs')    //过滤不需要的class    exclude "**/**/BuildConfig.class"    exclude "**/**/BuildConfig\$*.class"    exclude "**/R.class"    exclude "**/R\$*.class"    //指定打包的class    include "com/test/**/*.class"}makeJar.dependsOn(build)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

通过上述脚本就可以实现选择性的打包,关于打包时的混淆配置等,感兴趣的朋友可以参考: 
http://my.oschina.net/u/2531612/blog/591366?fromerr=Xd1Kd7dY