Android中關於Proguard的一些記錄

來源:互聯網
上載者:User

Proguard 是Android tools包中提供用來對代碼進行壓縮,最佳化和進行混淆的工具,它能夠移除無用的代碼,對類名,方法名,欄位名進行重新命名,但不改變代碼的結構,從而達到對代碼混淆的作用。而這樣做的好處是我們可以得到一個更小的apk檔案和一份更難以被反編譯的代碼。

在項目中使用Proguard,必須在project.properties修改如下:

# This file is automatically generated by Android Tools.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!## This file must be checked in Version Control Systems.## To customize properties used by the Ant build system edit# "ant.properties", and override values to adapt the script to your# project structure.## To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txt...

Android本身已經提供了兩份關於proguard的設定檔,放在sdk目錄下面,如下:


所以,當然,我們也可以自己建立一個設定檔,放在指定的任意位置上,然後將改變這份設定檔就好,如下:

#proguard.config=${sdk.dir}/tools/proguard/proguard-android-optimize.txt:proguard-project.txtproguard.config=proguard-project.cfg

下面是Android 提供的 proguard-android.txt檔案,如下:

# This is a configuration file for ProGuard.# http://proguard.sourceforge.net/index.html#manual/usage.html-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-verbose# Optimization is turned off by default. Dex does not like code run# through the ProGuard optimize and preverify steps (and performs some# of these optimizations on its own).-dontoptimize-dontpreverify# Note that if you want to enable optimization, you cannot just# include optimization flags in your own project configuration file;# instead you will need to point to the# "proguard-android-optimize.txt" file instead of this one from your# project.properties file.-keepattributes *Annotation*-keep public class com.google.vending.licensing.ILicensingService-keep public class com.android.vending.licensing.ILicensingService# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native-keepclasseswithmembernames class * {    native ;}# keep setters in Views so that animations can still work.# see http://proguard.sourceforge.net/manual/examples.html#beans-keepclassmembers public class * extends android.view.View {   void set*(***);   *** get*();}# We want to keep methods in Activity that could be used in the XML attribute onClick-keepclassmembers class * extends android.app.Activity {   public void *(android.view.View);}# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations-keepclassmembers enum * {    public static **[] values();    public static ** valueOf(java.lang.String);}-keep class * implements android.os.Parcelable {  public static final android.os.Parcelable$Creator *;}-keepclassmembers class **.R$* {    public static ;}# The support library contains references to newer platform versions.# Don't warn about those in case this app is linking against an older# platform version.  We know about them, and they are safe.-dontwarn android.support.**

其實基本上上面提供的也是Android不建議進行混淆的代碼,所以我們的如果要對自己使用的類進行混淆,可以在這份文檔的基本上進行加工就可以。

前面說過,Proguard其實做的不只混淆,還有壓縮最佳化代碼(移除掉一些無用的代碼和重新命名類名,方法名,欄位名等),當進行過Proguard之後,會在項目的根目錄產生一個proguard目錄,其下面有幾個檔案:


其中seeds是我們上面配置中不進行proguard的檔案,其內容如下:

android.support.v4.accessibilityservice.AccessibilityServiceInfoCompatandroid.support.v4.accessibilityservice.AccessibilityServiceInfoCompat: android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat$AccessibilityServiceInfoVersionImpl IMPLandroid.support.v4.accessibilityservice.AccessibilityServiceInfoCompat: int CAPABILITY_CAN_RETRIEVE_WINDOW_CONTENTandroid.support.v4.accessibilityservice.AccessibilityServiceInfoCompat: int CAPABILITY_CAN_REQUEST_TOUCH_EXPLORATIONandroid.support.v4.accessibilityservice.AccessibilityServiceInfoCompat: int CAPABILITY_CAN_REQUEST_ENHANCED_WEB_ACCESSIBILITYandroid.support.v4.accessibilityservice.AccessibilityServiceInfoCompat: int CAPABILITY_CAN_FILTER_KEY_EVENTSandroid.support.v4.accessibilityservice.AccessibilityServiceInfoCompat: int FEEDBACK_BRAILLEandroid.support.v4.accessibilityservice.AccessibilityServiceInfoCompat: int FEEDBACK_ALL_MASK
而mapping.txt則是進行過proguard,被重新命名過的檔案,其內容如下:

com.xxx.xxx.xxx.xxx.xxx.xxxActivity$Get~~~~Task -> com.xxx.activity.xx.aa    com.xxx.xxx.xxx.xxx.xxx.activity.account.xxxActivity this$0 -> a         void onPreExecute() -> onPreExecute                                      android.util.SparseArray doInBackground$7d5cdb5d() -> a                  void onPostExecute(android.util.SparseArray) -> a                        java.lang.Object doInBackground(java.lang.Object[]) -> doInBackground18002240500    
可以看到 類名會變成a,b等無明顯意義的字母表示。

在進行proguard之後,應用可能運行起來會有或多或少,這樣那樣的問題,所以每一次進行過proguard之後,可以將這個seeds和mapping.txt給儲存起來,前後進行比對,從而快速定位到在proguard中出現的問題。

在我們的應用中,有很多情況下是需要使用到第三方的jar包的,別的不說,本身 support包就已經是以第三方jar包形式存在了。

在應用這些第三方jar包的時候,最好不要對這些jar包進行proguard,尤其是跟第三方系統有介面互動的時候,因為混淆了話,兩邊類名,或者欄位名,或者方法名不一致,就有可能出現問題,所以對第三方jar包進行開放,在設定檔中配置如下:

-dontwarn com.thoughtworks.xstream.**-keep class com.thoughtworks.xstream.** {*; }-keep interface com.thoughtworks.xstream.** {*; }

-keep options,顧名思義,就是保持那些檔案,不進行代碼混淆。

不過有一點要注意的是,如果在開放的代碼中有應用到泛型的話,我們還必須開放下面的屬性,如下:

# The "Signature" attribute is required to be able to access generic types when compiling in JDK 5.0 and higher.-keepattributes Signature

而如果用到注釋annotation的話,下面的屬性也是要放開的,

-keepattributes *Annotation*

結束。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.