欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android开发之android混淆问题解析

程序员文章站 2022-03-26 20:08:10
应用开发完了,测试没问题了,快上市场了,这时候就要混淆加固多渠道打包加上线了,所以,混淆就来了: 混淆是什么,就是把类名,方法名,变量名给弄成简单的单词或者字母来代替,从而防止别人反编译后轻松的得到...

应用开发完了,测试没问题了,快上市场了,这时候就要混淆加固多渠道打包加上线了,所以,混淆就来了:

混淆是什么,就是把类名,方法名,变量名给弄成简单的单词或者字母来代替,从而防止别人反编译后轻松的得到。与其说我们配置混淆,不如说我们来配置哪些包里面的哪些类不要被混淆,也就是在下面的那个文件里声明一下,哪些不要给我混淆:

1.在module的build.gradle的android标签里

buildtypes {
 release {
  signingconfig signingconfigs.release
  shrinkresources true
  minifyenabled true
  zipalignenabled true
  proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'

 }

 debug {
  signingconfig signingconfigs.release
  shrinkresources true
  minifyenabled true
  zipalignenabled true
  proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'
 }

}

2.在proguard-rules.pro里

-optimizationpasses 5 # 指定代码的压缩级别
-dontusemixedcaseclassnames# 是否使用大小写混合
-dontpreverify  # 混淆时是否做预校验
-verbose # 混淆时是否记录日志
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*  # 混淆时所采用的算法
#glide混淆
-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.imageheaderparser$** {
  **[] $values;
  public *;
}

# for dexguard only
#-keepresourcexmlelements manifest/application/meta-data@value=glidemodule

#okgo混淆
#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}

#okio
-dontwarn okio.**
-keep class okio.**{*;}

#腾讯bugly
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}

#butterknife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$viewbinder { *; }
-keepclasseswithmembernames class * {
 @butterknife.* ;
}
-keepclasseswithmembernames class * {
 @butterknife.* ;
}
## gson 2.2.4 specific rules ##
# 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
# for using gson @expose annotation
-keepattributes *annotation*
-keepattributes enclosingmethod
# gson specific classes
-dontwarn com.google.gson.**

#eventbus2.0
-keep class de.greenrobot.event.** {*;}
-keepclassmembers class ** {
 public void onevent*(**);
 void onevent*(**);
}

## new rules for eventbus 3.0.x ##
# https://greenrobot.org/eventbus/documentation/proguard/
-keepattributes *annotation*
-keepclassmembers class ** {
 @org.greenrobot.eventbus.subscribe ;
}
-keep enum org.greenrobot.eventbus.threadmode { *; }
# only required if you use asyncexecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.throwablefailureevent {
 (java.lang.throwable);
}

# fastjson proguard rules
# https://github.com/alibaba/fastjson
-dontwarn com.alibaba.fastjson.**
-keepattributes signature
-keepattributes *annotation*

#support-v7-appcompat
-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 (android.content.context);
}

#support-design
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.r$* { *; }

# 枚举也不要混淆also keep - enumerations. keep the special static methods that are required in
# enumeration classes.
-keepclassmembers enum  * {
 public static **[] values();
 public static ** valueof(java.lang.string);
}



#native方法不做混淆 keep names - native method names. keep all native class/method names.
-keepclasseswithmembers,allowshrinking class * {
 native ;
}
# 保持 parcelable 不被混淆
-keep class * implements android.os.parcelable {
 public static final android.os.parcelable$creator *;
}

#实体类不参与混淆
-keep class com.hejun.solidtext.model.** { *; }
 #不混淆r类
-keep public class com.jph.android.r$*{
 public static final int *;
}
这里面我也还有一些不是很明白,比如android的很多依赖,比如通过gradle配置的一些v4,v7包里面的依赖是不是也要配置不要被混淆呢?但是,我的应用小,做了上面的事情就够了。