APK优化(六)之代码混淆

model的build.gradle
buildTypes {
        release {
//            源代码混淆 true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            //            源代码混淆 true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
proguard-rules.pro
-optimizationpasses 5 # 指定代码的压缩级别
-dontusemixedcaseclassnames # 是否使用大小写混合
-dontpreverify # 混淆时是否做预校验
-verbose # 混淆时是否记录日志
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆时所采用的算法
-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 # 保持哪些类不被混淆
-keepclasseswithmembernames class * { # 保持 native 方法不被混淆 
   native ;
}
-keepclasseswithmembers class * { # 保持自定义控件类不被混淆 
   public (android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {# 保持自定义控件类不被混淆 
   public (android.content.Context, android.util.AttributeSet, int); 
}
-keepclassmembers class * extends android.app.Activity { # 保持自定义控件类不被混淆  
   public void *(android.view.View);
}
-keepclassmembers enum * { # 保持枚举 enum 类不被混淆  
   public static **[] values(); 
   public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {#保持Parcelable不被混淆
   public static final android.os.Parcelable$Creator *;
}
# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keep public class * implements java.io.Serializable {*;}
-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();
}
-keepclassmembers class * {   
   public  (org.json.JSONObject);
}
#com.demo.demo是你的包名
-keep public class com.demo.demo.R$*{
   public static final int *;
}

Butterknife

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {    
   @butterknife.* ;
}
-keepclasseswithmembernames class * {    
   @butterknife.* ;
}

Gson

# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# 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.** { *; }
-keep class com.google.gson.** { *;}
#这句非常重要,主要是滤掉 com.demo.demo.bean包下的所有.class文件不进行混淆编译,com.demo.demo是你的包名
-keep class com.demo.demo.bean.** {*;}

SlidingMenu

-dontwarn com.jeremyfeinstein.slidingmenu.lib.**
-keep class com.jeremyfeinstein.slidingmenu.lib.**{*;}

网易

-dontwarn com.netease.**
-keep class com.netease.** {*;}

以上列举了几个,需要的话去官网查找。

注意

将所需要的混淆都加进去,发现哪个class混淆有问题就keep哪个,例如-keep class com.netease.** {*;} 即不混淆netease下的东西。检查重复的 然后删掉即可

APK优化(一)之SVG--------------解决大量的套图问题
APK优化(二)之Tint着色器-------------减少apk体积
APK优化(三)之资源打包配置----resources.arsc
APK优化(四)之.so动态库打包配置
APK优化(五)之移除无用资源,物理删除
APK优化(六)之代码混淆
APK优化(七)之启用shrinkResources资源缩减 ,不需要物理上的删除
APK优化(八)之启用webp转换插件-----图片变小
APK优化(九)之资源res文件混淆和7ZIP压缩

你可能感兴趣的:(APK优化(六)之代码混淆)