混淆介绍:针对项目代码,为防止反编译和再次编译你的APP,需要在打包APP时加入混淆。通过对混淆文件的配置从而将程序的类名、方法名、成员变量等进行无意义的字符替换,达到反编译后阅读困难。通过反编译,apk的体积能减小25%,亲测有效。
混淆语法:
1.keep:保留类和类中的成员,防止被混淆;
2.dontwarn:dontwarn是一个和keep可以说是形影不离,尤其是处理引入的library时.
3:* 匹配任意长度字符,不包含包名分隔符(.)
4.匹配任意长度字符,包含包名分隔符(.)
混淆使用方法:
1.在app的build.gradle文件中设置minifyEnabled true
android {
buildTypes {
release {
minifyEnabled true//是否启动混淆true:打开;false:关闭
shrinkResources true//打开资源压缩。
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
2.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
-ignorewarnings
-dontwarn android.support.**
# ------不混淆实体类-----------
-dontwarn com.incall.apps.lib.bean.**
-keep class com.incall.apps.lib.bean.** { *; }
-dontwarn com.incall.apps.fota.bean.**
-keep class com.incall.apps.fota.bean.** { *; }
-dontwarn com.incall.apps.fota.upgradeobject.**
-keep class com.incall.apps.fota.upgradeobject.** { *; }
-dontwarn com.incall.apps.bubu.Beans.**
-keep class com.incall.apps.bubu.Beans.** { *; }
-dontwarn com.incall.apps.fota.ecu.protocol.b.wrapper.InstallProgressBean
-keep class com.incall.apps.fota.ecu.protocol.b.wrapper.InstallProgressBean { *; }
-dontwarn com.incall.apps.fota.ecu.protocol.a.api.EcuInfoBean
-keep class com.incall.apps.fota.ecu.protocol.a.api.EcuInfoBean { *; }
-dontwarn com.incall.apps.fota.ecu.protocol.a.api.BreakPointCacheBean
-keep class com.incall.apps.fota.ecu.protocol.a.api.BreakPointCacheBean { *; }
-dontwarn com.incall.apps.fota.ecu.protocol.a.api.InstallConditionBean
-keep class com.incall.apps.fota.ecu.protocol.a.api.InstallConditionBean { *; }
# ------不混淆泛型和反射----
-keepattributes Signature
-keepattributes *Annotation*
-keep class * extends java.lang.annotation.Annotation { *; }
# ------不混淆枚举----
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# -----不混淆序列化-------
-keepnames class * implements java.io.Serializable
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
!private <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
# 对于带有回调函数的onXXEvent、**On*Listener的,不能被混淆
-keepclassmembers class * {
void *(**On*Event);
void *(**On*Listener);
}
# ------不混淆泛型和反射----
-keepattributes Signature
-keepattributes *Annotation*
-keep class * extends java.lang.annotation.Annotation { *; }
# ------不混淆集成自view的自定义控件----
-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);
}
# ------不混淆lib-system中的jar----
-dontwarn com.incall.proxy.**
-keep class com.incall.proxy.** { *;}
-dontwarn com.car.**
-keep class com.car.** { *;}
-dontwarn com.incall.vehicle.proxy.**
-keep class com.incall.vehicle.proxy.** { *;}
-dontwarn com.wutong.upgradetool.**
-keep class com.wutong.upgradetool.** { *;}
#-libraryjars ..\lib-system\libs_S201\classes0627.jar
# ------不混淆的第三方库------
-dontwarn com.fce.**
-keep class com.fce.** { *;}
# org
-dontwarn org.**
-keep class org.** { *;}
# android
-dontwarn android.**
-keep class android.** { *;}
-dontwarn com.android.**
-keep class com.android.** { *;}
# google
-dontwarn android.**
-keep class android.** { *;}
# androidx
-dontwarn androidx.**
-keep class androidx.** { *;}
# jdk
-dontwarn java.**
-keep class java.** { *;}
-dontwarn javax.**
-keep class javax.** { *;}
-dontwarn org.**
-keep class org.** { *;}
-dontwarn sun.misc.**
-keep class sun.misc.** { *;}
# squareup
-dontwarn com.squareup.**
-keep class com.squareup.** { *;}
# dinuscxj
-dontwarn com.dinuscxj.**
-keep class com.dinuscxj.** { *;}
# okhttp
-dontwarn okio.**
-keep class okio.** { *;}
-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.** { *;}
-dontwarn okhttp3.**
-keep class okhttp3.** { *;}
# gson
-dontwarn ccom.google.gson.**
-keep class com.google.gson.** { *;}
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
# retrofit2
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
# RxJava2 RxAndroid
-dontwarn io.reactivex.**
-keep class io.reactivex.** { *; }
# jetbrains
-dontwarn org.jetbrains.**
-keep class org.jetbrains.** { *; }
# junit
-dontwarn junit.**
-keep class junit.** { *; }
# apache
-dontwarn org.apache.**
-keep class apache.** { *; }
# 不混淆资源类
-keepclassmembers class **.R$* {
public static <fields>;
}
3.混淆中经常遇到的问题:
(1)引用第三方jar时不能混淆
(2)混淆不能多次加入