Android Studio 3.4.1 version generates Jar packages and obfuscates Jar package methods

The method of generating each version of Android Studio is different.

1. Generate a jar package without obfuscation:

1. First find the build.gradle file under the module project where the jar package needs to be generated, and add the task task directly at the bottom:

task makeJar(type: Jar, dependsOn: ['compileReleaseJavaWithJavac']) {
    destinationDir = file('build/libs/jar/')//jar包保存位置
    baseName =test   // Jar名称
    from('build/intermediates/javac/release/compileReleaseJavaWithJavac/classes/')

    exclude('**/BuildConfig.class')  //exclude 命令就是 新jar包需要删除的类或文件
    exclude('**/BuildConfig\$*.class')
    exclude('**/R.class')
    exclude('**/R\$*.class')
    include('**/*.class')
}

2. Click the Terminal at the bottom of AS, use the command line to execute the task, and enter gradlew makeJar on the command line. In this way, all tasks including makeJar in the entire project will be executed, and all jar packages will be generated at once.

You can see that the execution is successful as shown below.

If it was helpful to you, please give it a like and leave?

2. Generate obfuscated jar package

1. First find the build.gradle file under the module project where the jar package needs to be generated, and add the task task directly at the bottom:

task makeJar(type: Jar, dependsOn: ['compileReleaseJavaWithJavac']) {
    destinationDir = file('build/libs/jar/')//jar包保存位置
    baseName =test   // Jar名称
    from('build/intermediates/javac/release/compileReleaseJavaWithJavac/classes/')

    exclude('**/BuildConfig.class')  //exclude 命令就是 新jar包需要删除的类或文件
    exclude('**/BuildConfig\$*.class')
    exclude('**/R.class')
    exclude('**/R\$*.class')
    include('**/*.class')
}

task proguardJar(dependsOn: ['makeJar'], type: proguard.gradle.ProGuardTask) {
   //manifest 注册的组件对应的 proguard 文件
    configuration 'proguard-rules.pro'
    String inJar = makeJar.archivePath.getAbsolutePath()
    //输入 jar
    injars inJar
    //输出 jar 的位置和名称
    String outJar = inJar.substring(0, inJar.lastIndexOf(File.separator)) + "/proguard-${makeJar.archiveName}"
    outjars outJar
    //设置不删除未引用的资源(类,方法等)
    dontshrink
}

afterEvaluate {
    bundleRelease.dependsOn libCopy
}

2. Click the Terminal at the bottom of AS, use the command line to execute the task, and enter gradlew proguardJar on the command line. In this way, all the tasks including proguardJar in the entire project will be executed, and all jar packages will be generated at once. There is no need to execute the gradle makeJar command here . .

If it was helpful to you, please give it a like and leave?

Finally, just find the new jar package directly in the directory you set above:

Be sure to open the test.jar package to check whether the resource class is complete

Guess you like

Origin blog.csdn.net/zhao8856234/article/details/111035282