Android Apk資原始檔壓縮學習
阿新 • • 發佈:2018-12-24
最近學習了一個apk的資源id進行壓縮的工具。
原文地址:https://github.com/shwenzhang/AndResGuard
資源壓縮的原理:安裝包立減1M--微信Android資源混淆打包工具
這兩篇文章對資源壓縮的用法和原理講解的比較詳細。這裡就只記錄了我自己用的時候的一些心得。
有兩種方式可以使用。
1,直接在Android Studio中配置gradle。
在專案的build.gradle中加入:
classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.1.9'
我的配置結果:
在app中的build.gradle加入:buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.0.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.1.9' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
配置完成後:apply plugin: 'AndResGuard' andResGuard { mappingFile = null use7zip = true useSign = true keepRoot = false whiteList = [ //for your icon "R.drawable.icon", //for fabric "R.string.com.crashlytics.*", //for umeng update "R.string.umeng*", "R.string.UM*", "R.string.tb_*", "R.layout.umeng*", "R.layout.tb_*", "R.drawable.umeng*", "R.drawable.tb_*", "R.anim.umeng*", "R.color.umeng*", "R.color.tb_*", "R.style.*UM*", "R.style.umeng*", "R.id.umeng*" //umeng share for sina "R.drawable.sina*" ] compressFilePattern = [ "*.png", "*.jpg", "*.jpeg", "*.gif", "resources.arsc" ] sevenzip { artifact = 'com.tencent.mm:SevenZip:1.1.9' //path = "/usr/local/bin/7za" } }
然後在Android Studio自帶的命令列中輸入gradlew resguard,然後就可以得到相應的安裝包apply plugin: 'com.android.application' apply plugin: 'AndResGuard' android { compileSdkVersion 23 buildToolsVersion "23.0.3" signingConfigs { release { try { storeFile file("../keystore/butterknifedemo.jks") storePassword "123456" keyAlias "asdfg" keyPassword "123456" } catch (ex) { throw new InvalidUserDataException(ex.toString()) } } } defaultConfig { applicationId "com.tongyan.butterknifedemo" minSdkVersion 14 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } } } andResGuard { mappingFile = null use7zip = true useSign = true keepRoot = false whiteList = [//因為我的專案只是一個簡單的demo就沒有新增白名單 ] compressFilePattern = [ "*.png", "*.jpg", "*.jpeg", "*.gif", "resources.arsc" ] sevenzip { artifact = 'com.tencent.mm:SevenZip:1.1.9' //path = "/usr/local/bin/7za" } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:design:23.4.0' }
注意:沒有簽名的安裝包會出現‘安裝包解析錯誤’的提示。原因未知
2,直接下載壓縮工具: 點選下載 tool_output資料夾下就是壓縮工具
然後用記事本開啟buildApk.bat檔案,
set jdkpath=D:\Program Files\Java\jdk1.7.0_79\bin\java.exe
set storepath=release.keystore
set storepass=testres
set keypass=testres
set alias=testres
set zipalign=D:\soft\dev\android\sdk\build-tools\23.0.2\zipalign.exe
"%jdkpath%" -jar AndResGuard-cli-1.1.9.jar input.apk -config config.xml -out outapk -signature "%storepath%" "%storepass%" "%keypass%" "%alias%" -zipalign "%zipalign%"
pause
把幾個set引數都修改成我們自己的引數,把input.apk修改成直接的apk檔名
修改config.xml檔案替換裡面的包名為應用的包名。
<?xml version="1.0" encoding="UTF-8"?>
<resproguard>
<!--defaut property to set -->
<issue id="property">
<!--whether use 7zip to repackage the signed apk, you must install the 7z command line version in window -->
<!--sudo apt-get install p7zip-full in linux -->
<!--and you must write the sign data fist, and i found that if we use linux, we can get a better result -->
<seventzip value="false"/>
<!--the sign data file name in your apk, default must be META-INF-->
<!--generally, you do not need to change it if you dont change the meta file name in your apk-->
<metaname value="META-INF"/>
<!--if keep root, res/drawable will be kept, it won't be changed to such as r/s-->
<keeproot value="false"/>
</issue>
<!--whitelist, some resource id you can not proguard, such as getIdentifier-->
<!--isactive, whether to use whitelist, you can set false to close it simply-->
<issue id="whitelist" isactive="false">
<!--you must write the full package name, such as com.tencent.mm.R -->
<!--for some reason, we should keep our icon better-->
<!--and it support *, ?, such as com.tencent.mm.R.drawable.emoji_*, com.tencent.mm.R.drawable.emoji_?-->
</issue>
<!--keepmapping, sometimes if we need to support incremental upgrade, we should keep the old mapping-->
<!--isactive, whether to use keepmapping, you can set false to close it simply-->
<!--if you use -mapping to set keepmapping property in cammand line, these setting will be overlayed-->
<!-- <issue id="keepmapping" isactive="false"> -->
<!--the old mapping path, in window use \, in linux use /, and the default path is the running location-->
<!--<path value="{your_mapping_path}"/> -->
<!--</issue> -->
<!--compress, if you want to compress the file, the name is relative path, such as resources.arsc, res/drawable-hdpi/welcome.png-->
<!--what can you compress? generally, if your resources.arsc less than 1m, you can compress it. and i think compress .png, .jpg is ok-->
<!--isactive, whether to use compress, you can set false to close it simply-->
<issue id="compress" isactive="false">
<!--you must use / separation, and it support *, ?, such as *.png, *.jpg, res/drawable-hdpi/welcome_?.png-->
<path value="*.png"/>
<path value="*.jpg"/>
<path value="*.jpeg"/>
<path value="*.gif"/>
<path value="resources.arsc"/>
</issue>
<!--keepmapping, sometimes if we need to support incremental upgrade, we should keep the old mapping-->
<!--isactive, whether to use keepmapping, you can set false to close it simply-->
<!--if you use -mapping to set keepmapping property in cammand line, these setting will be overlayed-->
<issue id="keepmapping" isactive="false">
<!--the old mapping path, in window use \, in linux use /, and the default path is the running location-->
<path value="{your_mapping_path}"/>
</issue>
<!--sign, if you want to sign the apk, and if you want to use 7zip, you must fill in the following data-->
<!--isactive, whether to use sign, you can set false to close it simply-->
<!--if you use -signature to set sign property in cammand line, these setting will be overlayed-->
<issue id="sign" isactive="true">
<!--the signature file path, in window use \, in linux use /, and the default path is the running location-->
<path value="D:/android_studio_work/ButterKnifeDemo/keystore/butterknifedemo.jks"/>
<!--storepass-->
<storepass value="123456"/>
<!--keypass-->
<keypass value="123456"/>
<!--alias-->
<alias value="tongyan"/>
</issue>
</resproguard>
最後執行buildApk.bat檔案,就可以生成壓縮後的安裝包
注意 :沒有簽名的安裝包會出現‘安裝包解析錯誤’的提示。原因未知