Android 基于AOP监控之——AspectJ使用指南
来源:互联网 发布:python list fuzhi 编辑:程序博客网 时间:2024/06/06 03:34
- 如何使用
- 使用方法
- Step
- 1创建AS原工程
- 2创建moduleAndroid Library然后添加AspectJ依赖必须添加至module中添加至APP工程中ajc编译器是不会重构目标代码的
- 3编写build脚本添加任务使得IDE使用ajc作为编译器编译代码
- 4编写AspectJ切面程序代码
- 运行结果
- DEMO地址
如何使用
在Eclipse中已经有AJDT插件集成了AspectJ编译器的使用和关键字的声明。但是在Android Studio中没有这样的官方插件。因此,这里讲一下如何在Android Studio中使用AspectJ。详细了解AspectJ的使用请看Android基于AOP的非侵入式监控
AspectJ的使用核心就是它的编译器,它就做了一件事,将AspectJ的代码在编译期插入目标程序当中,运行时跟在其它地方没什么两样,因此要使用它最关键的就是使用它的编译器去编译代码ajc。ajc会构建目标程序与AspectJ代码的联系,在编译期将AspectJ代码插入被切出的PointCut中,已达到AOP的目的。
因此,无论在什么IDE上(如果使用命令行就可以直接使用ajc编译了),问题就是让IDE使用ajc作为编译器编译代码。
使用方法
1、插件:网上有人在github上提供了集成的插件gradle-android-aspectj-plugin,一开始我也是用的这个,但是在项目当中,无法兼容databinding,这个问题现在作者依然没有解决。
2、Gradle配置:国外还有一个大牛在build文件中添加了一些脚本,也能够在AS中使用,但是脚本定义的任务有点麻烦(Gradle基础不好的看不懂,我就是)。不过正是这位大牛解决了我项目上遇到的问题。文章地址:Aspect Oriented Programming in Android
下面就介绍一下第二种方法的具体的使用步骤。
Step
1、创建AS原工程
2、创建module(Android Library),然后添加AspectJ依赖,必须添加至module中,添加至APP工程中ajc编译器是不会重构目标代码的。
compile 'org.aspectj:aspectjrt:1.8.9'
3、编写build脚本,添加任务,使得IDE使用ajc作为编译器编译代码。
这里,主工程和Module都需要对构建脚本添加一些任务,目的就是为了建立两者的通信,使得IDE使用ajc编译代码。
build.gradle(app):
import org.aspectj.bridge.IMessageimport org.aspectj.bridge.MessageHandlerimport org.aspectj.tools.ajc.Mainbuildscript { repositories { mavenCentral() } dependencies { classpath 'org.aspectj:aspectjtools:1.8.9' }}apply plugin: 'com.android.application'repositories { mavenCentral()}dependencies { compile project(':aspectjlibrary')}android { compileSdkVersion 21 buildToolsVersion '22.0.1' buildTypes { debug { minifyEnabled false // should disable proguard in debug builds } } defaultConfig { applicationId "com.example.lingyimly.try2" minSdkVersion 15 targetSdkVersion 21 } lintOptions { abortOnError true }}final def log = project.loggerfinal def variants = project.android.applicationVariantsvariants.all { variant -> if (!variant.buildType.isDebuggable()) { log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.") return; } JavaCompile javaCompile = variant.javaCompile javaCompile.doLast { String[] args = ["-showWeaveInfo", "-1.5", "-inpath", javaCompile.destinationDir.toString(), "-aspectpath", javaCompile.classpath.asPath, "-d", javaCompile.destinationDir.toString(), "-classpath", javaCompile.classpath.asPath, "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)] log.debug "ajc args: " + Arrays.toString(args) MessageHandler handler = new MessageHandler(true); new Main().run(args, handler); for (IMessage message : handler.getMessages(null, true)) { switch (message.getKind()) { case IMessage.ABORT: case IMessage.ERROR: case IMessage.FAIL: log.error message.message, message.thrown break; case IMessage.WARNING: log.warn message.message, message.thrown break; case IMessage.INFO: log.info message.message, message.thrown break; case IMessage.DEBUG: log.debug message.message, message.thrown break; } } }}
build.gradle(module):
import com.android.build.gradle.LibraryPluginimport org.aspectj.bridge.IMessageimport org.aspectj.bridge.MessageHandlerimport org.aspectj.tools.ajc.Mainapply plugin: 'com.android.library'buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:2.1.0' classpath 'org.aspectj:aspectjtools:1.8.9' classpath 'org.aspectj:aspectjweaver:1.8.9' }}repositories { mavenCentral()}dependencies { compile 'org.aspectj:aspectjrt:1.8.9' compile 'com.android.support:appcompat-v7:22.2.1'}android { compileSdkVersion 22 buildToolsVersion '23.0.1' lintOptions { abortOnError false }}android.libraryVariants.all { variant -> LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin) JavaCompile javaCompile = variant.javaCompile javaCompile.doLast { String[] args = ["-showWeaveInfo", "-1.5", "-inpath", javaCompile.destinationDir.toString(), "-aspectpath", javaCompile.classpath.asPath, "-d", javaCompile.destinationDir.toString(), "-classpath", javaCompile.classpath.asPath, "-bootclasspath", plugin.project.android.bootClasspath.join( File.pathSeparator)] MessageHandler handler = new MessageHandler(true); new Main().run(args, handler) def log = project.logger for (IMessage message : handler.getMessages(null, true)) { switch (message.getKind()) { case IMessage.ABORT: case IMessage.ERROR: case IMessage.FAIL: log.error message.message, message.thrown break; case IMessage.WARNING: case IMessage.INFO: log.info message.message, message.thrown break; case IMessage.DEBUG: log.debug message.message, message.thrown break; } } }}
4、编写AspectJ切面程序代码
package com.example.lingyimly.aspectj;import android.util.Log;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * Created by lingyi.mly on 2016/5/27. */@Aspectpublic class TraceAspect { private String TAG = "TAG "; private static final String POINT_METHOD = "execution(* com.example.lingyimly.try3.MainActivity.*(..))"; private static final String POINT_CALLMETHOD = "call(* com.example.lingyimly.try3.MainActivity.*(..))"; @Pointcut(POINT_METHOD) public void methodAnnotated(){} @Pointcut(POINT_CALLMETHOD) public void methodCallAnnotated(){} @Around("methodAnnotated()") public Object aronudWeaverPoint(ProceedingJoinPoint joinPoint) throws Throwable { joinPoint.proceed(); String result = "----------------------------->aroundWeaverPoint"; Log.e(TAG,"----------------------------->aroundWeaverPoint"); return result;//替换原方法的返回值 } @Before("methodCallAnnotated()") public void beforecall(JoinPoint joinPoint){ Log.e(TAG,"beforecall"); }}
5 运行结果
05-27 21:01:30.618 25192-25192/? E/TAG: beforecall05-27 21:01:30.678 25192-25192/? E/TAG: beforecall05-27 21:01:30.678 25192-25192/? E/TAG: ----------------------------->aroundWeaverPoint05-27 21:01:30.678 25192-25192/? E/TAG: MainActivity ----------------------------->aroundWeaverPoint05-27 21:01:30.678 25192-25192/? E/TAG: ----------------------------->aroundWeaverPoint05-27 21:39:20.108 27390-27390/? E/TAG: MainActivity From MainActivity
DEMO地址
AndroidAOP
- Android 基于AOP监控之——AspectJ使用指南
- Android 基于AOP监控之——AspectJ使用指南
- Android基于AOP的非侵入式监控之——AspectJ实战
- Android基于AOP的非侵入式监控之——AspectJ实战
- Android基于AOP的非侵入式监控之——AspectJ实战
- 非侵入式AOP监控之——AspectJ使用
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3
- SSM框架项目搭建系列(六)—Spring AOP之基于XML的声明式AspectJ
- SSM框架项目搭建系列(七)—Spring AOP之基于注解的声明式AspectJ
- Gym 100947K-Jpeg
- vertica-not fit in memory
- 基于hibernate对数据库操作封装
- 可变个数形参的方法
- 一二三级联动
- Android 基于AOP监控之——AspectJ使用指南
- 不使用数据线连接手机调试app 显示Log日志
- C语言字符串、数组与指针结合
- vertica-数据库强制关闭
- 用java求水仙花数最经典的代码
- win2012R2配置双网卡绑定
- adb logcat 查看日志命令行用法(简版)
- ZOJ,PKU--训练题分类
- nyoj1248 海岛争霸(第七届河南省程序设计大赛)