close
当前位置: 物联网在线 > 技术文库 > android >

android studio 使用gradle打jar包并混淆

昨天准备把写好的代码使用gradle打jar包出来,并打算加混淆。打jar包容易,结果在混淆上走了弯路。

首先打jar包的配置很简单,使用jar的task,具体代码如下:

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. (#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.
(责任编辑:ioter)

用户喜欢...

在 Airbnb 使用机器学习预测房源的价格

作者:Robert Chang 位于希腊爱琴海伊莫洛维里的一个 Airbnb 民宿的美好风景 简介 数据产品一直是 Airbnb 服务的重要组成...


如何使用可编程逻辑为按钮输入消抖

可编程逻辑具有传统分立 IC 无法提供的灵活性。 借助现成的开发工具,可轻松使用现场可编程门阵列和复杂可编程逻辑器件创建应用特定型功能。 按钮开关输入消抖便是此类功能的一个例子...


使用 LoRa 进行低速率、长距离物联网应用开发

设计人员可以使用各种各样的无线技术将产品连接到物联网 (IoT)。每种技术适用于不同的应用,需要设计人员仔细考虑作用距离和数据速率、成本、功耗、体积和外形等因素。 本文将介绍 LoR...


这款智能手表心率检测超强 可当做心电图机使用

Cronovo智能手表特别强调心率追踪功能,它安装了Android系统,可以成为个人健身教练,拥有先进的感知追踪系统。Cr...


摆脱APP收入被Android逆袭的命运?苹果宣布App Store支持微信支付

APP营收即将被Android超越的苹果,和移动支付市场份额即将追上支付宝的腾讯,终于走到了一起...IT经理网...


人肉推荐才是王道:Google Play独立手游节发布Android游戏TOP20大名单

Google Play独立游戏节是大数据时代通过人工推荐来提高优秀APP曝光率和可视性的另外一个尝试。IT经理网...


Quora是如何使用机器学习的?

作者:Nikhil Dandekar 2015年,公司的工程副总裁Xavier Amatriain,关于如何在Quora上使用机器学习给了一个很好的答案。从那...


机器学习算法的基本知识(使用Python和R代码)

本篇文章是原文的译文,然后自己对其中做了一些修改和添加内容(随机森林和降维算法)。文章简洁地介绍了机器学习的主要算法和一些伪代码,对于初学者有很大帮助,是一篇不错的总结...


如何使用TensorFlow构建、训练和改进循环神经网络

作者:机器之心编译 来自 Silicon Valley Data Science 公司的研究人员为我们展示了循环神经网络(RNN)探索时间序列和开发语...


Seeed Studio Wio Tracker让你轻松创建基于GPS的IoT项目

Seeed Studio的Wio Tracker这款无线输入输出 (Wio) Tracker为开源网关,通过跟踪几乎所有移动的目标并以无线方式上传数据,...