Android埋点方案的简单实现-AOP之AspectJ
个人博客
android埋点方案的简单实现-aop之aspectj
aop的定义
aop为aspect oriented programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。
以上关于aop的定义引用自百度百科。
aop的运用场景
日志记录、性能统计、权限控制、埋点等
aop的具体实现方案有很多,这里选用aspectj来简单实现
- 监听view的点击、页面打开、关闭
- 为方法添加开始、结束的日志
- 统计方法运行时间
aspectj的使用
aspectj的引入
这里引用aspectjx,aspectjx是基于aspectj的一个aop框架
新建android工程,在项目根目录下的build.gradle文件中添加依赖
dependencies { //... classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.8' //... }
新建module,类型选择android library,在新建的library的build.gradle文件中,添加相应的依赖
apply plugin: 'android-aspectjx'
在app的build.gradle文件中增加对刚才新建的library的引用及aspectj的依赖
apply plugin: 'android-aspectjx' dependencies { //... androidtestimplementation 'androidx.test.espresso:espresso-core:3.1.1' }
监听view的点击、页面打开、关闭
在library中新建回调接口trackcallback
public interface trackcallback { /** * 当view被点击 * * @param pagename * @param viewidname */ void onclick(string pagename, string viewidname); /** * 当页面打开时 * * @param pagename */ void onpageopen(string pagename); /** * 当页面关闭时 * * @param pagename */ void onpageclose(string pagename); }
在library中新建切入点trackpoint
public class trackpoint { private static trackcallback mtrackcallback; private trackpoint() { } /** * 初始化 * @param trackcallback */ public static void init(trackcallback trackcallback) { mtrackcallback = trackcallback; } static void onclick(string pagename, string viewidname) { if (mtrackcallback == null) { return; } mtrackcallback.onclick(pagename, viewidname); } static void onpageopen(string pagename) { if (mtrackcallback == null) { return; } mtrackcallback.onpageopen(pagename); } static void onpageclose(string pagename) { if (mtrackcallback == null) { return; } mtrackcallback.onpageclose(pagename); } }
在library中新建切面traceaspect
@aspect public class traceaspect { private static final string tag = traceaspect.class.getsimplename(); @pointcut("execution(* onclick(..))") public void onclickpointcut() { } @pointcut("execution(* android.app.activity+.oncreate(..))") public void activityoncreatepointcut() { } @pointcut("execution(* android.app.activity+.ondestroy(..))") public void activitydestroypointcut() { } @around("onclickpointcut()") public void onclick(proceedingjoinpoint joinpoint) throws throwable { object target = joinpoint.gettarget(); string classname = ""; if (target != null) { classname = target.getclass().getname(); } object[] args = joinpoint.getargs(); if (args.length > 0 && args[0] instanceof view) { view view = (view) args[0]; string entryname = view.getresources().getresourceentryname(view.getid()); trackpoint.onclick(classname, entryname); } joinpoint.proceed(); } @around("activityoncreatepointcut()") public void pageopen(proceedingjoinpoint joinpoint) throws throwable { object target = joinpoint.gettarget(); string classname = target.getclass().getname(); trackpoint.onpageopen(classname); joinpoint.proceed(); } @around("activitydestroypointcut()") public void pageclose(proceedingjoinpoint joinpoint) throws throwable { object target = joinpoint.gettarget(); string classname = target.getclass().getname(); trackpoint.onpageclose(classname); joinpoint.proceed(); } }
在app模块新建application,在oncreate中执行初始化:
public class app extends application { private static final string tag = traceaspect.class.getsimplename(); @override public void oncreate() { super.oncreate(); trackpoint.init(new trackcallback() { @override public void onclick(string pagename, string viewidname) { log.d(tag, "onclick:" + pagename + "-" + viewidname); //执行相应的业务 } @override public void onpageopen(string pagename) { log.d(tag, "onpageopen:" + pagename); //执行相应的业务 } @override public void onpageclose(string pagename) { log.d(tag, "onpageclose:" + pagename); //执行相应的业务 } }); } }
新增的application需要在androidmanifest中引用才会生效。
运行app后,点击打开另一个activity,然后依次退出activity,输出日志如下:
2020-01-13 16:50:17.373 16610-16610/com.wangyz.aspectjdemo d/traceaspect: onpageopen:com.wangyz.aspectjdemo.mainactivity 2020-01-13 16:50:19.243 16610-16610/com.wangyz.aspectjdemo d/traceaspect: onclick:com.wangyz.aspectjdemo.mainactivity-btn_open 2020-01-13 16:50:19.298 16610-16610/com.wangyz.aspectjdemo d/traceaspect: onpageopen:com.wangyz.aspectjdemo.secondactivity 2020-01-13 16:50:21.392 16610-16610/com.wangyz.aspectjdemo d/traceaspect: onpageclose:com.wangyz.aspectjdemo.secondactivity 2020-01-13 16:50:22.320 16610-16610/com.wangyz.aspectjdemo d/traceaspect: onpageclose:com.wangyz.aspectjdemo.mainactivity
为方法添加开始、结束的日志
在library中增加注解addlog
@target(elementtype.method) @retention(retentionpolicy.runtime) public @interface addlog { }
在traceaspect增加以下代码
@pointcut("execution(@com.wangyz.library.addlog * *(..))") public void addlogpointcut() { } @around("addlogpointcut()") public void addlog(proceedingjoinpoint joinpoint) throws throwable { methodsignature signature = (methodsignature) joinpoint.getsignature(); addlog addlog = signature.getmethod().getannotation(addlog.class); if (addlog != null) { object target = joinpoint.gettarget(); string classname = ""; if (target != null) { classname = target.getclass().getname(); } log.d(tag, "start execute:" + classname + "-" + signature.getmethod().getname()); joinpoint.proceed(); log.d(tag, "end execute:" + classname + "-" + signature.getmethod().getname()); } else { joinpoint.proceed(); } }
在mainactivity的oncreate上增加addlog注解
@addlog @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //... }
运行app后,输入日志如下:
2020-01-13 16:50:17.373 16610-16610/com.wangyz.aspectjdemo d/traceaspect: start execute:com.wangyz.aspectjdemo.mainactivity-oncreate 2020-01-13 16:50:17.392 16610-16610/com.wangyz.aspectjdemo d/traceaspect: end execute:com.wangyz.aspectjdemo.mainactivity-oncreate
统计方法运行时间
在library中增加注解exectime
@target(elementtype.method) @retention(retentionpolicy.runtime) public @interface exectime { }
在traceaspect增加以下代码
@pointcut("execution(@com.wangyz.library.exectime * *(..))") public void exectimepointcut() { } @around("exectimepointcut()") public void exectime(proceedingjoinpoint joinpoint) throws throwable { methodsignature signature = (methodsignature) joinpoint.getsignature(); exectime exectime = signature.getmethod().getannotation(exectime.class); if (exectime != null) { long start = system.currenttimemillis(); joinpoint.proceed(); long end = system.currenttimemillis(); object target = joinpoint.gettarget(); string classname = ""; if (target != null) { classname = target.getclass().getname(); } log.d(tag, "execute time:" + classname + "-" + signature.getmethod().getname() + " : " + (end - start) + "ms"); } else { joinpoint.proceed(); } }
在onclick方法上增加exectime注解
@exectime @override public void onclick(view v) { switch (v.getid()) { case r.id.btn_open: intent intent = new intent(this, secondactivity.class); startactivity(intent); break; default: break; } }
运行app后,输出日志如下:
2020-01-13 16:50:19.272 16610-16610/com.wangyz.aspectjdemo d/traceaspect: execute time:com.wangyz.aspectjdemo.mainactivity-onclick : 28ms
源码地址:https://github.com/milovetingting/samples/tree/master/aspectjdemo