温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android studio利用gradle打jar包并混淆的方法详解

发布时间:2020-09-12 23:48:10 来源:脚本之家 阅读:392 作者:jacpy 栏目:移动开发

本文主要介绍了Android studio利用gradle打jar包并混淆的方法,下面话不多说,来看看详细的介绍吧。

首先打jar包的配置很简单,使用jar的task,可以参考gradle官方文档,具体代码如下:

task buildJar(type: Jar, dependsOn: ['assembleRelease']) {
 destinationDir = file('build/outputs/jar/')
 appendix = ""
 baseName = ""
 version = SDK_VERSION
 // manifest信息
 def map = ['Version': SDK_VERSION,
  'Gradle': project.gradle.gradleVersion,
  'Vendor': '',
  'Date': new Date().getDateTimeString()
 ]
 manifest.attributes(map)
 from('build/intermediates/classes/release/')
 exclude('**/BuildConfig.class')
 exclude('**/BuildConfig\$*.class')
 exclude('**/R.class')
 exclude('**/R\$*.class')
 include('**/*.class')
}

但是发现上面打出来的jar包中的代码没有被混淆,于是加混淆。使用混淆的proguard.gradle.ProGuardTask task时,发现总是报如下错误:

java.io.IOException: The output jar [....jar] must be specified after an input jar, or it will be empty.

按照上面的提示,outjars是写在injars后面啊,it will be empty是提示哪里有问题?以为是proguard-rules.pro文件中的配置有问题,结果把文件清空,还是报上面的错误,错误原因不得而知了。在这个问题上耗了很长时间,最终还是没有解决。
突然看到上面的assembleRelease task想起了平常打APK时,会使用到这个task,打出来的APK会自动混淆。

那这个assembleRelease task就会依赖处理混淆的task,于是查看了输出日志:

To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html.
Observed package id 'system-images;android-22;google_apis;x86' in inconsistent location 'android-sdk-windows-studio\system-images\addon-google_apis-google-22
\x86' (Expected 'android-sdk-windows-studio\system-images\android-22\google_apis\x86')
Observed package id 'system-images;android-22;google_apis;x86' in inconsistent location 'android-sdk-windows-studio\system-images\addon-google_apis-google-22
\x86' (Expected 'android-sdk-windows-studio\system-images\android-22\google_apis\x86')
Incremental java compilation is an incubating feature.  
:sdk:preBuild UP-TO-DATE                  
:sdk:extractProguardFiles
:sdk:preReleaseBuild
:sdk:checkReleaseManifest
:sdk:preDebugAndroidTestBuild UP-TO-DATE
:sdk:preDebugBuild UP-TO-DATE
:sdk:preDebugUnitTestBuild UP-TO-DATE
:sdk:preReleaseUnitTestBuild UP-TO-DATE
:sdk:prepareComAndroidSupportAnimatedVectorDrawable2510Library UP-TO-DATE 
:sdk:prepareComAndroidSupportAppcompatV72510Library UP-TO-DATE 
:sdk:prepareComAndroidSupportSupportCompat2510Library UP-TO-DATE
:sdk:prepareComAndroidSupportSupportCoreUi2510Library UP-TO-DATE
:sdk:prepareComAndroidSupportSupportCoreUtils2510Library UP-TO-DATE
:sdk:prepareComAndroidSupportSupportFragment2510Library UP-TO-DATE 
:sdk:prepareComAndroidSupportSupportMediaCompat2510Library UP-TO-DATE 
:sdk:prepareComAndroidSupportSupportV42510Library UP-TO-DATE
:sdk:prepareComAndroidSupportSupportVectorDrawable2510Library UP-TO-DATE
:sdk:prepareReleaseDependencies
:sdk:compileReleaseAidl UP-TO-DATE 
:sdk:compileReleaseNdk UP-TO-DATE
:sdk:compileLint UP-TO-DATE
:sdk:copyReleaseLint UP-TO-DATE
:sdk:compileReleaseRenderscript UP-TO-DATE
:sdk:generateReleaseBuildConfig   
:sdk:generateReleaseResValues UP-TO-DATE
:sdk:generateReleaseResources UP-TO-DATE
:sdk:mergeReleaseResources UP-TO-DATE 
:sdk:processReleaseManifest UP-TO-DATE
:sdk:processReleaseResources UP-TO-DATE 
:sdk:generateReleaseSources
:sdk:incrementalReleaseJavaCompilationSafeguard   
:sdk:compileReleaseJavaWithJavac   
:sdk:compileReleaseJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
注: 某些输入文件使用或覆盖了已过时的 API。                
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。               
:sdk:extractReleaseAnnotations   
:sdk:mergeReleaseShaders UP-TO-DATE 
:sdk:compileReleaseShaders UP-TO-DATE
:sdk:generateReleaseAssets UP-TO-DATE
:sdk:mergeReleaseAssets UP-TO-DATE
:sdk:mergeReleaseProguardFiles UP-TO-DATE
:sdk:packageReleaseRenderscript UP-TO-DATE
:sdk:packageReleaseResources UP-TO-DATE 
:sdk:processReleaseJavaRes UP-TO-DATE
:sdk:transformResourcesWithMergeJavaResForRelease UP-TO-DATE
:sdk:transformClassesAndResourcesWithProguardForRelease   
ProGuard, version 5.2.1
Reading input...       
Reading program directory [sdk\build\intermediates\classes\release] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-media-compat\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-compat\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-fragment\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-core-ui\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\animated-vector-drawable\25.1.0\jars\classes.jar] (filte
red)
Reading library jar [android-sdk-windows-studio\extras\android\m2repository\com\android\support\support-annotations\25.1.0\support-annotations-25.1.0.jar] (f
iltered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-v4\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-vector-drawable\25.1.0\jars\classes.jar] (filter
ed)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\appcompat-v7\25.1.0\jars\classes.jar] (filtered)
Reading library jar [sdk\build\intermediates\exploded-aar\com.android.support\support-core-utils\25.1.0\jars\classes.jar] (filtered)
Reading library jar [\android-sdk-windows-studio\platforms\android-25\android.jar]
Reading library jar [\android-sdk-windows-studio\platforms\android-25\optional\org.apache.http.legacy.jar]
Note: duplicate definition of library class [android.net.http.HttpResponseCache]
Note: duplicate definition of library class [android.net.http.SslCertificate$DName]
Note: duplicate definition of library class [android.net.http.SslError]
Note: duplicate definition of library class [android.net.http.SslCertificate]
Note: duplicate definition of library class [org.apache.http.params.CoreConnectionPNames]
Note: duplicate definition of library class [org.apache.http.params.HttpConnectionParams]
Note: duplicate definition of library class [org.apache.http.params.HttpParams]
Note: duplicate definition of library class [org.apache.http.conn.scheme.SocketFactory]
Note: duplicate definition of library class [org.apache.http.conn.scheme.LayeredSocketFactory]
Note: duplicate definition of library class [org.apache.http.conn.scheme.HostNameResolver]
Note: duplicate definition of library class [org.apache.http.conn.ConnectTimeoutException]
Note: there were 11 duplicate class definitions.
 (http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass)
