1. 程式人生 > >Android開發之常用第三方庫混淆欄位

Android開發之常用第三方庫混淆欄位

在Android開發中, 經常使用別人的第三方庫, 開發完成後, 我們需要對apk進行壓縮, 混淆, 這樣別人就很難看懂你的程式碼了, 就算反編譯過來也看不懂(大牛除外). 下面收集一些目前我正在使用的混淆欄位, 我有個專案apk是10M, 壓縮混淆後只有3.4M, 很爽的喲.

開啟壓縮和混淆

要開啟混淆, 一般只需要把minifyEnabled設定為true就好了.

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

常用庫混淆

  • agentweb
-keep class com.just.agentweb.** {
    *;
}
-dontwarn com.just.agentweb.**

-dontwarn javax.annotation.**
-dontwarn javax.inject.**
  • OkHttp3
-dontwarn okhttp3.logging.**
-keep class okhttp3.internal.**{*;}
-dontwarn okio.**
  • Retrofit
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature-keepattributes
-keep class io.haobi.wallet.network.** { *; }
  • 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; }
  • Gson
-keepattributes EnclosingMethod
-keep class io.haobi.wallet.beans.** { *; }
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-keep class com.google.gson.** { *;}
  • EventBus
-keepattributes *Annotation*
-keepclassmembers class * {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}
  • LRecyclerview
-dontwarn com.github.jdsjlzx.**
-keep class com.github.jdsjlzx.progressindicator.indicators.** { *; }
  • Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}
  • bitcoinj
-keep,includedescriptorclasses class org.bitcoinj.wallet.Protos$** { *; }
-keepclassmembers class org.bitcoinj.wallet.Protos { com.google.protobuf.Descriptors$FileDescriptor descriptor; }
-keep,includedescriptorclasses class org.bitcoin.protocols.payments.Protos$** { *; }
-keepclassmembers class org.bitcoin.protocols.payments.Protos { com.google.protobuf.Descriptors$FileDescriptor descriptor; }
-dontwarn org.bitcoinj.store.WindowsMMapHack
-dontwarn org.bitcoinj.store.LevelDBBlockStore
-dontnote org.bitcoinj.crypto.DRMWorkaround
-dontnote org.bitcoinj.crypto.TrustStoreLoader$DefaultTrustStoreLoader
-dontnote com.subgraph.orchid.crypto.PRNGFixes
-dontwarn okio.DeflaterSink
-dontwarn okio.Okio
-dontnote com.squareup.okhttp.internal.Platform
-dontwarn org.bitcoinj.store.LevelDBFullPrunedBlockStore**
  • Spongycastle
-dontwarn org.spongycastle.**
  • jackson
-keepnames class com.fasterxml.jackson.** {
*;
}
-keepnames interface com.fasterxml.jackson.** {
    *;
}
-dontwarn com.fasterxml.jackson.databind.**
  • slf4j
-keep class org.slf4j.** { *; }
-dontwarn org.slf4j.**
  • android-support
-dontwarn android.support.**
-dontnote android.support.**
-keep class android.support.v7.widget.RoundRectDrawable { *; }

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

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

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclasseswithmembernames,includedescriptorclasses class * {
    native <methods>;
}

-keepclassmembers,includedescriptorclasses public class * extends android.view.View {
    void set*(***);
    *** get*();
}

-keepclassmembers class * extends android.app.Activity {
    public void *(android.view.View);
}

-keepclassmembers class * implements java.io.Serializable {
    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程式碼, 會依稀看清楚某些程式碼構成, 這對於程式碼保護不利, 那麼應該怎麼辦呢? 還有種神混淆, 那麼就是自定義混淆字典.我們先來看一下他們的區別

  • 自定義字典前

  • 自定義字典後

感覺如何? 是不是很讓人頭大, 那麼應該如何實現這種效果呢? 這個時候就需要一個自定義混淆字典了. 只需要在混淆檔案裡面加入以下程式碼

-obfuscationdictionary dic.txt
-classobfuscationdictionary dic.txt
-packageobfuscationdictionary dic.txt

dic.txt的位置在專案根目錄/app/dic.txt, 是不是很簡單啦? 自定義字典下載地址:dic.txt