1. 程式人生 > >AndroidStudio 3.2 打混淆jar包

AndroidStudio 3.2 打混淆jar包

1、引用此library,打包apk,生成對應release目錄

2、library  build.gradle中新增

apply plugin: 'com.android.application'
buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'net.sf.proguard:proguard-gradle:5.2.1'
    }
}

import proguard.gradle.ProGuardTask

def jarPath = 'build/outputs/jar/repackaged.jar'
task makeJar(type: ProGuardTask, dependsOn: "build") {
    //刪除之前編譯混淆jar包
    delete jarPath
    // 未混淆的jar,不同gradle版本目錄不一樣,坑坑坑坑坑
//    injars 'build/intermediates/packaged-classes/release/classes.jar'
    injars 'build/libs/carryFramework.jar'
    // 混淆後的jar路徑
    outjars jarPath
    // 混淆檔案
    configuration 'makejar-rules.pro'
}

3、混淆指令碼

##引入依賴包rt.jar(jdk路徑)(注意:如在makeJar的時候提示指定了兩次,可以將其註釋掉)
-libraryjars '/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre/lib/rt.jar'
##引入、依賴包android.jar(android SDK路徑)(注意:如在makeJar的時候提示指定了兩次,可以將其註釋掉)
-libraryjars '/Users/xievxin/Library/Android/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'

-keep class com.xiex.sdk.**{*;}

4、檢查對應release目錄下有classes.jar,在控制檯輸入

gradlew makeJar
or
./gradlew makeJar

(注:JD-GUI有快取,最好將jar拖入的時候換個名字。)

如果過程中還出現報錯,很有可能是Gradle版本引起。筆者的環境是:

moudle:

project:

擴充套件,打指定類檔案jar:

task releaseJar(type: Jar) {
    baseName "carryFramework"
    def fromDir = "${project.buildDir}/intermediates/javac/release/compileReleaseJavaWithJavac/classes/com/carry/"
    from fromDir
    into('com/carry')

    //去掉不需要打包的目錄和檔案
    exclude('BuildConfig.class', 'R.class')

    //去掉R$開頭的檔案
    exclude{ it.name.startsWith('R$')}
}
releaseJar.dependsOn build

如果要混淆的話: 

buildTypes {
    debug {
        minifyEnabled true 
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}