AndroidStudio如何生成混淆jar包

首先把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包

 
聲明:打出來的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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章