网站建设资讯

NEWS

网站建设资讯

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

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

创新互联主营南溪网站建设的网络公司,主营网站建设方案,App定制开发,南溪h5小程序定制开发搭建,南溪网站营销推广欢迎南溪等地区企业咨询

首先打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开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对创新互联的支持。


分享标题:Androidstudio利用gradle打jar包并混淆的方法详解
标题来源:http://cdweb.net/article/jjcgdd.html