当前位置: 首页 > 编程笔记 >

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

百里业
2023-03-14
本文向大家介绍Android studio利用gradle打jar包并混淆的方法详解,包括了Android studio利用gradle打jar包并混淆的方法详解的使用技巧和注意事项,需要的朋友参考一下

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

 类似资料:
  • 混淆打包 在项目的主工程目录的proguard-project.txt配置中加入如下设置: V3.2.3及后续版本: -dontwarn solid.ren.skinlibrary.** -keep class solid.ren.skinlibrary.** { *; } -keep interface solid.ren.skinlibrary.** { *; } -keep public c

  • 本文向大家介绍Jar打包用法详解,包括了Jar打包用法详解的使用技巧和注意事项,需要的朋友参考一下 本文较为详细的分析了Jar打包的用法。分享给大家供大家参考。具体分析如下: jar是标准的java打包命令,位于JAVA_HOME/bin/目录下面。主要功能是将多个文件打包成一个单独的jar文件。 创建jar文件 更新jar文件 解压jar文件 显示Jar包结构 添加索引到jar文件   其中[]

  • 本文向大家介绍AndroidStduio3.0 使用gradle将module打包jar文件的方法,包括了AndroidStduio3.0 使用gradle将module打包jar文件的方法的使用技巧和注意事项,需要的朋友参考一下 AndroidStduio3.0使用gradle将module打包jar文件,首先需要安装gradle。 打开控制台输入      open -e .bash_prof

  • 我尝试在android studio中使用Proguard,但Proguard似乎没有混淆类名,例如,我的应用程序结构和配置: 和配置 但当我尝试在应用程序中触发异常时: ADB控制台中列出了异常: 只有方法被混淆了,MainActivity.class没有

  • 而RNDecryptor的addData方法如下: 我不明白这一行实际上是要做什么,它对加密流的每一个块都调用: 假设我有一个1000字节的块,而HMACLength是32。 提前道谢。

  • 但如果我用: 如果需要,我很乐意提供整个SSCE,但目前,为了让事情更简短,下面是TableModel的getValueAt()和setValueAt()方法: 数据只是Person类型的arraylist(一个具有明显变量(String)fName、(String)sName和(LocalDate)DOB的类) 任何帮助都将不胜感激!