关于混淆proguard的配置

这几天在做自己项目里面代码的混淆,对于代码的混淆的好处不用多说,减小代码的字节码大小,增加反编译代码阅读的复杂度。
如何开启混淆的配置在androidStudio的配置:
在gradle中加入这段代码 ,就可以使用proguard-rules.pro文件中的配置

 buildTypes {
        release {
            signingConfig signingConfigs.releaseConfig
            minifyEnabled true//开启混淆
            shrinkResources true//移除无用的resource文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

然后看下proguard-rules.pro的文件中配置:
我大概分了两个区域:基本不用动区域和自定义区域
基本不动区域:

#-------------------------------------------基本不用动区域--------------------------------------------
#---------------------------------基本指令区----------------------------------
#压缩指数
-optimizationpasses 5
-dontskipnonpubliclibraryclassmembers
-printmapping proguardMapping.txt
#混淆算法
-optimizations !code/simplification/cast,!field/*,!class/merging/*
-keepattributes *Annotation*,InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
#----------------------------------------------------------------------------

#---------------------------------默认保留区---------------------------------
-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 * extends android.view.View
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.** {*;}

-keep public class * extends android.view.View{
    *** get*();
    void set*(***);
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * implements java.io.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();
}
#保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
-keep class **.R$* {
 *;
}
# 对于带有回调函数的onXXEvent、**On*Listener的,不能被混淆
-keepclassmembers class * {
    void *(**On*Event);
    void *(**On*Listener);
}
#----------------------------------------------------------------------------

#---------------------------------webview------------------------------------
-keepclassmembers class fqcn.of.javascript.interface.for.Webview {
   public *;
}
-keepclassmembers class * extends android.webkit.WebViewClient {
    public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
    public boolean *(android.webkit.WebView, java.lang.String);
}
-keepclassmembers class * extends android.webkit.WebViewClient {
    public void *(android.webkit.WebView, jav.lang.String);
}
# webview + js
-keepattributes *JavascriptInterface*
-keepattributes *Annotation*
#----------------------------------------------------------------------------

自定义区域:

#-------------------------------------------定制化区域----------------------------------------------
#---------------------------------1.实体类---------------------------------
-keep class com.amugua.entity.** { *; }
-keep class com.amugua.im.entity.** { *; }
-keep class com.amugua.member.entity.** { *; }
-keep class com.amugua.action.entity.** { *; }
#-------------------------------------------------------------------------

#---------------------------------2.第三方包-------------------------------
#----gson---
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
#----picasso的混淆----
-dontwarn com.squareup.okhttp.**
# ----OkHttp3-----
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.** { *;}
-dontwarn okio.**
#-----annotations的混淆-----
-dontwarn org.apache.http.annotation.Immutable
-dontwarn org.apache.http.annotation.NotThreadSafe
-dontwarn org.springframework.**

-keep class com.mypackage.** {
  public protected private *;
}

-keepclassmembers public class org.springframework.** {
   public *;
}
#-----greendao的混淆------
-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties

# If you do not use SQLCipher:
-dontwarn org.greenrobot.greendao.database.**
# If you do not use Rx:
-dontwarn rx.**
#----个推混淆配置-----
-dontwarn com.igexin.**
-keep class com.igexin.**{*;}
#----腾讯bugly---
-keep public class com.tencent.bugly.**{*;}
#---shareSdk
   -keep class cn.sharesdk.**{*;}
    -keep class com.sina.**{*;}
    -keep class **.R$* {*;}
    -keep class **.R{*;}
    -keep class com.mob.**{*;}
    -dontwarn com.mob.**
    -dontwarn cn.sharesdk.**
    -dontwarn **.R$*
#-------------------------------------------------------------------------

#---------------------------------3.与js互相调用的类------------------------
-keepclassmembers class com.amugua.JSInterface.CustomJavaScriptInterface{ public *; }
-keepclassmembers class com.amugua.JSInterface.ShopCartInterface{ public *; }
-keepclassmembers class com.amugua.JSInterface.LocalStorageJavaScriptInterface{ public *; }
#-------------------------------------------------------------------------
#---------------------------------4.反射相关的类和方法-----------------------


#----------------------------------------------------------------------------

在混淆有js与原生交互的时候必须把关于关于有交互的类避免混淆

#---------------------------------3.与js互相调用的类------------------------
-keepclassmembers class com.amugua.JSInterface.CustomJavaScriptInterface{ public *; }
-keepclassmembers class com.amugua.JSInterface.ShopCartInterface{ public *; }
-keepclassmembers class com.amugua.JSInterface.LocalStorageJavaScriptInterface{ public *; }
#-------------------------------------------------------------------------

贴一下proguard的完整代码

#-------------------------------------------基本不用动区域--------------------------------------------
#---------------------------------基本指令区----------------------------------
-optimizationpasses 5
-dontskipnonpubliclibraryclassmembers
-printmapping proguardMapping.txt
-optimizations !code/simplification/cast,!field/*,!class/merging/*
-keepattributes *Annotation*,InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
#----------------------------------------------------------------------------

#---------------------------------默认保留区---------------------------------
-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 * extends android.view.View
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.** {*;}

-keep public class * extends android.view.View{
    *** get*();
    void set*(***);
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * implements java.io.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();
}
#保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
-keep class **.R$* {
 *;
}
# 对于带有回调函数的onXXEvent、**On*Listener的,不能被混淆
-keepclassmembers class * {
    void *(**On*Event);
    void *(**On*Listener);
}
#----------------------------------------------------------------------------

#---------------------------------webview------------------------------------
-keepclassmembers class fqcn.of.javascript.interface.for.Webview {
   public *;
}
-keepclassmembers class * extends android.webkit.WebViewClient {
    public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
    public boolean *(android.webkit.WebView, java.lang.String);
}
-keepclassmembers class * extends android.webkit.WebViewClient {
    public void *(android.webkit.WebView, jav.lang.String);
}
# webview + js
-keepattributes *JavascriptInterface*
-keepattributes *Annotation*
#----------------------------------------------------------------------------
#-------------------------------------------定制化区域----------------------------------------------
#---------------------------------1.实体类---------------------------------
-keep class com.amugua.entity.** { *; }
-keep class com.amugua.im.entity.** { *; }
-keep class com.amugua.member.entity.** { *; }
-keep class com.amugua.action.entity.** { *; }
#-------------------------------------------------------------------------

#---------------------------------2.第三方包-------------------------------
#----gson---
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# For using GSON @Expose annotation
-keepattributes *Annotation*
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
#----picasso的混淆----
-dontwarn com.squareup.okhttp.**
# ----OkHttp3-----
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.** { *;}
-dontwarn okio.**
#-----annotations的混淆-----
-dontwarn org.apache.http.annotation.Immutable
-dontwarn org.apache.http.annotation.NotThreadSafe
-dontwarn org.springframework.**

-keep class com.mypackage.** {
  public protected private *;
}

-keepclassmembers public class org.springframework.** {
   public *;
}
#-----greendao的混淆------
-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties

# If you do not use SQLCipher:
-dontwarn org.greenrobot.greendao.database.**
# If you do not use Rx:
-dontwarn rx.**
#----个推混淆配置-----
-dontwarn com.igexin.**
-keep class com.igexin.**{*;}
#----腾讯bugly---
-keep public class com.tencent.bugly.**{*;}
#---shareSdk
   -keep class cn.sharesdk.**{*;}
    -keep class com.sina.**{*;}
    -keep class **.R$* {*;}
    -keep class **.R{*;}
    -keep class com.mob.**{*;}
    -dontwarn com.mob.**
    -dontwarn cn.sharesdk.**
    -dontwarn **.R$*
#-------------------------------------------------------------------------

#---------------------------------3.与js互相调用的类------------------------
-keepclassmembers class com.amugua.JSInterface.CustomJavaScriptInterface{ public *; }
-keepclassmembers class com.amugua.JSInterface.ShopCartInterface{ public *; }
-keepclassmembers class com.amugua.JSInterface.LocalStorageJavaScriptInterface{ public *; }
#-------------------------------------------------------------------------
#---------------------------------4.反射相关的类和方法-----------------------


#----------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/ljngya/article/details/52997451
今日推荐