Initializing...
Note: you're ignoring all warnings!     
Ignoring unused library classes...     
 Original number of library classes: 5857
 Final number of library classes: 383
Printing kept classes, fields, and methods...
Shrinking...
Printing usage to [sdk\build\outputs\mapping\release\usage.txt]...
Removing unused program classes and class elements...   
 Original number of program classes: 52    
 Final number of program classes: 48
Obfuscating...
Printing mapping to [sdk\build\outputs\mapping\release\mapping.txt]...
Writing output...
Preparing output jar [sdk\build\intermediates\transforms\proguard\release\jars\3\3\main.jar]
 Copying resources from program directory [sdk\build\intermediates\classes\release] (filtered)
Printing classes to [sdk\build\outputs\mapping\release\dump.txt]...
:sdk:transformClassesAndResourcesWithSyncLibJarsForRelease   
:sdk:mergeReleaseJniLibFolders UP-TO-DATE
:sdk:transformNative_libsWithMergeJniLibsForRelease UP-TO-DATE 
:sdk:transformNative_libsWithSyncJniLibsForRelease UP-TO-DATE
:sdk:bundleRelease   
:sdk:compileReleaseSources
:sdk:assembleRelease
:sdk:buildJar   
  
BUILD SUCCESSFUL.

好家伙,被我发现了。其中从:sdk:transformClassesAndResourcesWithProguardForRelease这个task就可以看到,下面一堆日志是关于ProGuard混淆工具的,最后可以看到:

Preparing output jar [sdk\build\intermediates\transforms\proguard\release\jars\3\3\main.jar]

这个main.jar就是混淆后的jar包,只不过这个jar还包含了R类和BuildConfig类的信息,所以将这个信息过滤掉就可以。但问题也来了,jar包都已经打好了,怎么配置混淆?解决方法是只要把jar包当输入就行了,最终配置如下:

task buildJar(type: Jar, dependsOn: ['assembleRelease']) {
 destinationDir = file('build/outputs/jar/')
 appendix = "" // SDK的后缀名称
 baseName = "" // SDK名称
 version = SDK_VERSION // 这个常量是在gradle.properties中配置的
 // manifest信息
 def map = ['Version': SDK_VERSION,
  'Gradle': project.gradle.gradleVersion,
  'Vendor': '',
  'Date': new Date().getDateTimeString()
 ]
 manifest.attributes(map)
 from(project.zipTree('build/intermediates/transforms/proguard/release/jars/3/3/main.jar'))
 exclude('**/BuildConfig.class')
 exclude('**/BuildConfig\$*.class')
 exclude('**/R.class')
 exclude('**/R\$*.class')
 include('**/*.class')
}

总结

OK,任务搞定。以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI