房价背后:通过注解完美解决混淆问题

混淆是代码安全一个很有效的措施,防止代码在市场中裸奔,Androidstudio通过写proguard-rules.pro文件来混淆,mainfest中的类不混淆,四大组件和Application的子类和Framework层下所有的类默认不会进行混淆,对于四大组件是不可以混淆的,之前还有人和我争论过,manifest中需要的是配置完整路径,一旦混淆了,路径或者名称就变了,怎么还能找到?

第三方会提供防混淆代码,避免必要的包和类被混淆掉,但是对于一些日常开发中使用proguard-rules.pro文件来混淆会有一下弊端:

灵活性差,需要绝对路径

不便于扩展,每次都需要去proguard-rules.pro文件下配置

冗余太多,比如一个包下面部分类不需要混淆,这就造成了一堆混淆代码

既然混淆文件中可以保留指定的注解,那何不使用注解来解决上面所述的一大堆问题呢?代码量很少,却很有用,如下:

/**

* 可配置类,方法,属性,配置后,将不会被混淆

* Created by apple on 2017/8/3.

*/@Retention(RetentionPolicy.CLASS)@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})public@interfaceKeepNotProguard{}

1

2

3

4

5

6

7

8

9

1

2

3

4

5

6

7

8

9

proguard-rules.pro文件下需要如下配置:

-keep @com.zero.framework.annotation.KeepNotProguardclass * {*;}-keep class * {    @com.zero.framework.annotation.KeepNotProguard;}-keepclassmembers class * {    @com.zero.framework.annotation.KeepNotProguard;}

1

2

3

4

5

6

7

1

2

3

4

5

6

7

gradle文件如下:

buildTypes {        release {            minifyEnabledtrueproguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'}    }

1

2

3

4

5

6

1

2

3

4

5

6

默认的minifyEnabled是false,改为true才会进行混淆。

下面做一组对比,如下:

/**

* Created by Zero on 2017/5/31.

*/publicclassTestPresenterextendsBasePresenter{privateITest iView;privateinttest = -1;@OverrideprotectedvoidinitBaseData(Context context, Handler handler, BaseInterface iView, Intent intent) {if(iView!=null) {if(iViewinstanceofITest) {this.iView = (ITest) iView;            }        }    }@OverrideprotectedvoidhandMsg(Message msg) {    }publicvoidcommit(){    }publicvoidgetCommit() {    }}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

在需要使用的地方只需要一个注解就可以搞定,示例如下:

/**

* Created by Zero on 2017/5/31.

*/publicclassTestPresenterextendsBasePresenter{@KeepNotProguardprivateITest iView;privateinttest = -1;@Override@KeepNotProguardprotectedvoidinitBaseData(Context context, Handler handler, BaseInterface iView, Intent intent) {if(iView!=null) {if(iViewinstanceofITest) {this.iView = (ITest) iView;            }        }    }@Override@KeepNotProguardprotectedvoidhandMsg(Message msg) {    }publicvoidcommit(){    }publicvoidgetCommit() {    }}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

PS:以上只是一个示例,在类上加了@KeepNotProguard整个类便不会被混淆。

推荐阅读更多精彩内容

  • 声明 这篇文章更多的是做一个整理,内容来自于ProGuard官方文档以及各种博客等,相关文章的链接在参考目录里,感...
    夷陵小祖阅读 3,372评论 0 23
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 168,574评论 25 707
  • 澡澡时光,麻麻正在给丫头洗头发,手指不小心划过丫头的小脸。许是有些碰疼了,又不至痛极,丫头将哭不哭的小表情煞是可爱...
    小雎阅读 83评论 0 0
  • 六月十四小暑日归家 文|子丘 旬月东归去,田园草已深。 时花迎旧主,老狗戏家禽。 暂托鹪鹩志,将离蚌鹬心。 夜来蛩...
    子丘1987阅读 196评论 6 7
  • 猪小妹从小就是个胖娃娃,街坊邻居每当看见她都会很亲切的喊“胖妮儿”想要以玩笑的形式来逗趣生活,然而他们不知道的是,...
    阿俊xi阅读 109评论 0 1