Android Apk资源文件压缩学习

来源:互联网 发布:湖南云箭集团知乎 编辑:程序博客网 时间:2024/06/02 20:26

最近学习了一个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'
我的配置结果:

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}
在app中的build.gradle加入:

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"    }}
配置完成后:

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'}
然后在Android Studio自带的命令行中输入gradlew resguard,然后就可以得到相应的安装包
注意:没有签名的安装包会出现‘安装包解析错误’的提示。原因未知
2,直接下载压缩工具:点击下载 tool_output文件夹下就是压缩工具

然后用记事本打开buildApk.bat文件,

set jdkpath=D:\Program Files\Java\jdk1.7.0_79\bin\java.exeset storepath=release.keystoreset storepass=testresset keypass=testresset alias=testresset 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文件,就可以生成压缩后的安装包
注意:没有签名的安装包会出现‘安装包解析错误’的提示。原因未知



0 0