当前位置:   article > 正文

Android项目混淆配置_# uncomment this to preserve the line number infor

# uncomment this to preserve the line number information for # debugging sta

proguard-rules.pro文件中配置

  1. # Add project specific ProGuard rules here.
  2. # By default, the flags in this file are appended to flags specified
  3. # in D:\work\Develop_tool\android-sdk/tools/proguard/proguard-android.txt
  4. # You can edit the include path and order by changing the proguardFiles
  5. # directive in build.gradle.
  6. #
  7. # For more details, see
  8. # http://developer.android.com/guide/developing/tools/proguard.html
  9. # Add any project specific keep options here:
  10. # If your project uses WebView with JS, uncomment the following
  11. # and specify the fully qualified class name to the JavaScript interface
  12. # class:
  13. #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
  14. # public *;
  15. #}
  16. # Uncomment this to preserve the line number information for
  17. # debugging stack traces.
  18. #-keepattributes SourceFile,LineNumberTable
  19. # If you keep the line number information, uncomment this to
  20. # hide the original source file name.
  21. #-renamesourcefileattribute SourceFile
  22. # Add project specific ProGuard rules here.
  23. # By default, the flags in this file are appended to flags specified
  24. # in D:\work\Develop_tool\android-sdk/tools/proguard/proguard-android.txt
  25. # You can edit the include path and order by changing the proguardFiles
  26. # directive in build.gradle.
  27. #
  28. # For more details, see
  29. # http://developer.android.com/guide/developing/tools/proguard.html
  30. # Add any project specific keep options here:
  31. # If your project uses WebView with JS, uncomment the following
  32. # and specify the fully qualified class name to the JavaScript interface
  33. # class:
  34. #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
  35. # public *;
  36. #}
  37. # Uncomment this to preserve the line number information for
  38. # debugging stack traces.
  39. #-keepattributes SourceFile,LineNumberTable
  40. # If you keep the line number information, uncomment this to
  41. # hide the original source file name.
  42. -renamesourcefileattribute SourceFile
  43. #忽略警告
  44. -ignorewarning
  45. #指定代码的压缩级别,在0~7之间,默认为5,一般不下需要修改
  46. -optimizationpasses 5
  47. #包名不混合大小写,混淆后的类名为小写
  48. # windows下的同学还是加入这个选项吧(windows大小写不敏感)
  49. -dontusemixedcaseclassnames
  50. #不去忽略非公共的库类
  51. # 默认跳过,有些情况下编写的代码与类库中的类在同一个包下,并且持有包中内容的引用,此时就需要加入此条声明
  52. -dontskipnonpubliclibraryclasses
  53. # 指定不去忽略非公共的库的类的成员
  54. #-dontskipnonpubliclibraryclassmembers
  55. #不进行压缩,保证是独立的jar,没有任何项目引用,如果不写就会认为我们所有的代码时无用的,
  56. #从而把所有的代码压缩掉,导出一个空的jar
  57. #库的aar或者jar必须要加上这个
  58. #-dontshrink
  59. #优化 不优化输入的类文件
  60. -dontoptimize
  61. #预校验,不做预检验,preverify是proguard的四个步骤之一
  62. # Android不需要preverify,去掉这一步可以加快混淆速度
  63. -dontpreverify
  64. #把混淆类中的方法名也混淆了
  65. #-useuniqueclassmembernames
  66. #优化时允许访问并修改有修饰符的类和类的成员
  67. #-allowaccessmodification
  68. #混淆时是否记录日志
  69. # 有了verbose这句话,混淆后就会生成映射文件
  70. # 包含有类名->混淆后类名的映射关系
  71. # 然后使用printmapping指定映射文件的名称
  72. -verbose
  73. # 混淆时所采用的算法,后面的参数是一个过滤器
  74. # 这个过滤器是谷歌推荐的算法,一般不改变
  75. -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
  76. #指定受保护的属性
  77. # 可以直接写在一行
  78. -keepattributes Exceptions, InnerClasses, Signature, Deprecated,SourceFile, LineNumberTable, *Annotation*, EnclosingMethod,*JavascriptInterface*
  79. #指定受保护的属性
  80. #保护注解
  81. # 这在JSON实体映射时非常重要,比如fastJson
  82. #-keepattributes *Annotation*,InnerClasses
  83. # 抛出异常时保留代码行号
  84. #-keepattributes SourceFile,LineNumberTable
  85. # 保持哪些类不被混淆
  86. #因为这些子类有可能被外部调用
  87. -keep public class * extends android.app.Fragment
  88. -keep public class * extends android.app.Activity
  89. -keep public class * extends android.app.Application
  90. -keep public class * extends android.app.Service
  91. -keep public class * extends android.content.BroadcastReceiver
  92. -keep public class * extends android.content.ContentProvider
  93. -keep public class * extends android.app.backup.BackupAgentHelper
  94. -keep public class * extends android.preference.Preference
  95. -keep public class com.android.vending.licensing.ILicensingService
  96. #如果有引用v4包可以添加下面这行
  97. -keep public class * extends android.support.v4.app.Fragment
  98. -keep public class * extends android.support.annotation.**
  99. -keep public class * extends android.support.v7.**
  100. # 保留Activity中的方法参数是view的方法,
  101. # 从而我们在layout里面编写onClick就不会影响
  102. -keepclassmembers class * extends android.app.Activity {
  103. public void * (android.view.View);
  104. }
  105. -keep public class * extends android.widget.BaseAdapter {*;}
  106. ##记录生成的日志数据,gradle build时在本项目根目录输出##
  107. #apk 包内所有 class 的内部结构
  108. -dump proguard/class_files.txt
  109. #未混淆的类和成员
  110. -printseeds proguard/seeds.txt
  111. #列出从 apk 中删除的代码
  112. -printusage proguard/unused.txt
  113. #混淆前后的映射
  114. -printmapping proguard/mapping.txt
  115. ########记录生成的日志数据,gradle build时 在本项目根目录输出-end######
  116. #如果引用了v4或者v7包
  117. -dontwarn android.support.**
  118. ####混淆保护自己项目的部分代码以及引用的第三方jar包library-end####
  119. #保持 native 方法不被混淆
  120. -keepclasseswithmembernames class * {
  121. native <methods>;
  122. }
  123. #保持自定义控件类不被混淆
  124. -keepclasseswithmembers class * {
  125. public <init>(android.content.Context, android.util.AttributeSet);
  126. }
  127. #保持自定义控件类(继承自View)不被混淆
  128. -keep public class * extends android.view.View {
  129. public <init>(android.content.Context);
  130. public <init>(android.content.Context, android.util.AttributeSet);
  131. public <init>(android.content.Context, android.util.AttributeSet, int);
  132. public void set*(...);
  133. *** get* ();
  134. }
  135. #WebView的处理
  136. -keepclassmembers class * extends android.webkit.WebViewClient {
  137. public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
  138. public boolean *(android.webkit.WebView, java.lang.String);
  139. }
  140. -keepclassmembers class * extends android.webkit.WebViewClient {
  141. public void *(android.webkit.WebView, java.lang.String);
  142. }
  143. #保持 Parcelable 不被混淆
  144. -keep class * implements android.os.Parcelable {
  145. public static final android.os.Parcelable$Creator *;
  146. }
  147. #保持 Serializable 不被混淆
  148. -keepnames class * implements java.io.Serializable
  149. #保持 Serializable 不被混淆并且enum 类也不被混淆
  150. -keepclassmembers class * implements java.io.Serializable {
  151. static final long serialVersionUID;
  152. private static final java.io.ObjectStreamField[] serialPersistentFields;
  153. !static !transient <fields>;
  154. !private <fields>;
  155. !private <methods>;
  156. private void writeObject(java.io.ObjectOutputStream);
  157. private void readObject(java.io.ObjectInputStream);
  158. java.lang.Object writeReplace();
  159. java.lang.Object readResolve();
  160. }
  161. #保持枚举 enum 类不被混淆
  162. -keepclassmembers enum * {
  163. public static **[] values();
  164. public static ** valueOf(java.lang.String);
  165. }
  166. #不混淆资源类,对R文件下的所有类及其方法,都不能被混淆
  167. -keepclassmembers class **.R$* {
  168. public static <fields>;
  169. }
  170. # 保持测试相关的代码
  171. -dontnote junit.framework.**
  172. -dontnote junit.runner.**
  173. -dontwarn android.test.**
  174. -dontwarn android.support.test.**
  175. -dontwarn org.junit.**
  176. #避免混淆泛型 如果混淆报错建议关掉
  177. # 这在JSON实体映射时非常重要,比如fastJson
  178. #-keepattributes Signature
  179. #移除Log类打印各个等级日志的代码,打正式包的时候可以做为禁log使用,这里可以作为禁止log打印的功能使用,另外的一种实现方案是通过BuildConfig.DEBUG的变量来控制
  180. #-assumenosideeffects class android.util.Log {
  181. # public static *** v(...);
  182. # public static *** i(...);
  183. # public static *** d(...);
  184. # public static *** w(...);
  185. # public static *** e(...);
  186. #}
  187. #############################################################################################
  188. ######################## 以上通用 ##################################
  189. #############################################################################################
  190. ########>>>>>>>>>>>>>>>>>>> 以下常用第三方模块的混淆选项 <<<<<<<<<<<<<<<<<##############
  191. #############GSON#############
  192. #https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg
  193. #如果用用到Gson解析包的,直接添加下面这几行就能成功混淆,不然会报错。
  194. #gson
  195. #如果用用到Gson解析包的,直接添加下面这几行就能成功混淆,不然会报错。
  196. -keepattributes Signature
  197. # Gson specific classes
  198. -dontwarn sun.misc.**
  199. -keep class sun.misc.Unsafe { *; }
  200. # Application classes that will be serialized/deserialized over Gson
  201. -dontwarn com.google.gson.**
  202. -keep class com.google.gson.** { *; }
  203. -keep class com.google.gson.stream.** { *; }
  204. # Prevent proguard from stripping interface information from TypeAdapterFactory,
  205. # JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
  206. -keep class * implements com.google.gson.TypeAdapterFactory
  207. -keep class * implements com.google.gson.JsonSerializer
  208. -keep class * implements com.google.gson.JsonDeserializer
  209. ##---------------End: proguard configuration for Gson ----------
  210. #############mob#############
  211. -keep class android.net.http.SslError
  212. -keep class android.webkit.**{*;}
  213. -keep class cn.sharesdk.**{*;}
  214. -keep class com.sina.**{*;}
  215. -keep class m.framework.**{*;}
  216. -keep class **.R$* {*;}
  217. -keep class **.R{*;}
  218. #忽略警告,保持cn.sharesdk.**这个包里面的所有类和所有方法不被混淆。
  219. -dontwarn cn.sharesdk.**
  220. -dontwarn com.sina.**
  221. -dontwarn **.R$*
  222. #############butterknife#############
  223. #https://github.com/JakeWharton/butterknife/blob/master/butterknife/proguard-rules.txt
  224. -keep class butterknife.** { *; }
  225. -dontwarn butterknife.internal.**
  226. -keep class **$$ViewBinder { *; }
  227. -keepclasseswithmembernames class * {
  228. @butterknife.* <fields>;
  229. }
  230. -keepclasseswithmembernames class * {
  231. @butterknife.* <methods>;
  232. }
  233. # Retain generated class which implement Unbinder.
  234. -keep public class * implements butterknife.Unbinder { public <init>(**, android.view.View); }
  235. #############OKHTTP3#############
  236. #https://github.com/square/okhttp
  237. -dontwarn okhttp3.**
  238. -dontwarn com.squareup.okhttp3.**
  239. -keep class com.squareup.okhttp3.** { *;}
  240. -keep class okhttp3.**{*;}
  241. -dontwarn okio.**
  242. -keep class okio.**{*;}
  243. -dontwarn javax.annotation.**
  244. # A resource is loaded with a relative path so the package of this class must be preserved.
  245. -keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
  246. #############AndroidUtilCode#############
  247. #https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/README-CN.md
  248. -keep class com.blankj.utilcode.** { *; }
  249. -keepclassmembers class com.blankj.utilcode.** { *; }
  250. -dontwarn com.blankj.utilcode.**
  251. #############greenDAO3#############
  252. #http://greenrobot.org/greendao/documentation/updating-to-greendao-3-and-annotations/
  253. -dontwarn org.greenrobot.greendao.**
  254. -keep class org.greenrobot.greendao.**{*;}
  255. -keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
  256. public static java.lang.String TABLENAME;
  257. }
  258. #-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
  259. #public static void dropTable(org.greenrobot.greendao.database.Database, boolean);
  260. #public static void createTable(org.greenrobot.greendao.database.Database, boolean);
  261. #}
  262. -keep class **$Properties
  263. # If you do not use SQLCipher:
  264. -dontwarn org.greenrobot.greendao.database.**
  265. # If you do not use Rx:
  266. -dontwarn rx.**
  267. #############RxJava RxAndroid#############
  268. -dontwarn sun.misc.**
  269. -dontwarn rx.internal.util.unsafe.**
  270. -keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
  271. long producerIndex;
  272. long consumerIndex;
  273. }
  274. -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
  275. rx.internal.util.atomic.LinkedQueueNode producerNode;
  276. }
  277. -keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
  278. rx.internal.util.atomic.LinkedQueueNode consumerNode;
  279. }
  280. -dontnote rx.internal.util.PlatformDependent
  281. #############Glide#############
  282. #https://github.com/bumptech/glide
  283. -dontwarn com.bumptech.glide.**
  284. -keep public class * implements com.bumptech.glide.module.GlideModule
  285. -keep public class * extends com.bumptech.glide.module.AppGlideModule
  286. -keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  287. **[] $VALUES;
  288. public *;
  289. }
  290. #############EventBus#############
  291. #http://greenrobot.org/eventbus/documentation/proguard
  292. -keepclassmembers class ** {
  293. @org.greenrobot.eventbus.Subscribe <methods>;
  294. }
  295. -keep enum org.greenrobot.eventbus.ThreadMode { *; }
  296. # Only required if you use AsyncExecutor
  297. -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
  298. <init>(java.lang.Throwable);
  299. }
  300. # compile 'org.greenrobot:eventbus:3.0.0'
  301. -keep class org.greenrobot.eventbus.** { *; }
  302. -keep interface org.greenrobot.eventbus.** { *; }
  303. -dontwarn org.greenrobot.eventbus.**
  304. #############Retrofit#############
  305. #https://github.com/square/retrofit
  306. -dontwarn retrofit2.**
  307. -keep class retrofit2.** { *; }
  308. -keepattributes Signature
  309. -keepattributes Exceptions
  310. -dontwarn okio.**
  311. -dontwarn javax.annotation.**
  312. #############################################################################################
  313. ######################## 以上第三方 ###############################
  314. #############################################################################################
  315. ######引用的其他Module可以直接在app的这个混淆文件里配置
  316. #####混淆保护自己项目的部分代码以及引用的第三方jar包library#######
  317. #如果在当前的application module或者依赖的library module中使用了第三方的库,并不需要显式添加规则
  318. #-libraryjars xxx
  319. #添加了反而有可能在打包的时候遭遇同一个jar多次被指定的错误,一般只需要添加忽略警告和保持某些class不被混淆的声明。
  320. #以libaray的形式引用了开源项目,如果不想混淆 keep 掉,在引入的module的build.gradle中设置minifyEnabled=false
  321. #-keep class com.nineoldandroids.** { *; }
  322. #-keep interface com.nineoldandroids.** { *; }
  323. #-dontwarn com.nineoldandroids.**
  324. ## 下拉刷新
  325. #-keep class in.srain.cube.** { *; }
  326. #-keep interface in.srain.cube.** { *; }
  327. #-dontwarn in.srain.cube.**
  328. ## observablescrollview:tab fragment
  329. #-keep class com.github.ksoichiro.** { *; }
  330. #-keep interface com.github.ksoichiro.** { *; }
  331. #-dontwarn com.github.ksoichiro.**
  332. #保护引用的第三方jar包不被混淆,在使用Eclipse+ADT时需要加入-libraryjars
  333. #-libraryjars libs/baidumapapi_v3_2_0.jar
  334. #############################################################################################
  335. ######################## 以下自己项目 #############################
  336. #############################################################################################
  337. #用到GSON解析的java Bean类不能混淆
  338. #如果使用了Gson之类的工具要使被它解析的JavaBean类即实体类不被混淆。
  339. # Application classes that will be serialized/deserialized over Gson
  340. #例如 -keep class 包名.** { *; }
  341. #例如 -keep class com.matrix.app.entity.json.** { *; }
  342. #例如 -keep class com.matrix.appsdk.network.model.** { *; }
  343. -keep class **.bean.** { *; }
  344. #在不想混淆的方法、成员变量、类上使用@Keep注解
  345. -keepclasseswithmembers class * {
  346. @android.support.annotation.Keep <methods>;
  347. }
  348. -keepclasseswithmembers class * {
  349. @android.support.annotation.Keep <fields>;
  350. }
  351. -keepclasseswithmembers class * {
  352. @android.support.annotation.Keep <init>(...);
  353. }

 build.gradle文件中配置

  1. buildTypes {
  2. release {
  3. minifyEnabled false
  4. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  5. }
  6. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/285520
推荐阅读
相关标签
  

闽ICP备14008679号