首先将位于tools/proguard/ 目录下的混淆设置复制到proguard-rules.pro 文件中

  1. optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆时所采用的算法
  2. optimizationpasses 5 # 指定代码的压缩级别
  3. allowaccessmodification # 允许访问并修改有修饰符的类和类的成员
  4. dontpreverify # 混淆时不做预校验
  5. # The remainder of this file is identical to the non-optimized version
  6. # of the Proguard configuration file (except that the other file has
  7. # flags to turn off optimization).
  8. dontusemixedcaseclassnames # 是否使用大小写混合
  9. dontskipnonpubliclibraryclasses # 是否混淆第三方jar
  10. verbose # 混淆时记录日志
  11. keepattributes *Annotation* # 保留任何可选属性
  12. keep public class com.google.vending.licensing.ILicensingService
  13. keep public class com.android.vending.licensing.ILicensingService
  14. # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
  15. keepclasseswithmembernames class * {
  16. native <methods>;
  17. }
  18. # keep setters in Views so that animations can still work.
  19. # see http://proguard.sourceforge.net/manual/examples.html#beans
  20. keepclassmembers public class * extends android.view.View {
  21. void set*(***);
  22. *** get*();
  23. }
  24. # We want to keep methods in Activity that could be used in the XML attribute onClick
  25. keepclassmembers class * extends android.app.Activity {
  26. public void *(android.view.View);
  27. }
  28. # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
  29. keepclassmembers enum * {
  30. public static **[] values();
  31. public static ** valueOf(java.lang.String);
  32. }
  33. keepclassmembers class * implements android.os.Parcelable {
  34. public static final android.os.Parcelable$Creator CREATOR;
  35. }
  36. keepclassmembers class **.R$* {
  37. public static <fields>;
  38. }
  39. # The support library contains references to newer platform versions.
  40. # Don’t warn about those in case this app is linking against an older
  41. # platform version. We know about them, and they are safe.
  42. dontwarn android.support.**
  43. # Understand the @Keep support annotation.
  44. keep class android.support.annotation.Keep
  45. keep @android.support.annotation.Keep class * {*;}
  46. keepclasseswithmembers class * {
  47. @android.support.annotation.Keep <methods>;
  48. }
  49. keepclasseswithmembers class * {
  50. @android.support.annotation.Keep <fields>;
  51. }
  52. keepclasseswithmembers class * {
  53. @android.support.annotation.Keep <init>(…);
  54. }

 

通常不需混淆的Android类

  1. -keep public class * extends android.app.Fragment
  2. -keep public class * extends android.app.Activity
  3. -keep public class * extends android.app.Application
  4. -keep public class * extends android.app.Service
  5. -keep public class * extends android.content.BroadcastReceiver
  6. -keep public class * extends android.preference.Preference
  7. -keep public class * extends android.content.ContentProvider
  8. -keep public class * extends android.support.v4.**
  9. -keep public class * extends android.support.annotation.**
  10. -keep public class * extends android.support.v7.**

 

Butter Knife

  1. keep class butterknife.** { *; }
  2. dontwarn butterknife.internal.**
  3. keep class **$$ViewBinder { *; }
  4. keepclasseswithmembernames class * {
  5. @butterknife.* <fields>;
  6. }
  7. keepclasseswithmembernames class * {
  8. @butterknife.* <methods>;
  9. }

 

Retrofit

  1. dontwarn retrofit2.**
  2. keep class retrofit2.** { *; }
  3. keepattributes Signature
  4. keepattributes Exceptions

 

RxJava RxAndroid

  1. -dontwarn sun.misc.**
  2. -keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
  3. long producerIndex;
  4. long consumerIndex;
  5. }
  6. -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
  7. rx.internal.util.atomic.LinkedQueueNode producerNode;
  8. }
  9. -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
  10. rx.internal.util.atomic.LinkedQueueNode consumerNode;
  11. }

 

Gson

  1. keepattributes Signature-keepattributes *Annotation*
  2. keep class sun.misc.Unsafe { *; }
  3. keep class com.google.gson.stream.** { *; }
  4. # Application classes that will be serialized/deserialized over Gson 下面替换成自己的实体类
  5. keep class com.example.bean.** { *; }

 

OkHttp3

  1. dontwarn com.squareup.okhttp3.**
  2. keep class com.squareup.okhttp3.** { *;}
  3. dontwarn okio.**

 

Glide

  1. -keep public class * implements com.bumptech.glide.module.GlideModule
  2. keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  3. **[] $VALUES;
  4. public *;
  5. }

 

友盟统计

  1. -keepclassmembers class * {
  2. public <init> (org.json.JSONObject);
  3. }
  4. -keep public class [您的应用包名].R$*{
  5. public static final int *;
  6. }
  7. -keepclassmembers enum * {
  8. public static **[] values(); public static ** valueOf(java.lang.String);
  9. }