[Android]在windows下使用facebook的apk优化工具redex

Android应用优化,有一种打包后的优化,或叫dex优化,例如可以去除一些不必要的信息,从而实现对apk的裁剪,又例如对dex文件进行排序重打包,优化app冷启动速度。这些方法功能最全又最好的就是facebook的redex。

网上都说redex只能在mac或者ubuntu下使用,其实官网文档显示在window64下也是可以使用,只是可能步骤稍微复杂一点,主要是编译redex的源代码。在哇indow64下编译redex,可以使用Visual Studio,但是会遇到很多错误,需要自己修改,也可以单独下载vcpkg进行编译,但是由于国内网络问题,其安装插件时可能需要翻墙。所以这里选用最容易的一种,使用MSYS2工具来编译redex。

1. 安装配置MSYS2

1)在这里下载安装MSYS2:MSYS2,注意按照相关步骤按照配置。
2) 点击msys2.exe或者mingw64.exe打开shell,然后输入命令安装插件:

pacman -Syuu && pacman -Sy make mingw-w64-x86_64-boost mingw-w64-x86_64-cmake mingw-w64-x86_64-gcc mingw-w64-x86_64-jsoncpp mingw-w64-x86_64-make

3)安装git

pacman -S git

2. 下载和编译redex

1)下载redex

git clone https://github.com/facebook/redex.git
cd redex

2)编译redex

# Assumes you want to use Git under MSYS. Else skip to below.
git clone https://github.com/facebook/redex.git
cd redex
# Assumes you are in the redex directory
mkdir build-cmake
cd build-cmake
cmake -G "MSYS Makefiles" ..
make -j4

注意,如果出现out of memory的错误,试着将进程数减少。

使用如下命令确认redex编译OK:

# In the MingW64 shell:
./redex-all.exe --show-passes
# Or in a standard Windows command prompt in the same directory
redex-all.exe --show-passes

3. 简单使用redex

在window下使用redex,要么将生成的redex-all.exe文件拷贝到redex.py同一目录下,要么指定–redex-binary参数:

python redex.py --redex-binary build-cmake/redex-all.exe -o after.apk /e/Learning/Android/code/AndroidAdvanceWithGeektime/Chapter17/SocketHookSample/release/SocketHookSample-release.apk

特别提醒:由于目前redex只支持dex-version:35,因此minSdkVersion不能太高,否则会有:Bad dex magic dex 038 for support_dex_version 35 的错误。

4. redex的使用例子

redex的具体功能由各种解析器(pass)指定,命令./redex-all.exe --show-passes 可以将所有pass列举出来,而各pass的功能,可以参考pass doc,具体使用就是配置config文件,然后在上一步的命令中指定config文件,例如

redex.py -c default.config -o tmp/output.apk input.apk

1)包体积裁剪
stripdebuginfo.config:

{
  "redex" : {
    "passes" : [
      "StripDebugInfoPass"
    ]
  },
  "StripDebugInfoPass" : {
    "drop_all_dbg_info" : "0",
    "drop_local_variables" : "1",
    "drop_line_numbers" : "0",
    "drop_src_files" : "0",
    "use_whitelist" : "0",
    "cls_whitelist" : [],
    "method_whitelist" : [],
    "drop_prologue_end" : "1",
    "drop_epilogue_begin" : "1",
    "drop_all_dbg_info_if_empty" : "1"
  }

}

redex参考命令:

redex --sign -s ReDexSample/keystore/debug.keystore -a androiddebugkey -p android -c redex-test/stripdebuginfo.config -P ReDexSample/proguard-rules.pro -o redex-test/strip_output.apk ReDexSample/build/outputs/apk/debug/ReDexSample-debug.apk

2)分包优化:
interdex.config:

{
  "redex" : {
    "passes" : [
      "InterDexPass"
    ]
  },
  "InterDexPass" : {
    "minimize_cross_dex_refs": true,
    "minimize_cross_dex_refs_method_ref_weight": 100,
    "minimize_cross_dex_refs_field_ref_weight": 90,
    "minimize_cross_dex_refs_type_ref_weight": 100,
    "minimize_cross_dex_refs_string_ref_weight": 90
  },
  "string_sort_mode" : "class_order",
  "bytecode_sort_mode" : "class_order"
}

redex参考命令:

redex --sign -s ReDexSample/keystore/debug.keystore -a androiddebugkey -p android -c redex-test/interdex.config -P ReDexSample/proguard-rules.pro -o redex-test/interdex_output.apk ReDexSample/build/outputs/apk/debug/ReDexSample-debug.apk

注意,这上面是ubuntu下的命令,在window下使用,参考上面说明,改为类似python redex.py --redex-binary build-cmake/redex-all.exe

后续看到其它例子还会补充~


版权声明:本文为dingpwen原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。