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

作者:jacpy 时间:2021-07-07 06:22:57 

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

来源:http://www.jacpy.com/2017/02/28/android-studio-gradle-make-jar-and-proguard.html

标签:Androidstudio,gradle,jar包混淆
0
投稿

猜你喜欢

  • Android App仿QQ制作Material Design风格沉浸式状态栏

    2021-06-07 01:10:48
  • Springboot实现给前端返回一个tree结构方法

    2022-04-02 23:37:07
  • jackson反序列化时如何忽略不需要的字段

    2021-09-26 20:36:20
  • Linux环境g++编译GDAL动态库操作方法

    2022-02-23 09:41:45
  • mybatis-plus QueryWrapper 添加limit方式

    2022-12-09 02:08:11
  • java调用python脚本引入第三方库失败的实现

    2021-10-16 02:36:25
  • 如何搭建新的WPF项目框架

    2023-09-28 08:18:05
  • C#借助OpenCvSharp读取摄像头并显示的实现示例

    2022-12-22 05:49:51
  • SpringBoot整合Web开发之Json数据返回的实现

    2023-04-27 05:06:51
  • C#学习笔记——基本语法

    2022-10-13 13:39:54
  • Java中BigDecimal的加减乘除、比较大小与使用注意事项

    2022-02-17 21:24:09
  • Java下载文件时文件名乱码问题解决办法

    2023-08-23 17:37:03
  • Netty启动流程注册多路复用源码解析

    2021-12-02 16:51:35
  • java数据结构与算法之双向循环队列的数组实现方法

    2022-08-12 23:24:44
  • Android RecyclerView详解之实现 ListView GridView瀑布流效果

    2023-11-26 10:09:00
  • Spring RestTemplate的使用与踩坑

    2022-01-12 21:43:14
  • Spring自动装配Bean实现过程详解

    2023-10-31 18:35:21
  • C#实现对AES加密和解密的方法

    2021-07-27 16:34:24
  • Java使用synchronized修饰方法来同步线程的实例演示

    2022-11-29 18:33:29
  • Java HtmlParse提取标签中的值操作

    2021-10-07 04:43:10
  • asp之家 软件编程 m.aspxhome.com