Android Apk 7z压缩

简介

内容 主要介绍 AndResGuard 使用

AndResGuard介绍:

微信资源压缩打包,主要为了混淆资源ID长度(例如将res/drawable/welcome.png混淆为r/s/a.png),同时利用7z深度压缩,大大减少了安装包体积,同时也增加了逼格,提升了反破解难度。

详情参考

原理介绍

获取

从github上获取工具

下载地址

写法

写法一

使用命令行

eg:

1
java -jar AndResGuard-cli-1.1.0.jar input.apk -config config.xml -out outapk -zipalign  D:/android/android-sdks/build-tools/23.0.2/zipalign

可参考:github的例子,其中.bat为win下批处理命令,双击运行。

.sh为Linux或mac下执行的脚本,通过

1
./buildApk.sh

写法二

使用插件,同样的在github上也有说明,配置好之后在project目录下使用

1
gradlew generate

写法三

配置task任务
在project下目录的build.gradle中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
*gradlew compressReleaseApp
* 对包进行微信资源压缩
*/
task compressReleaseApp {
// 在现有release的版本上生成到compressed目录下
def guardJarFile = file('/AndResGuard/AndResGuard-cli-1.1.0.jar') //AndResGuard jar包
def guardConfigFile = file('config.xml') //config.xml配置文件
def originApkFile = file("xxx.apk") //需要压缩的apk
def outputDir = file("/build/AndResGuard/") //输出的位置
// 开始执行压缩命令
def proc = "java -jar ${guardJarFile} ${originApkFile} -config ${guardConfigFile} -out ${outputDir} -zipalign D:/android/android-sdks/build-tools/23.0.2/zipalign".execute();
proc.waitFor();
println "return code: ${ proc.exitValue()}" + ", stderr: ${proc.err.text}" + " stdout: ${proc.in.text}"
}

在cmd中执行:

1
gradlew compressReleaseApp

关于 config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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="true">
<!--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_?-->
<!--<path value="<your_package_name>.R.drawable.icon"/>-->
<!--<path value="<your_package_name>.R.string.com.crashlytics.*"/>-->
<!--<path value="<your_package_name>.R.string.umeng*"/>-->
<!--<path value="<your_package_name>.R.layout.umeng*"/>-->
<!--<path value="<your_package_name>.R.drawable.umeng*"/>-->
<!--<path value="<your_package_name>.R.anim.umeng*"/>-->
<!--<path value="<your_package_name>.R.color.umeng*"/>-->
<!--<path value="<your_package_name>.R.style.*UM*"/>-->
<!--<path value="<your_package_name>.R.style.umeng*"/>-->
<!--<path value="<your_package_name>.R.id.umeng*"/>-->
<!--<path value="<your_package_name>.R.string.UM*"/>-->
<!--<path value="<your_package_name>.R.string.tb_*"/>-->
<!--<path value="<your_package_name>.R.layout.tb_*"/>-->
<!--<path value="<your_package_name>.R.drawable.tb_*"/>-->
<!--<path value="<your_package_name>.R.color.tb_*"/>-->
</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>

<!--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="release.keystore"/>
<!--storepass-->
<storepass value="testres"/>
<!--keypass-->
<keypass value="testres"/>
<!--alias-->
<alias value="testres"/>
</issue>

</resproguard>

注:

在win下7Z的安装后如果没有7za.exe的话,可以把7z.exe复制一份改成7za.exe,并配置环境变量