在Android Studio中,使用混淆的方法如下:
//在buildTypes下指明混淆配置
buildTypes { release { // 不显示Log buildConfigField "boolean", "LOG_DEBUG", "false" //混淆 minifyEnabled true //Zipalign优化 zipAlignEnabled true // 移除无用的resource文件 shrinkResources true //前一部分代表系统默认的android程序的混淆文件,该文件已经包含了基本的混淆声明 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }
完整的build.gradle:
android { defaultConfig { applicationId "com.test.test" versionName = project.ext.getNewVersionName versionCode = project.ext.getNewVersionCode } buildTypes { release { // 不显示Log buildConfigField "boolean", "LOG_DEBUG", "false" //混淆 minifyEnabled true //Zipalign优化 zipAlignEnabled true // 移除无用的resource文件 shrinkResources true //前一部分代表系统默认的android程序的混淆文件,该文件已经包含了基本的混淆声明 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } packagingOptions { exclude 'META-INF/NOTICE.txt' exclude 'META-INF/LICENSE.txt' } }
其中,个性化的混淆配置主要在proguard-rules.pro中指明:
//google默认混淆文件: \sdk\tools\proguard\proguard-android.txt -keep public class com.google.vending.licensing.ILicensingService -keep public class com.android.vending.licensing.ILicensingService # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native -keepclasseswithmembernames class * {//指定不混淆所有的JNI方法 native <methods>; } # keep setters in Views so that animations can still work. # see http://proguard.sourceforge.net/manual/examples.html#beans -keepclassmembers public class * extends android.view.View {//所有View的子类及其子类的get、set方法都不进行混淆 void set*(***); *** get*(); } # We want to keep methods in Activity that could be used in the XML attribute onClick -keepclassmembers class * extends android.app.Activity {//不混淆Activity中参数类型为View的所有方法 public void *(android.view.View); } # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations -keepclassmembers enum * {//不混淆Enum类型的指定方法 public static **[] values(); public static ** valueOf(java.lang.String); } //不混淆Parcelable和它的子类,还有Creator成员变量 -keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; } //不混淆R类里及其所有内部static类中的所有static变量字段 -keepclassmembers class **.R$* { public static <fields>; } # The support library contains references to newer platform versions. # Don't warn about those in case this app is linking against an older # platform version. We know about them, and they are safe. -dontwarn android.support.**//不提示兼容库的错误警告
详细配置情况可查阅:http://blog.csdn.net/chen930724/article/details/49687067
转载于:https://blog.51cto.com/whatever957/1835895