歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 使用 IntelliJ IDEA 開發 Android 應用程序時配置 Allatori 進行代碼混淆

使用 IntelliJ IDEA 開發 Android 應用程序時配置 Allatori 進行代碼混淆

日期:2017/3/1 9:20:22   编辑:Linux編程

IntelliJ IDEA 提供了非常強大的 Android 開發支持,就連 Google 官方推薦的 Android Studio 其實也是 IntelliJ IDEA 的一個 Android 開發專用版。因為 Android 程序發布時采用 APK 文件封裝格式,其內部仍然是虛擬機字節碼,是可以通過諸如 dex2jar、jd 等工具進行反編譯的,所以進行產品發布前都要經過字節碼混淆,以最大限度地保護軟件知識產權。而 Google 官方推薦的 ProGuard 混淆器的混淆效果又不盡如人意,因此各類專業的混淆器並應運而生,這裡面 Allatori 便是佼佼者。Allatori 是商業混淆器軟件,混淆強度非常高,但其最新版官方網站上給出的與 Android Studio 的集成方式,采用的是 gradle 構建工具配置模式,經過實際測試並不成功。而 Allatori 自己的文檔中描述了如何與 Ant 構建工具進行配合,因此還是考慮在 IntelliJ IDEA 中實現 Allatori 與 Android 開發的自動化配合問題。

首先在 IDEA 的 Android Module 所在硬盤目錄內創建一個名為 allatori 的子目錄,將 Allatori 自己的 jar 文件都復制到這個子目錄下。然後在 IDEA 的 Android Module 所在目錄內創建兩個 XML 文件,一個是 Allatori 自己的混淆配置文件,命名為 config-allatori.xml;另一個是用於 Ant 構建的配置文件,命名為 build-allatori.xml。首先看 config-allatori.xml 文件的內容,基本可以作為一個模板:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<jars>
<dir in="${out.classes.absolute.dir}" out="${out.classes.absolute.dir}-obfuscated"/>
</jars>

<classpath>
<jar name="libs/netty/netty-all-4.0.19.Final.jar"/>
</classpath>

<keep-names>
<class template="public class * instanceof android.app.Activity">
<method template="public void *(android.view.View)"/>
</class>
<class template="public class * instanceof android.app.Application"/>
<class template="public class * instanceof android.app.Service"/>
<class template="public class * instanceof android.view.View">
<method template="public void set*(**)"/>
</class>
<class template="public class * instanceof android.content.BroadcastReceiver"/>
<class template="public class * instanceof android.content.ContentProvider"/>
<class template="public class * instanceof android.app.backup.BackupAgentHelper"/>
<class template="public class * instanceof android.preference.Preference"/>
<class template="public class com.android.vending.licensing.ILicensingService"/>
<class template="public class com.google.android.vending.licensing.ILicensingService"/>
<class template="class * implements android.os.Parcelable">
<field template="public static final android.os.Parcelable$Creator *"/>
</class>

<class template="class io.netty.*">
<field access="private+"/>
<method template="private+ *(**)"/>
</class>
</keep-names>

<property name="log-file" value="log.xml"/>
</config>

這裡要注意第 4 行,${out.classes.absolute.dir} 以及 ${out.classes.absolute.dir}-obfuscated 都是在 build-allatori.xml 中自定義的變量,用來表示 IDEA 編譯 Android Module 源代碼生成的 java class 類文件(字節碼)所在目錄以及類文件被混淆後保存的目錄。第 7 至 9 行表示 Android Module 中引用的第三方庫所在類路徑,有多個第三方庫 jar 文件就要有對應的多行類路徑說明。第 30 至 33 行表示第三方庫中的類不應被混淆,通過指定包名前綴及通配符來限定,如果有多個第三方類庫,就要有多個類似這樣的聲明。

下面再來看 build-allatori.xml 文件的內容,也可以作為一個模板:

<?xml version="1.0" encoding="UTF-8"?>
<project name="你的module名字-allatori" default="你的module名字-obfuscated">

