第一次混淆,主要是因为引用本地第三方jar包的问题。虽然说本地第三方jar包自动避免混淆,但一些本地第三方jar包下的一些包要避免混淆。比如:
文中的com、org 这些包名都要避免混淆。
下面是我用到的混淆代码:
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/firefish/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # 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 *;
#} -keep class de.greenrobot.event.** {*;}
-keepclassmembers class ** {
public void onEvent*(**);
void onEvent*(**);
}
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
} #指定代码的压缩级别
-optimizationpasses 5 #包明不混合大小写
-dontusemixedcaseclassnames #不去忽略非公共的库类
-dontskipnonpubliclibraryclasses #优化 不优化输入的类文件
-dontoptimize #预校验
-dontpreverify #混淆时是否记录日志
-verbose # 混淆时所采用的算法
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* #保护注解
-keepattributes *Annotation* # 保持哪些类不被混淆
-keep public class * extends android.app.Fragment
-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
#如果有引用v4包可以添加下面这行
-keep public class * extends android.support.v4.app.Fragment #忽略警告
-ignorewarning ##记录生成的日志数据,gradle build时在本项目根目录输出##
#apk 包内所有 class 的内部结构
-dump proguard/class_files.txt
#未混淆的类和成员
-printseeds proguard/seeds.txt
#列出从 apk 中删除的代码
-printusage proguard/unused.txt
#混淆前后的映射
-printmapping proguard/mapping.txt
########记录生成的日志数据,gradle build时 在本项目根目录输出-end###### #如果引用了v4或者v7包
-dontwarn android.support.** # support-v4
#https://stackoverflow.com/questions/18978706/obfuscate-android-support-v7-widget-gridlayout-issue
-dontwarn android.support.v4.**
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class android.support.v4.** { *; } #保留crash日志的行号
-keepattributes SourceFile,LineNumberTable # support-v7
-dontwarn android.support.v7.**
-keep class android.support.v7.internal.** { *; }
-keep interface android.support.v7.internal.** { *; }
-keep class android.support.v7.** { *; } # support design
#@link http://stackoverflow.com/a/31028536
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; } ####混淆保护自己项目的部分代码以及引用的第三方jar包library-end#### #保持 native 方法不被混淆
-keepclasseswithmembernames class * {
native <methods>;
} #保持自定义控件类不被混淆
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
} #保持自定义控件类不被混淆
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
} -keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
} #保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
} #保持 Serializable 不被混淆
-keepnames class * implements java.io.Serializable #保持 Serializable 不被混淆并且enum 类也不被混淆
-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();
} #保持枚举 enum 类不被混淆
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
} -keepclassmembers class * {
public void *ButtonClicked(android.view.View);
} #不混淆资源类
-keepclassmembers class **.R$* {
public static <fields>;
} #避免混淆泛型 如果混淆报错建议关掉
-keepattributes Signature #移除Log类打印各个等级日志的代码,打正式包的时候可以做为禁log使用,这里可以作为禁止log打印的功能使用,另外的一种实现方案是通过BuildConfig.DEBUG的变量来控制
#-assumenosideeffects class android.util.Log {
# public static *** v(...);
# public static *** i(...);
# public static *** d(...);
# public static *** w(...);
# public static *** e(...);
#} #############################################################################################
######################## 以上通用 ##################################
############################################################################################# ####################### 常用第三方模块的混淆选项 ###################################
#gson
#如果用用到Gson解析包的,直接添加下面这几行就能成功混淆,不然会报错。
-keepattributes Signature
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.** { *; }
-keep class com.google.gson.stream.** { *; } #mob
-keep class android.net.http.SslError
-keep class android.webkit.**{*;}
-keep class cn.sharesdk.**{*;}
-keep class com.sina.**{*;}
-keep class m.framework.**{*;}
-keep class **.R$* {*;}
-keep class **.R{*;}
-dontwarn cn.sharesdk.**
-dontwarn **.R$* #butterknife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * {
@butterknife.* <fields>;
} -keepclasseswithmembernames class * {
@butterknife.* <methods>;
} ######引用的其他Module可以直接在app的这个混淆文件里配置 # 如果使用了Gson之类的工具要使被它解析的JavaBean类即实体类不被混淆。
-keep class com.matrix.app.entity.json.** { *; }
-keep class com.matrix.appsdk.network.model.** { *; } #####混淆保护自己项目的部分代码以及引用的第三方jar包library#######
#如果在当前的application module或者依赖的library module中使用了第三方的库,并不需要显式添加规则
#-libraryjars xxx
#添加了反而有可能在打包的时候遭遇同一个jar多次被指定的错误,一般只需要添加忽略警告和保持某些class不被混淆的声明。
#以libaray的形式引用了开源项目,如果不想混淆 keep 掉,在引入的module的build.gradle中设置minifyEnabled=false
-keep class com.nineoldandroids.** { *; }
-keep interface com.nineoldandroids.** { *; }
-dontwarn com.nineoldandroids.**
# 下拉刷新
-keep class in.srain.cube.** { *; }
-keep interface in.srain.cube.** { *; }
-dontwarn in.srain.cube.**
#fab-transformation
#-keep class konifar.fab-transformation.** { *; }
#-keep interface konifar.fab-transformation.** { *; }
#-dontwarn konifar.fab-transformation.**
#info.hoang8f:android-segmented
-keep class info.hoang8f.android-segmented.** { *; }
-keep interface info.hoang8f.android-segmented.** { *; }
-dontwarn info.hoang8f.android-segmented.**
#xutils:3.3.36
################### region for xUtils
-keepattributes Signature,*Annotation*
-keep public class org.xutils.** {
public protected *;
}
-keep public interface org.xutils.** {
public protected *;
}
-keepclassmembers class * extends org.xutils.** {
public protected *;
}
-keepclassmembers @org.xutils.db.annotation.* class * {*;}
-keepclassmembers @org.xutils.http.annotation.* class * {*;}
-keepclassmembers class * {
@org.xutils.view.annotation.Event <methods>;
}
#################### end region
#eventbus:3.0.0
-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);
}
# OkHttp3
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.** { *;}
-dontwarn okio.**
# Okio
-dontwarn com.squareup.**
-dontwarn okio.**
-keep public class org.codehaus.* { *; }
-keep public class java.nio.* { *; }
#com.getbase:floatingactionbutton
-keep class com.getbase.floatingactionbutton.** { *; }
-keep interface com.getbase.floatingactionbutton.** { *; }
-dontwarn com.getbase.floatingactionbutton.**
#com.orhanobut:logger:1.15
-keep class com.orhanobut.logger.** { *; }
-keep interface com.orhanobut.logger.** { *; }
-dontwarn com.com.orhanobut.logger.**
#com.zhy:flowlayout-lib:1.0.3
-keep class com.zhy.flowlayout-lib.** { *; }
-keep interface com.zhy.flowlayout-lib.** { *; }
-dontwarn com.zhy.flowlayout-lib.**
# observablescrollview:tab fragment
-keep class com.github.ksoichiro.** { *; }
-keep interface com.github.ksoichiro.** { *; }
-dontwarn com.github.ksoichiro.**
#greendao:3.2.2
-keep class org.greenrobot.greendao.**{*;}
-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties
##---------------Begin: proguard configuration for fastjson ----------
-keep public class * implements java.io.Serializable {
public *;
}
-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();
}
-dontwarn android.support.**
-dontwarn com.alibaba.fastjson.** -dontskipnonpubliclibraryclassmembers
-dontskipnonpubliclibraryclasses #-libraryjars libs/fastjson.jar
-keep class com.alibaba.fastjson.** { *; } -keepclassmembers class * {
public <methods>;
}
##---------------End: proguard configuration for fastjson ---------- #-libraryjars libs/AnyOfficeSDK.jar
-dontwarn com.huawei.**
-keep class com.huawei.** { *; } -dontwarn com.tencent.**
-keep class com.tencent.** { *; } #-libraryjars libs/iAppPDF.jar
-dontwarn com.kinggrid.**
-keep class com.kinggrid.** { *; }
-dontwarn com.longmai.mtoken.k5.**
-keep class com.longmai.mtoken.k5.** { *; }
-dontwarn org.bouncycastle.**
-keep class org.bouncycastle.**{*;}
-dontwarn org.dom4j.**
-keep class org.dom4j.**{*;}
-dontwarn org.jaxen.**
-keep class org.jaxen.**{*;}
-dontwarn org.w3c.dom.**
-keep class org.w3c.dom.**{*;} -keep class org.xmlpull.v1.** { *; }