Android studio 混淆打包问题

枕上诗书闲处好,门前风景雨来佳。这篇文章主要讲述Android studio 混淆打包问题相关的知识,希望能为你提供帮助。
参考 :
Android Studio代码混淆设置以及上传mapping文件
AndroidStudio 混淆打包
 
在app 目录下   proguard-rules.pro中加入 通用 混淆

#指定代码的压缩级别 -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.**####混淆保护自己项目的部分代码以及引用的第三方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(...); #}############################################################################################# ########################以上通用################################## #############################################################################################

 
【Android studio 混淆打包问题】 
问题:
用到afinal注解的项目代码混淆后某些控件view点击事件无效
添加下面的,注解使用的控件就能用了
# ---------------------- #不压缩指定的文件 #-dontshrink # ---------------------- -dontshrink

完整的如下
# Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in D:\\android-sdk\\sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For ic_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 *; #}#指定代码的压缩级别 -optimizationpasses 5#包明不混合大小写 -dontusemixedcaseclassnames#不去忽略非公共的库类 -dontskipnonpubliclibraryclasses #优化不优化输入的类文件 -dontoptimize #预校验 -dontpreverify #混淆时是否记录日志 -verbose # 混淆时所采用的算法 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*# 保持哪些类不被混淆 -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.**####混淆保护自己项目的部分代码以及引用的第三方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(...); #}############################################################################################# ########################以上通用################################## #############################################################################################-keep public class android.support.v7.widget.** { *; } -keep public class android.support.v7.internal.widget.** { *; } -keep public class android.support.v7.internal.view.menu.** { *; }-keep public class * extends android.support.v4.view.ActionProvider { public < init> (android.content.Context); }#2.rxjava -keep class rx.schedulers.Schedulers { public static < methods> ; } -keep class rx.schedulers.ImmediateScheduler { public < methods> ; } -keep class rx.schedulers.TestScheduler { public < methods> ; } -keep class rx.schedulers.Schedulers { public static ** test(); } -dontwarn sun.misc.** -keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* { long producerIndex; long consumerIndex; } -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef { rx.internal.util.atomic.LinkedQueueNode producerNode; } -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef { rx.internal.util.atomic.LinkedQueueNode consumerNode; }#3.retrolambda -dontwarn java.lang.invoke.*#4.support-v4 -keep class android.support.v4.** { *; } -keep interface android.support.v4.** { *; }#5.ucrop -dontwarn com.yalantis.ucrop** -keep class com.yalantis.ucrop** { *; } -keep interface com.yalantis.ucrop** { *; }#6.photoview -keep class uk.co.senab.photoview** { *; } -keep interface uk.co.senab.photoview** { *; }#7.rxgalleryfinal -keep class cn.finalteam.rxgalleryfinal.ui.widget** { *; }-keepclassmembers class * extends android.app.Activity { public void *(android.view.View); } -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } -keepclassmembers class **.R$* { public static < fields> ; } #保护注解 -keepattributes *Annotation* -keepclasseswithmembernames class * { native < methods> ; } -keepclassmembers public class * extends android.view.View { void set*(***); *** get*(); }#Evebus混淆规则 -dontwarn de.greenrobot.event.** -keep class de.greenrobot.event.** { *; } -keepclassmembers class ** { public void onEvent(**); void onEvent*(**); }#litepal混淆规则 -dontwarn org.litepal.* -keep class org.litepal.** { *; } -keep enum org.litepal.** -keep interface org.litepal.** { *; } -keep public class * extends org.litepal.** -keepattributes *Annotation* -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); }-keepclassmembers class * extends org.litepal.crud.DataSupport{*; }#afinal 混淆规则 -dontwarn net.tsz.afinal.** -keep class net.tsz.afinal.** { *; } -keep public class * extends net.tsz.afinal.** -keep public interface net.tsz.afinal.** {*; } -keepattributes Signature -keepattributes *Annotation* # ---------------------- #不压缩指定的文件 #-dontshrink # ---------------------- -dontshrink

 

    推荐阅读