1. 程式人生 > >Android 混淆 以及 java程式碼方式實現混淆配置

Android 混淆 以及 java程式碼方式實現混淆配置

第一步 : 在build.gradle中做如下配置

注 : 可以分別配置debug與release的混淆配置

android {
    buildTypes {
        debug {
            minifyEnabled true     //混淆
            zipAlignEnabled true   //zipalign優化
            shrinkResources true   //移除無用的resource檔案
            //前一部分代表系統預設的android程式的混淆檔案,該檔案已經包含了基本的混淆宣告,後一個檔案是自己的定義混淆檔案
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } release { minifyEnabled true //混淆 zipAlignEnabled true //zipalign優化 shrinkResources true //移除無用的resource檔案 //前一部分代表系統預設的android程式的混淆檔案,該檔案已經包含了基本的混淆宣告,後一個檔案是自己的定義混淆檔案
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }

第二步 : 配置proguard-rules.pro檔案

以下是根據本人實際專案中做出的配置,可以參考

# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

# * 混淆日誌生成目錄:app.build.outputs.mapping下,打包時請拷貝此日誌,用於錯誤日誌還原分析.
#--------------------------------------------- 公共配置 ----------------------------------------#
-optimizationpasses 5
# 程式碼混淆壓縮比
-dontusemixedcaseclassnames # 混合時不使用大小寫混合,混合後的類名為小寫 -dontskipnonpubliclibraryclasses # 指定不去忽略非公共的庫類。 -dontskipnonpubliclibraryclassmembers # 指定不去忽略非公共庫的類成員 -dontpreverify # 混淆時不做預校驗 -verbose # 使我們的專案混淆後產生對映檔案 : 包含有類名->混淆後類名的對映關係 -ignorewarnings # 忽略所有警告 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*# 指定混淆是採用的演算法,後面的引數是一個過濾器(谷歌推薦的演算法,一般不做更改) -keepattributes *Annotation*,InnerClasses #保留註解不混淆 -keepattributes Signature # 避免混淆泛型 -keepattributes SourceFile,LineNumberTable# 丟擲異常時保留程式碼行號 #--------------------------------------------- 公共配置 End ----------------------------------------# #--------------------------------------------- Java ----------------------------------------# -keepclasseswithmembernames class * { # 保留本地native方法不被混淆 native <methods>; } -keepclassmembers enum * {# 保留列舉類不被混淆 public static **[] values(); public static ** valueOf(java.lang.String); } -keepclassmembers class * {# 保留JsonObject不被混淆 public <init> (org.json.JSONObject); } -keep class * implements android.os.Parcelable {# 保留Parcelable序列化類不被混淆 public static final android.os.Parcelable$Creator *; } -keepnames class * implements java.io.Serializable -keepclassmembers class * implements java.io.Serializable {# 保留Serializable序列化的類不被混淆 static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); } #--------------------------------------------- Java End ----------------------------------------# #--------------------------------------------- Android 系統 ----------------------------------------# -keep public class * extends android.view.View -keep public class * extends android.app.Fragment -keep public class * extends android.app.Activity -keep public class * extends android.app.Application -keep public class * extends android.app.Service -keep public class * extends android.content.BroadcastReceiver -keep public class * extends android.content.ContentProvider -keep public class * extends android.app.backup.BackupAgentHelper -keep public class * extends android.preference.Preference -keep public class com.android.vending.licensing.ILicensingService -keep class **.R$* { *; }# 保留R下面的資源 -keep class android.support.** { *; }# 保留support下的所有類及其內部類 -keep public class * extends android.support.v4.** -keep public class * extends android.support.v7.** -keep public class * extends android.support.annotation.** -keepclassmembers class * extends android.app.Activity{#保留在Activity中的方法引數是view的方法(這樣以來我們在layout中寫的onClick就不會被影響) public void *(android.view.View); } -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet); } -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet, int); } -keepclassmembers class * {# 對於帶有回撥函式的**on*Event、**On*Listener 不被混淆 void *(**On*Event); void *(**On*Listener); } #--------------------------------------------- Android 系統 End ----------------------------------------# #--------------------------------------------- 繼承類混淆配置 ----------------------------------------# -keep interface com.acmenxd.frame.utils.proguard.** { *; } -keep class * implements com.acmenxd.frame.utils.proguard.IKeepClass -keepnames class * implements com.acmenxd.frame.utils.proguard.IKeepClassName -keepclassmembers class * implements com.acmenxd.frame.utils.proguard.IKeepFieldName { <fields>; } -keepclassmembers class * implements com.acmenxd.frame.utils.proguard.IKeepPublicFieldName { public <fields>; } -keepclassmembers class * implements com.acmenxd.frame.utils.proguard.IKeepMethodName { <methods>; } -keepclassmembers class * implements com.acmenxd.frame.utils.proguard.IKeepPublicMethodName { public <methods>; } #--------------------------------------------- 繼承類混淆配置 End ----------------------------------------# #--------------------------------------------- 依賴庫混淆配置 ----------------------------------------# #libs #rxjava & rxandroid -dontwarn sun.misc.** -keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* { long producerIndex; long consumerIndex; } -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef { rx.internal.util.atomic.LinkedQueueNode producerNode; } -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef { rx.internal.util.atomic.LinkedQueueNode consumerNode; } #okhttp3 -dontwarn okio.** -dontwarn okhttp3.** -dontwarn com.squareup.okhttp3.** #retrofit2 -dontwarn retrofit2.** -keep class retrofit2.** { *; } -keepattributes Signature -keepattributes Exceptions #FrescoView -dontwarn okio.** -dontwarn okhttp3.** -dontwarn com.squareup.okhttp3.** -dontwarn javax.annotation.** -dontwarn com.android.volley.toolbox.** -keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip -keep @com.facebook.common.internal.DoNotStrip class * -keepclassmembers class * { @com.facebook.common.internal.DoNotStrip *; } -keepclassmembers class * { native <methods>; } #Glide -keep public class * implements com.bumptech.glide.module.GlideModule -keep class com.bumptech.glide.** { *; } #greenDAO 3.2.0 #-keep class * extends org.greenrobot.greendao.AbstractDao{ *; } -keep class org.greenrobot.greendao.**{*;} -keepclassmembers class * extends org.greenrobot.greendao.AbstractDao { public static java.lang.String TABLENAME; } -keep class **$Properties #eventbus 3.0.0 -keepattributes *Annotation* -keepclassmembers class ** { @org.greenrobot.eventbus.Subscribe <methods>; } -keep enum org.greenrobot.eventbus.ThreadMode { *; } -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent { <init>(java.lang.Throwable); } #--------------------------------------------- 專案混淆 處理 ----------------------------------------# -keep class com.acmenxd.core.model.**{ *; } # 實體類 -keep class com.acmenxd.mvp.model.**{ *; } # 實體類 #--------------------------------------------- 反射類 處理 ----------------------------------------# #--------------------------------------------- WebView 處理 ----------------------------------------# #--------------------------------------------- js互動 處理 ----------------------------------------#

解釋部分 -> 繼承混淆類配置

其作用是避免 在實際專案中N多個類或包 需要做混淆配置, 導致proguard-rules.pro檔案過於混亂
其實現方式是 :
一. 在專案中定義介面,如下 :

/**
 * @author AcmenXD
 * @version v1.0
 * @github https://github.com/AcmenXD
 * @date 2017/2/21 18:00
 * @detail 實現此介面的類 -> 將保持整個類都不混淆
 */
public interface IKeepClass extends IKeepFieldName, IKeepMethodName {
}
/**
 * @author AcmenXD
 * @version v1.0
 * @github https://github.com/AcmenXD
 * @date 2017/2/21 18:00
 * @detail 實現此介面的類 -> 將保持類名不混淆
 */
public interface IKeepClassName {
}
/**
 * @author AcmenXD
 * @version v1.0
 * @github https://github.com/AcmenXD
 * @date 2017/2/21 18:00
 * @detail 實現此介面的類 -> 將保持類名和成員變數都不混淆
 */
public interface IKeepFieldName extends IKeepClassName {
}
/**
 * @author AcmenXD
 * @version v1.0
 * @github https://github.com/AcmenXD
 * @date 2017/2/21 18:00
 * @detail 實現此介面的類 -> 將保持類名和方法名都不混淆
 */
public interface IKeepMethodName extends IKeepClassName {
}
/**
 * @author AcmenXD
 * @version v1.0
 * @github https://github.com/AcmenXD
 * @date 2017/2/21 18:00
 * @detail 實現此介面的類 -> 將保持類名和public成員變數都不混淆
 */
public interface IKeepPublicFieldName extends IKeepClassName {
}
/**
 * @author AcmenXD
 * @version v1.0
 * @github https://github.com/AcmenXD
 * @date 2017/2/21 18:00
 * @detail 實現此介面的類 -> 將保持類名和public方法名都不混淆
 */
public interface IKeepPublicMethodName extends IKeepClassName {
}

二. 在proguard-rules.pro檔案新增如下內容

#--------------------------------------------- 繼承類混淆配置 ----------------------------------------#
-keep interface com.acmenxd.frame.utils.proguard.** { *; }
-keep class * implements com.acmenxd.frame.utils.proguard.IKeepClass
-keepnames class * implements com.acmenxd.frame.utils.proguard.IKeepClassName
-keepclassmembers class * implements com.acmenxd.frame.utils.proguard.IKeepFieldName {
    <fields>;
}
-keepclassmembers class * implements com.acmenxd.frame.utils.proguard.IKeepPublicFieldName {
    public <fields>;
}
-keepclassmembers class * implements com.acmenxd.frame.utils.proguard.IKeepMethodName {
    <methods>;
}
-keepclassmembers class * implements com.acmenxd.frame.utils.proguard.IKeepPublicMethodName {
    public <methods>;
}
#--------------------------------------------- 繼承類混淆配置 End ----------------------------------------#

三. 使用

/** 實現IKeepClass類,所以TestA整個類都不混淆,並且無需再proguard-rules.pro配置 */
public class TestA implements IKeepClass{
}
/** 實現IKeepClassName類,所以TestA類名不混淆,並且無需再proguard-rules.pro配置 */
public class TestA implements IKeepClassName{
}
/** 實現IKeepFieldName類,所以TestA類名和成員變數都不混淆,並且無需再proguard-rules.pro配置 */
public class TestA implements IKeepFieldName{
}
/** 實現IKeepMethodName類,所以TestA類名和方法名都不混淆,並且無需再proguard-rules.pro配置 */
public class TestA implements IKeepMethodName{
}

End