Android 混淆

一、步驟:

ProGuard由shrink、optimize、obfuscate和preverify四個步驟組成,每個步驟都是可選的,需要哪些步驟都可以在腳本中配置。參見ProGuard官方介紹。

  壓縮(Shrink):默認開啓,偵測並移除代碼中無用的類、字段、方法和特性,減少應用體積,並且會在優化動作執行之後再次執行(因爲優化後可能會再次暴露一些未使用的類和成員)。
    -dontshrink 關閉混淆
  優化(Optimize):默認開啓,分析和優化字節碼,讓應用運行的更快。
    -dontoptimize 關閉優化,默認混淆配置文件開始
    -optimizationpasses n 表示proguard對代碼進行迭代優化的次數,Android一般爲5
  混淆(Obfuscate):默認開啓,使用a、b、c、d這樣簡短而無意義的名稱,對類、字段和方法進行重命名,增大反編譯難度。
    -dontobfuscate 關閉混淆
上面三個步驟使代碼大小更小、更高效,也更難被逆向工程。

  預檢(Preverify):在java平臺上對處理後的代碼進行預檢。

二、開啓方法:

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

三、默認的混淆方案

proguard-android.txt

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# This file is no longer maintained and is not used by new (2.2+) versions of the
# Android plugin for Gradle. Instead, the Android plugin for Gradle generates the
# default rules at build time and stores them in the build directory.


-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 <methods>;
}


# 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);
}


-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}


-keepclassmembers class **.R$* {
    public static <fields>;
}


# 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.**


# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep


-keep @android.support.annotation.Keep class * {*;}


-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}


-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}


-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);

}

四、通配符

? matches any single character in a name.(匹配一個字符)

* matches any part of a name not containing the directory separator.(匹配一個名字,除了目錄分隔符外的任意部分)

** matches any part of a name, possibly containing any number of directory separators.(匹配任意名,可能包含任意路徑分隔符)

! exclude

<field> 匹配類中的所有字段

<method> 匹配類中所有的方法

<init> 匹配類中所有的構造函數

五、關鍵

-keep 只保留類名 加上{*;}則會保留類名和所有成員
-keepclassmembers 只保留成員
-keepclasseswithmembers 保留類名及指定的成員
不帶names的選項爲既不會被移除或重命名,即使類或類成員未被使用。帶有names的選項爲不會被重命名,如果是無用的類或類成員,會被移除,移除是指在壓縮(Shrinking)時是否會被刪除。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章