混淆配置

一、应用中使用混淆

1.创建一个混淆配置文件,命名为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 ;
}
-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.tabButton.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 *;
}

-keepattributes Signature
-ignorewarnings


-keepattributes *Annotation*
//第三方库不做混淆
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe ;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    (java.lang.Throwable);
}
-dontwarn org.greenrobot.eventbus.**
-keep class org.greenrobot.eventbus.** { *;}

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}
//腾讯Bugly
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}

-dontwarn com.prtek.**
-keep public class com.prtek.**{*;}
//腾讯云统计
-keep class com.tencent.stat.*{*;}

-keep class com.tencent.mid.*{*;}

2.在应用模块build.gradle下配置混淆文件路径,启用代码压缩 minifyEnabled true

android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}

二、库项目配置混淆

1.和应用项目第一步一致

2.要指定库模块的配置文件名称,请将其添加到 consumerProguardFiles 方法中,此方法位于库模块的 build.gradle 文件的 defaultConfig 块内。

android {
    defaultConfig {
        consumerProguardFiles 'proguard-rules.pro'
    }
    ...
}

三、混淆问题

1.将library项目改成App项目时,记得检查一下build.gradle 文件,出问题先按着这两个步骤来排查

2.混淆过apk如何追踪问题及查看已移除的文件和mapping文件

你可能感兴趣的:(开发笔记)