Fresco 源码解析 - 利用 @DoNotSkip 来防止混淆
来源:互联网 发布:php 面相过程 编辑:程序博客网 时间:2024/06/04 18:26
我们都知道,如果打开了混淆开关,代码 release 阶段会根据 proguard 规则进行混淆,但是有些实体类(例如 json 字符串对应的 model)需要进行序列化、反序列化,而序列化工具(例如 Gson、fastjson)是利用反射来一一对应 json 串的 key 和实体类的成员变量。
例如,我们定义一个 POJO 类型的 User 实体类:
public class User { public String firstName; public String lastName; public int age;}
那么对应的 json 串内容如下:
{ "firstName":"fei", "lastName":"liangfei", "age":28}
如果 User 类被混淆掉(成员变量的可能因为没有被用到而被删除或者名字改掉),那序列化工具就无能为力了,因为找不到 json key 与成员变量的对应关系(例如 User.firstName => “firstName”)。
一般的做法会在 proguard 文件中 keep 住 User
的成员变量:
-keepclassmembernames User.**
但是当多团队合作,代码分散在不同 project 时,每次添加一个实体类还需要修改 proguard 文件,如果多个人同时修改,很可能产生冲突,所以我们必须要想一个一劳永逸的办法。
方法肯定有很多个,Fresco 利用 Annotation - @DoNotSkip
的做法还是比较巧妙的。
首先定义 @DoNotSkip
(注释里详细说明了功能):
/** * Add this annotation to a class, method, or field to instruct Proguard to not strip it out. * * This is useful for methods called via reflection that could appear as unused to Proguard. */@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR })@Retention(CLASS)public @interface DoNotStrip {}
然后在 proguard 文件中添加如下规则:
# Keep our interfaces so they can be used by other ProGuard rules.# See http://sourceforge.net/p/proguard/bugs/466/-keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip# Do not strip any method/class that is annotated with @DoNotStrip-keep @com.facebook.common.internal.DoNotStrip class *-keepclassmembers class * { @com.facebook.common.internal.DoNotStrip *;}
这样一来,我们只要给 User
加上 @DoNotSkip
就不会被混淆掉了。
@DoNotSkippublic class User { }
由 @DoNotSkip
的定义可以知道它的 target:
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR })
所以,除了类(TYPE)我们还可以把 @DoNotSkip
用于 成员变量(FIELD)、方法(METHOD) 和构造方法(CONSTCTUCTOR)。
@Keep
其实,我们可以不用定义这个 annotation,因为 Android support-annotations 包提供了一个与 @DoNotSkip
类似的 annotation - @Keep
, 但是现阶段我们仍然需要在 proguard 中一次性添加规则,将来可能就就不需要了。
We’ve also added @Keep to the support annotations. Note however that that annotation hasn’t been hooked up to the Gradle plugin yet (though it’s in progress.) When finished this will let you annotate methods and classes that should be retained when minimizing the app.
- Fresco 源码解析 - 利用 @DoNotSkip 来防止混淆
- Fresco源码解析 - DraweeView
- Fresco源码解析 - 本地编译
- Android 6.0 利用Fresco解析gif图片
- Fresco源码解析 - Hierarchy / View / Controller
- Fresco源码解析 - 初始化过程分析
- Fresco源码解析 - DataSource怎样存储数据
- Fresco源码解析 - DataSource怎样存储数据
- Android Studio 利用Gradle来混淆代码
- Fresco解析
- 如何混淆电子邮件来防止垃圾发送者
- 利用token来进行防止重复提交
- 防止应用程序被反编译的译源码混淆控件Dotfuscator
- Android代码混淆防止反编译APK获取源码
- Fresco源码解析 - 创建一个ImagePipeline(一)
- Fresco源码解析 - 创建一个ImagePipeline(一)
- 利用SgmlReader来解析HTML
- 利用python来解析html
- Unity 画图效果
- 使用JDK1.6的JAX-WS编写WebService(转)
- Android百度定位API使用方法
- 一个绚丽的loading动效分析与实现!
- POST提交方式中文乱码的解决方案
- Fresco 源码解析 - 利用 @DoNotSkip 来防止混淆
- Java -- JavaBean,POJO,VO,DTO的区别和联系
- iOS-本地换肤思路
- linux基础目录结构
- 生成带有rgb值的pcd文件并显示
- 文章标题
- Android开发 如何快速实现分享功能
- Oracle V$SESSION详解
- Android开发技术学习之popupwindow的弹窗实现