欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-16 14:30:55
本文主要介绍了android studio利用gradle打jar包并混淆的方法,下面话不多说,来看看详细的介绍吧。 首先打jar包的配置很简单,使用jar的task,可...

本文主要介绍了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开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

上一篇:

下一篇: