笔者是面霸,面试200+场 当过考官:面过别人300+场 去过500强,也呆过初创公司。
关注我就能达到大师级水平,这话我终于敢说了, 年薪60万不是梦!
斩获腾讯、华为、oppo,VIVO,安卓岗offer!我有一套速通大厂技巧分享给你!
AOP介绍
Aspect Oriented Programming , 面向切面编程,优点如下。
(1)针对同一类问题的统一处理
(2)无侵入添加代码
(3) 运行在,,,,,,,但是注解在编译期间
1)在项目gradle中添加依赖
classpath 'com.android.tools.build:gradle:3.6.2' classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
2) 在app gradle中添加配置
apply plugin: 'android-aspectjx'
aspectjx { //排除所有package路径中包含`android.support`的class文件及库(jar文件) exclude 'android.support' }
3).类注解,方法注解,正则表达式。不要写错正则表达式
@Aspect public class PerformanceAop { @Around("call(* com.test.WorkManger.**(..))") public void getTime(ProceedingJoinPoint joinPoint) { Signature signature = joinPoint.getSignature(); String name = signature.toShortString(); long time = System.currentTimeMillis(); try { joinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } Log.i("PerformanceAop", name + " cost " + (System.currentTimeMillis() - time)); } }
public class WorkManger { public void start(){ try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }
2021-03-14 16:12:27.604 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2000
2021-03-14 16:12:29.618 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2003
2021-03-14 16:12:31.630 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2001
2021-03-14 16:12:33.633 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2002
2021-03-14 16:12:35.637 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2001
2021-03-14 16:12:37.643 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2002
2021-03-14 16:12:39.647 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2001
2021-03-14 16:12:41.651 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2001
2021-03-14 16:12:43.665 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2002
2021-03-14 16:12:45.669 18965-18965/com.test I/PerformanceAop: WorkManger.start() cost 2001
现在实行:某个类里面的方法耗时都可以抓到。如何把app里面的所有都抓到?
通过正则表达式。
列子二:在方法前增加一些
@Aspect public class AspectjxMine { @Pointcut("execution(* com.test.MainActivity.clickMe(..))") public void callMethod() { } @Around("callMethod()")// public void beforeMethodCall(ProceedingJoinPoint joinPoint) { try { Log.i("AspectjxMine", "callMethod before "); joinPoint.proceed(); Log.i("AspectjxMine", "callMethod after "); } catch (Throwable throwable) { throwable.printStackTrace(); } } }
demo地址;https://github.com/pengcaihua123456/shennandadao/tree/master/AspectJx
总结:
1.配置可能会有问题
2.加入,打点写错了,编译都编译不过去的。
3.总是要删除.gradle文件缓存
集成的时候总是报各种错误。mmd
我的Android Studio 版本 3.6.2,如果代码不生效很有可能是Android Studio版本和aspectjx版本不兼容的问题
理解:提取公共的方法。
优点:解耦
举例:
实际生活中的例子:
优点:
(1)代码无侵入性,
(2)方便修改;
有demo
https://blog.csdn.net/LosingCarryJie/article/details/106195627
耗时打印:(有问题)
https://zhuanlan.zhihu.com/p/102627869
https://blog.csdn.net/BunnyCoffer/article/details/108102916
无痕埋点:
登入理解:
https://www.cnblogs.com/ganchuanpu/p/8594877.html
https://www.jianshu.com/p/e737c187e0c2
AOP得应用
1.耗时
2.埋点
3.登入
4.权限,方便的Android M动态权限配置框架