<property name="out.classes.absolute.dir" value="/你的project絕對路徑/out/production/你的module名字"/>
<property name="out.jar.absolute.dir" value="/你的project絕對路徑/out/artifacts/你的module名字"/>

<target name="你的module名字-obfuscated">
<taskdef name="allatori" classname="com.allatori.ant.ObfuscatorTask" classpath="allatori/allatori.jar"/>
<allatori config="config-allatori.xml"/>
<delete dir="${out.classes.absolute.dir}"/>
<move todir="${out.classes.absolute.dir}">
<fileset dir="${out.classes.absolute.dir}-obfuscated"/>
</move>
</target>

<target name="你的module名字-clean">
<delete dir="${out.classes.absolute.dir}"/>
<delete dir="${out.jar.absolute.dir}"/>
<delete dir="${out.classes.absolute.dir}-obfuscated"/>
</target>
</project>

你需要將 build-allatori.xml 中“你的module名字”字樣部分全都改為你的 Android Module 的實際名字,將“你的project絕對路徑”字樣部分全都改為你的 project 的絕對路徑。下面將 build-allatori.xml 加入進 IDEA 的 Ant Build 中,如下圖所示:

點擊 IDEA 右上部分 Ant Build 那個按鈕,在彈出的 Dock 小窗口中點擊“+”按鈕,然後選擇 build-allatori.xml 文件即可將其加入到 IDEA 的 Ant Build 系統中。

然後在 IDEA 的 Project Stucture 配置中,創建好 Android Module 的 Artifacts,在其 Artifacts 的具體屬性配置中,需要指定其 Pre-processing 使用 build-allatori.xml 中定義的名為“你的module名字-obfuscated”的 target,如下圖所示:

注意紅色區域裡面,要把 Run Ant target 前面勾選中,然後點擊其右邊的“...”按鈕,會出現選擇窗口,你需要選擇 build-allatori.xml 中定義的名為“你的module名字-obfuscated”的 target。

之後,點擊 IDEA 菜單“Build”->“Build Artifacts...”菜單項並選擇“Build”命令來進行 Android APK 的編譯構建,然後記住要再次執行 Build Artifacts 的 Build(千萬不能是 Rebuild!)命令,實現混淆並重新構建 APK。即,通過兩次 Build Artifacts 的 Build 命令來實現編譯混淆構建 APK。其原理是,第一次 Build 時,在編譯生成 class 類文件後,會自動調用 Allatori 對 class 類文件進行混淆,但第一次 Build 生成 APK 仍然是基於未混淆的類文件的。所以第二次 Build 時,因為已編譯並被混淆的類文件已存在,不用再重新編譯,IDEA 會直接利用已混淆的類文件構建 APK,從而實現混淆構建 APK 的目的。

另外注意,如果需要清理 Android Module 已生成的類文件及 APK,可以通過 IDEA 的 Ant Build 執行 build-allatori.xml 中定義的“你的module名字-clean” target,就可以徹底清除已生成的類文件目錄及 APK 所在目錄。

使用IntelliJ IDEA 13搭建Android集成開發環境圖文教程 http://www.linuxidc.com/Linux/2015-09/123416.htm

IntelliJ IDEA 12 創建Web項目圖文詳細教程 http://www.linuxidc.com/Linux/2013-05/84213.htm

用IntelliJ IDEA開發Android程序圖文教程 http://www.linuxidc.com/Linux/2013-03/81471.htm

IntelliJ IDEA 12開發haXe NME應用配置指南 http://www.linuxidc.com/Linux/2013-01/77227.htm

IntelliJ IDEA運行Play Framework的test mode http://www.linuxidc.com/Linux/2013-07/87694.htm

Ubuntu 13.04 安裝IntelliJ IDEA 12 http://www.linuxidc.com/Linux/2013-11/93014.htm

IntelliJ IDEA 12創建Maven管理的Java Web項目(圖解) http://www.linuxidc.com/Linux/2014-04/99687p2.htm

IntelliJ IDEA 常用快捷鍵列表及技巧大全 http://www.linuxidc.com/Linux/2015-04/116398.htm

IntelliJ IDEA 的詳細介紹:請點這裡
IntelliJ IDEA 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved