最近新寫了一個安卓項目,採用了java和kotlin混編,kotlin用起來感受還不錯,語法簡介。 項目初版快完工了,整了一下混淆配置,記錄一下。
1.build.gradle文件裏面的配置:javascript
release { // 混淆 minifyEnabled true // Zipalign優化 zipAlignEnabled true // 移除無用的resource文件 shrinkResources true //指定混淆文件 proguardFiles 'proguard-rules.pro' applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { // String name = variant.productFlavors[0].name; // 下面的appname改成你本身的app name def fileName = "MobileLibrary_${defaultConfig.versionName}.apk" output.outputFile = new File(outputFile.parent, fileName) } } } }
2.混淆文件配置:java
#-------------------------------------------定製化區域---------------------------------------------- #---------------------------------1.實體類--------------------------------- -keep class com.yuntu.mobilelibrary.sdk.vo.** {*;} -keep class com.yuntu.mobilelibrary.vo.** {*;} #------------------------------------------------------------------------- #---------------------------------2.第三方包------------------------------- #EventBus -keepattributes *Annotation* -keepclassmembers class ** { @org.greenrobot.eventbus.Subscribe <methods>; } -keep enum org.greenrobot.eventbus.ThreadMode { *; } #Glide -dontwarn com.bumptech.glide.** -keep public class * implements com.bumptech.glide.module.GlideModule -keep public class * extends com.bumptech.glide.AppGlideModule -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** { **[] $VALUES; public *; } # banner 的混淆代碼 -keep class com.youth.banner.** { *; } # OkHttp3 -dontwarn okhttp3.logging.** -keep class okhttp3.internal.**{*;} -dontwarn okio.** -dontwarn com.squareup.okhttp3.** -keep class com.squareup.okhttp3.** { *;} # Retrofit -dontwarn retrofit2.** -keep class retrofit2.** { *; } #本身寫的api列表,這裏必定要加上,否則使用Retrofit會報400 -keep class com.yuntu.mobilelibrary.sdk.api.ApiService {*;} # RxJava RxAndroid -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.與js互相調用的類------------------------ -keep class com.yuntu.mobilelibrary.ui.js.** { *; } #------------------------------------------------------------------------- #---------------------------------4.反射相關的類和方法----------------------- #---------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------- #-------------------------------------------基本不用動區域-------------------------------------------- #---------------------------------基本指令區---------------------------------- -optimizationpasses 5 -dontusemixedcaseclassnames -dontskipnonpubliclibraryclasses -dontskipnonpubliclibraryclassmembers -dontpreverify -verbose -printmapping proguardMapping.txt -optimizations !code/simplification/cast,!field/*,!class/merging/* -keepattributes *Annotation*,InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable #忽略警告,這一行挺重要,不加的話某些狀況下打包會報錯中斷 -ignorewarnings #---------------------------------------------------------------------------- #---------------------------------默認保留區--------------------------------- -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 * extends android.view.View -keep public class com.android.vending.licensing.ILicensingService -keep class android.support.** {*;} -keep class com.google.**{*;} # 保留繼承的 -keep public class * extends android.support.v4.** -keep public class * extends android.support.v7.** -keep public class * extends android.support.annotation.** -keepclasseswithmembernames class * { native <methods>; } -keepclassmembers class * extends android.app.Activity{ public void *(android.view.View); } -keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String); } -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); } -keepclasseswithmembers class * { public <init>(android.content.Context, android.util.AttributeSet); public <init>(android.content.Context, android.util.AttributeSet, int); } -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } -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(); } -keep class **.R$* { *; } -keepclassmembers class * { void *(**On*Event); } # 保留本地native方法不被混淆 -keepclasseswithmembernames class * { native <methods>; } #---------------------------------------------------------------------------- #---------------------------------webview------------------------------------ -keepclassmembers class fqcn.of.javascript.interface.for.Webview { public *; } -keepclassmembers class * extends android.webkit.WebViewClient { public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap); public boolean *(android.webkit.WebView, java.lang.String); } -keepclassmembers class * extends android.webkit.WebViewClient { public void *(android.webkit.WebView, jav.lang.String); }
混淆文件配置參考了簡書上的一篇5分鐘搞定android混淆android