1. 程式人生 > >微信開源工具AndResGuard做Android資源混淆

微信開源工具AndResGuard做Android資源混淆

前言

在Android開發過程中,為了防止自己的勞動成功被竊取或者受到不發分子的篡改,往往在釋出的時候會對程式碼做混淆,但是混淆只混淆了程式碼,資原始檔卻還是赤裸在其他人面前,而且檢視資原始檔還非常簡單,將.apk檔案更換成.zip檔案幾個res資料夾下清楚的看到相關的資原始檔,如下圖:

為了保護專案的資原始檔的可讀性,同時也為了減小整個apk包的大小,下面一起來通過微信的開源庫

AndResGuard進行資原始檔的混淆。
瞭解AndResGuard的原理請戳這裡

準備工作

需要準備以下幾個檔案:

點選下載最新的配置檔案,下面對以上檔案一一做介紹

  • jar
    AndResGuard-cli-1.1.16.jar下載

  • 簽名檔案test.jks
    可以通過Android studio或者eclipse建立;也可以使用keytool.exe建立,具體操作在android 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="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="com.lpf.argt.R.drawable.ic_launcher"/> <path value="com.lpf.argt.R.mipmap.ic_launcher"/> <path value="com.lpf.argt.R.string.app_name"/> <path value="com.lpf.argt.R.drawable.icon"/> <path value="com.lpf.argt.R.string.umeng*"/> <path value="com.lpf.argt.R.layout.umeng*"/> <path value="com.lpf.argt.R.drawable.umeng*"/> <path value="com.lpf.argt.R.anim.umeng*"/> <path value="com.lpf.argt.R.color.umeng*"/> <path value="com.lpf.argt.R.style.*UM*"/> <path value="com.lpf.argt.R.style.umeng*"/> <path value="com.lpf.argt.R.id.umeng*"/> </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="F:/DATA/code/build/test.jks"/> <!--storepass--> <storepass value="123456789"/> <!--keypass--> <keypass value="123456789"/> <!--alias--> <alias value="test"/> </issue> </resproguard>
  • 7-zip工具的安裝
    下載軟體,根據引導安裝即可

  • bat檔案
    新建一個txt資料夾,將如下指令拷貝進去,然後將.txt的字尾更換為.bat

    Java -jar AndResGuard-cli-1.1.16.jar test.apk -config config.xml -out release -zipalign F:/DATA/code/software/sdk/build-tools/23.0.2/zipalign -7zip "E:/Program Files/7-Zip/7z.exe" -signature F:/DATA/code/build/test.jks 123456789 123456789 test
    • F:/DATA/code/software/sdk/build-tools/23.0.2/zipalign為SDK自帶的工具,具體路徑根據自己的實際情況配置
    • E:/Program Files/7-Zip/7z.exe為前一步中安裝的7-zip的路徑
    • F:/DATA/code/build/test.jks為簽名檔案,後面緊跟的是2個密碼和alias
  • test.apk
    測試apk,這裡的apk為任意apk均可以,簽名過的apk必須是使用上面bat中配置的簽名檔案進行簽名的;同時混淆後的apk也是沒有問題的

開始混淆

  • 按照上面步驟中的操作將配置檔案及資原始檔配齊之後,雙擊執行test.bat檔案,出現以下日誌:

  • 同時在同目錄下會出現一個“release”的資料夾,其中包含了混淆後的資原始檔、原始檔及混淆檔案的對應關係、簽名後的apk和未簽名的apk,詳情如下:

  • 再次檢視apk的資原始檔,發現res資料夾已經變成了r,裡面的各個資源也全部更換成其他的別名。

  • 資原始碼都混淆,資源和配置檔案引用均無問題;為了測試包含第三方庫是否會有影響,引入了ButterKnife測試,apk能正常執行。

  • 混淆前與混淆後的大小對比

    • 資源混淆前:
      資源混淆前
    • 資源混淆後:
      資源混淆後

    空專案就壓縮了70KB-100KB的大小,相信在實際的專案開發中,資原始檔更多,壓縮率會更高

Android studio混淆配置

使用Android studio直接混淆的配置入口