第一次android混淆实战
第一次混淆,主要是因为引用本地第三方jar包的问题。虽然说本地第三方jar包自动避免混淆,但一些本地第三方jar包下的一些包要避免混淆。比如:
文中的com、org 这些包名都要避免混淆。
下面是我用到的混淆代码:
1 # Add project specific ProGuard rules here. 2 # By default, the flags in this file are appended to flags specified 3 # in /Users/firefish/Library/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 10 # Add any project specific keep options here: 11 12 # If your project uses WebView with JS, uncomment the following 13 # and specify the fully qualified class name to the JavaScript interface 14 # class: 15 #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 # public *; 17 #} 18 19 -keep class de.greenrobot.event.** {*;} 20 -keepclassmembers class ** { 21 public void onEvent*(**); 22 void onEvent*(**); 23 } 24 -keepclassmembers class fqcn.of.javascript.interface.for.webview { 25 public *; 26 } 27 28 #指定代码的压缩级别 29 -optimizationpasses 5 30 31 #包明不混合大小写 32 -dontusemixedcaseclassnames 33 34 #不去忽略非公共的库类 35 -dontskipnonpubliclibraryclasses 36 37 #优化 不优化输入的类文件 38 -dontoptimize 39 40 #预校验 41 -dontpreverify 42 43 #混淆时是否记录日志 44 -verbose 45 46 # 混淆时所采用的算法 47 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 48 49 #保护注解 50 -keepattributes *Annotation* 51 52 # 保持哪些类不被混淆 53 -keep public class * extends android.app.Fragment 54 -keep public class * extends android.app.Activity 55 -keep public class * extends android.app.Application 56 -keep public class * extends android.app.Service 57 -keep public class * extends android.content.BroadcastReceiver 58 -keep public class * extends android.content.ContentProvider 59 -keep public class * extends android.app.backup.BackupAgentHelper 60 -keep public class * extends android.preference.Preference 61 -keep public class com.android.vending.licensing.ILicensingService 62 #如果有引用v4包可以添加下面这行 63 -keep public class * extends android.support.v4.app.Fragment 64 65 #忽略警告 66 -ignorewarning 67 68 ##记录生成的日志数据,gradle build时在本项目根目录输出## 69 #apk 包内所有 class 的内部结构 70 -dump proguard/class_files.txt 71 #未混淆的类和成员 72 -printseeds proguard/seeds.txt 73 #列出从 apk 中删除的代码 74 -printusage proguard/unused.txt 75 #混淆前后的映射 76 -printmapping proguard/mapping.txt 77 ########记录生成的日志数据,gradle build时 在本项目根目录输出-end###### 78 79 #如果引用了v4或者v7包 80 -dontwarn android.support.** 81 82 # support-v4 83 #https://stackoverflow.com/questions/18978706/obfuscate-android-support-v7-widget-gridlayout-issue 84 -dontwarn android.support.v4.** 85 -keep class android.support.v4.app.** { *; } 86 -keep interface android.support.v4.app.** { *; } 87 -keep class android.support.v4.** { *; } 88 89 #保留crash日志的行号 90 -keepattributes SourceFile,LineNumberTable 91 92 # support-v7 93 -dontwarn android.support.v7.** 94 -keep class android.support.v7.internal.** { *; } 95 -keep interface android.support.v7.internal.** { *; } 96 -keep class android.support.v7.** { *; } 97 98 # support design 99 #@link http://stackoverflow.com/a/31028536 100 -dontwarn android.support.design.** 101 -keep class android.support.design.** { *; } 102 -keep interface android.support.design.** { *; } 103 -keep public class android.support.design.R$* { *; } 104 105 ####混淆保护自己项目的部分代码以及引用的第三方jar包library-end#### 106 107 108 #保持 native 方法不被混淆 109 -keepclasseswithmembernames class * { 110 native <methods>; 111 } 112 113 #保持自定义控件类不被混淆 114 -keepclasseswithmembers class * { 115 public <init>(android.content.Context, android.util.AttributeSet); 116 } 117 118 #保持自定义控件类不被混淆 119 -keepclassmembers class * extends android.app.Activity { 120 public void *(android.view.View); 121 } 122 123 -keep public class * extends android.view.View { 124 public <init>(android.content.Context); 125 public <init>(android.content.Context, android.util.AttributeSet); 126 public <init>(android.content.Context, android.util.AttributeSet, int); 127 public void set*(...); 128 } 129 130 #保持 Parcelable 不被混淆 131 -keep class * implements android.os.Parcelable { 132 public static final android.os.Parcelable$Creator *; 133 } 134 135 #保持 Serializable 不被混淆 136 -keepnames class * implements java.io.Serializable 137 138 #保持 Serializable 不被混淆并且enum 类也不被混淆 139 -keepclassmembers class * implements java.io.Serializable { 140 static final long serialVersionUID; 141 private static final java.io.ObjectStreamField[] serialPersistentFields; 142 !static !transient <fields>; 143 !private <fields>; 144 !private <methods>; 145 private void writeObject(java.io.ObjectOutputStream); 146 private void readObject(java.io.ObjectInputStream); 147 java.lang.Object writeReplace(); 148 java.lang.Object readResolve(); 149 } 150 151 #保持枚举 enum 类不被混淆 152 -keepclassmembers enum * { 153 public static **[] values(); 154 public static ** valueOf(java.lang.String); 155 } 156 157 -keepclassmembers class * { 158 public void *ButtonClicked(android.view.View); 159 } 160 161 #不混淆资源类 162 -keepclassmembers class **.R$* { 163 public static <fields>; 164 } 165 166 #避免混淆泛型 如果混淆报错建议关掉 167 -keepattributes Signature 168 169 #移除Log类打印各个等级日志的代码,打正式包的时候可以做为禁log使用,这里可以作为禁止log打印的功能使用,另外的一种实现方案是通过BuildConfig.DEBUG的变量来控制 170 #-assumenosideeffects class android.util.Log { 171 # public static *** v(...); 172 # public static *** i(...); 173 # public static *** d(...); 174 # public static *** w(...); 175 # public static *** e(...); 176 #} 177 178 ############################################################################################# 179 ######################## 以上通用 ################################## 180 ############################################################################################# 181 182 ####################### 常用第三方模块的混淆选项 ################################### 183 #gson 184 #如果用用到Gson解析包的,直接添加下面这几行就能成功混淆,不然会报错。 185 -keepattributes Signature 186 # Gson specific classes 187 -keep class sun.misc.Unsafe { *; } 188 # Application classes that will be serialized/deserialized over Gson 189 -keep class com.google.gson.** { *; } 190 -keep class com.google.gson.stream.** { *; } 191 192 #mob 193 -keep class android.net.http.SslError 194 -keep class android.webkit.**{*;} 195 -keep class cn.sharesdk.**{*;} 196 -keep class com.sina.**{*;} 197 -keep class m.framework.**{*;} 198 -keep class **.R$* {*;} 199 -keep class **.R{*;} 200 -dontwarn cn.sharesdk.** 201 -dontwarn **.R$* 202 203 #butterknife 204 -keep class butterknife.** { *; } 205 -dontwarn butterknife.internal.** 206 -keep class **$$ViewBinder { *; } 207 208 -keepclasseswithmembernames class * { 209 @butterknife.* <fields>; 210 } 211 212 -keepclasseswithmembernames class * { 213 @butterknife.* <methods>; 214 } 215 216 ######引用的其他Module可以直接在app的这个混淆文件里配置 217 218 # 如果使用了Gson之类的工具要使被它解析的JavaBean类即实体类不被混淆。 219 -keep class com.matrix.app.entity.json.** { *; } 220 -keep class com.matrix.appsdk.network.model.** { *; } 221 222 #####混淆保护自己项目的部分代码以及引用的第三方jar包library####### 223 #如果在当前的application module或者依赖的library module中使用了第三方的库,并不需要显式添加规则 224 #-libraryjars xxx 225 #添加了反而有可能在打包的时候遭遇同一个jar多次被指定的错误,一般只需要添加忽略警告和保持某些class不被混淆的声明。 226 #以libaray的形式引用了开源项目,如果不想混淆 keep 掉,在引入的module的build.gradle中设置minifyEnabled=false 227 -keep class com.nineoldandroids.** { *; } 228 -keep interface com.nineoldandroids.** { *; } 229 -dontwarn com.nineoldandroids.** 230 # 下拉刷新 231 -keep class in.srain.cube.** { *; } 232 -keep interface in.srain.cube.** { *; } 233 -dontwarn in.srain.cube.** 234 #fab-transformation 235 #-keep class konifar.fab-transformation.** { *; } 236 #-keep interface konifar.fab-transformation.** { *; } 237 #-dontwarn konifar.fab-transformation.** 238 #info.hoang8f:android-segmented 239 -keep class info.hoang8f.android-segmented.** { *; } 240 -keep interface info.hoang8f.android-segmented.** { *; } 241 -dontwarn info.hoang8f.android-segmented.** 242 #xutils:3.3.36 243 ################### region for xUtils 244 -keepattributes Signature,*Annotation* 245 -keep public class org.xutils.** { 246 public protected *; 247 } 248 -keep public interface org.xutils.** { 249 public protected *; 250 } 251 -keepclassmembers class * extends org.xutils.** { 252 public protected *; 253 } 254 -keepclassmembers @org.xutils.db.annotation.* class * {*;} 255 -keepclassmembers @org.xutils.http.annotation.* class * {*;} 256 -keepclassmembers class * { 257 @org.xutils.view.annotation.Event <methods>; 258 } 259 #################### end region 260 #eventbus:3.0.0 261 -keepattributes *Annotation* 262 -keepclassmembers class ** { 263 @org.greenrobot.eventbus.Subscribe <methods>; 264 } 265 -keep enum org.greenrobot.eventbus.ThreadMode { *; } 266 267 # Only required if you use AsyncExecutor 268 -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent { 269 <init>(java.lang.Throwable); 270 } 271 # OkHttp3 272 -dontwarn com.squareup.okhttp3.** 273 -keep class com.squareup.okhttp3.** { *;} 274 -dontwarn okio.** 275 # Okio 276 -dontwarn com.squareup.** 277 -dontwarn okio.** 278 -keep public class org.codehaus.* { *; } 279 -keep public class java.nio.* { *; } 280 #com.getbase:floatingactionbutton 281 -keep class com.getbase.floatingactionbutton.** { *; } 282 -keep interface com.getbase.floatingactionbutton.** { *; } 283 -dontwarn com.getbase.floatingactionbutton.** 284 #com.orhanobut:logger:1.15 285 -keep class com.orhanobut.logger.** { *; } 286 -keep interface com.orhanobut.logger.** { *; } 287 -dontwarn com.com.orhanobut.logger.** 288 #com.zhy:flowlayout-lib:1.0.3 289 -keep class com.zhy.flowlayout-lib.** { *; } 290 -keep interface com.zhy.flowlayout-lib.** { *; } 291 -dontwarn com.zhy.flowlayout-lib.** 292 # observablescrollview:tab fragment 293 -keep class com.github.ksoichiro.** { *; } 294 -keep interface com.github.ksoichiro.** { *; } 295 -dontwarn com.github.ksoichiro.** 296 #greendao:3.2.2 297 -keep class org.greenrobot.greendao.**{*;} 298 -keepclassmembers class * extends org.greenrobot.greendao.AbstractDao { 299 public static java.lang.String TABLENAME; 300 } 301 -keep class **$Properties 302 ##---------------Begin: proguard configuration for fastjson ---------- 303 -keep public class * implements java.io.Serializable { 304 public *; 305 } 306 -keepclassmembers class * implements java.io.Serializable { 307 static final long serialVersionUID; 308 private static final java.io.ObjectStreamField[] serialPersistentFields; 309 private void writeObject(java.io.ObjectOutputStream); 310 private void readObject(java.io.ObjectInputStream); 311 java.lang.Object writeReplace(); 312 java.lang.Object readResolve(); 313 } 314 -dontwarn android.support.** 315 -dontwarn com.alibaba.fastjson.** 316 317 -dontskipnonpubliclibraryclassmembers 318 -dontskipnonpubliclibraryclasses 319 320 #-libraryjars libs/fastjson.jar 321 -keep class com.alibaba.fastjson.** { *; } 322 323 -keepclassmembers class * { 324 public <methods>; 325 } 326 ##---------------End: proguard configuration for fastjson ---------- 327 328 #-libraryjars libs/AnyOfficeSDK.jar 329 -dontwarn com.huawei.** 330 -keep class com.huawei.** { *; } 331 332 -dontwarn com.tencent.** 333 -keep class com.tencent.** { *; } 334 335 #-libraryjars libs/iAppPDF.jar 336 -dontwarn com.kinggrid.** 337 -keep class com.kinggrid.** { *; } 338 -dontwarn com.longmai.mtoken.k5.** 339 -keep class com.longmai.mtoken.k5.** { *; } 340 -dontwarn org.bouncycastle.** 341 -keep class org.bouncycastle.**{*;} 342 -dontwarn org.dom4j.** 343 -keep class org.dom4j.**{*;} 344 -dontwarn org.jaxen.** 345 -keep class org.jaxen.**{*;} 346 -dontwarn org.w3c.dom.** 347 -keep class org.w3c.dom.**{*;} 348 349 -keep class org.xmlpull.v1.** { *; }