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)

用户喜欢...

使用光学传感器解决传感挑战

能够适应不断变化的应用要求的测量系统是首选,因为不断增长的成本压力需要较长的系统寿命和功能灵活性。开发此类系统的最有效方法是使用像PXI这样的软件定义的模块化架构。可以混合和...


使用DLP技术创建高分辨率自适应大灯

传统上,典型的汽车前照灯光束仅照亮车辆前方的物体,以提高驾驶员在低光照和恶劣天气条件下的可视性。低光束在车辆前方短距离照亮道路,而远光灯具有更长的范围和更宽的角度。这种...


使用原型套件简化能量收集设计

在使用设计之前,根据其环境条件,能量收集电路能够提供多少功率可能并不明显。 这会影响能量收集源和能量存储所需的尺寸和质量。 为了使设计进一步复杂化,诸如能量存储泄漏,稳压器...


使用Xilinx系统生成器实现简单的DDS

在本文中,我们将讨论使用Xilinx System Generator实现简单的直接数字频率合成器(DDS)。 System Generator是一个功能强大的工具,它将Xilinx FPGA设计过程与MATLAB的Simulink集成,后者使用高级描述轻松实...


如何为RF应用选择,使用和维护同轴连接器

无线电频率(RF)电路在有线和无线通信中正在激增,包括用于物联网(IoT)的Wi-Fi和各种无线技术。这些高频信号需要在系统,电路组件和子组件之间进行分配,而损耗或寄生辐射损耗最小。...


为物联网程序存储器应用选择和使用正确的闪存技术

随着IoT节点变得越来越强大,处理需求可能会增加到需要完整的嵌入式操作系统(OS)(如Linux)才能正确管理所有运行任务的复杂性的水平。尽管将此代码存储在闪存microSDHC卡上很方便,但大...


使用RS-485和电流回路接口实现可靠的工业连接

尽管蓝牙,以太网和其他连接选项广泛普及并支持高数据速率,但工业应用设计人员仍然需要确保以最低的成本获得最可靠的连接。他们还必须在安装的连接选项基础范围内工作。 由于这些原...


使用Zigbee和线程快速构建网状连接的照明应用

对于家庭,建筑和工业自动化应用,连接的照明系统非常方便,可以帮助节省能源。基于802.15.4的网状网络提供低功耗操作,扩展范围和可用性的有效组合,但实施起来却很复杂。 为了减少这种...


使用全可编程SoC简化您的增强现实系统

研究表明,我们人类与世界的视觉互动,因为我们处理视觉图像比以其他形式呈现的信息快许多倍,例如书面文本。 增强现实(AR)与其虚拟现实(VR)表亲一样,使我们能够体验对周围环境的...


使用Amazon FreeRTOS将设计快速安全地连接到云端

传统的嵌入式系统开发人员通常在断开设备的裸机级别工作,但物联网需要系统连接到Internet。这要求嵌入式系统开发人员学习如何连接他们的设备并使用更高级别的操作系统,例如实时操作系...