代码混淆方法:
1.添加proguard-project.txt文件至app下目录
2.在该文件中添加要不混淆的代码,Gson、butterknife和eventbus等第三方包,除了将jar包中的包下类不混淆外,还有一些特殊的方法,也要保证不混淆,如下:
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
#先不混淆一些继承自带控件的view
-keep public class * extends android.app.Fragment
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.**
-keep public class com.android.vending.licensing.ILicensingService
-keep class com.kingnew.tian.model.** { *; }
-keep class com.kingnew.tian.Cropcategorys.Model.** { *; }
#-optimizationpasses 5 # 指定代码的压缩级别
#-dontusemixedcaseclassnames # 是否使用大小写混合
#-keepattributes *Annotation*
#-ignorewarnings # 抑制警告
#-dontskipnonpubliclibraryclasses # 是否混淆第三方jar
#不混淆Gson
##---------------Begin: proguard configuration for Gson ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
##---------------End: proguard configuration for Gson ----------
#不混淆butterknife
##---------------Begin: proguard configuration for butterknife ----------
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-dontwarn butterknife.**
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
##---------------End: proguard configuration for butterknife ----------
#不混淆eventbus
##---------------Begin: proguard configuration for eventbus ----------
-keep class org.greenrobot.eventbus.** { *; }
-dontwarn org.greenrobot.eventbus.**
-keepclassmembers class ** {
public void onEvent*(**);
void onEvent*(**);
}
##---------------Begin: proguard configuration for eventbus ----------
3.在app的build文件中,将minifyEnabled 置为true
buildTypes {
release {
minifyEnabled true/*minifyEnabled主要用来控制是否运行混淆的。*/
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
}
}
4.然后在签名打包之后,保证app能够正常运行
ps:可参考该文:https://segmentfault.com/a/1190000002910305
5. 需要做混淆保护的内容
一般四大组件都会默认不被混淆
-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
自定义组件的类也要设置为不被混淆,否则找不到控件
方案一、直接保护view的子类,因为自定义控件都是继承view
-keep public class * extends android.app.Dialog
-keep public class * extends android.view
方案二、具体保护某个自定义控件
-keep public class com.viewpagerindicator.TitlePageIndicator
被调用的第三方jar包的类等
如果你使用了第三方的包,你需要使用一下配置,让ProGuard知道库中的一些类并不是在所有的API中可用:
-libraryjars libs/roboguice-2.0.jar
-dontwarn roboguice.**
7.需要特别处理的第三方包
GSON
butterknife
eventbus