Android 基于AOP监控之——AspectJ使用指南
2016-05-27 22:01
751 查看
如何使用
使用方法
Step
1创建AS原工程
2创建moduleAndroid Library然后添加AspectJ依赖必须添加至module中添加至APP工程中ajc编译器是不会重构目标代码的
3编写build脚本添加任务使得IDE使用ajc作为编译器编译代码
4编写AspectJ切面程序代码
运行结果
DEMO地址
AspectJ的使用核心就是它的编译器,它就做了一件事,将AspectJ的代码在编译期插入目标程序当中,运行时跟在其它地方没什么两样,因此要使用它最关键的就是使用它的编译器去编译代码ajc。ajc会构建目标程序与AspectJ代码的联系,在编译期将AspectJ代码插入被切出的PointCut中,已达到AOP的目的。
因此,无论在什么IDE上(如果使用命令行就可以直接使用ajc编译了),问题就是让IDE使用ajc作为编译器编译代码。
2、Gradle配置:国外还有一个大牛在build文件中添加了一些脚本,也能够在AS中使用,但是脚本定义的任务有点麻烦(Gradle基础不好的看不懂,我就是)。不过正是这位大牛解决了我项目上遇到的问题。文章地址:Aspect Oriented Programming in Android
下面就介绍一下第二种方法的具体的使用步骤。
build.gradle(app):
build.gradle(module):
使用方法
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.IMessage import org.aspectj.bridge.MessageHandler import org.aspectj.tools.ajc.Main buildscript { 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.logger final def variants = project.android.applicationVariants variants.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.LibraryPlugin import org.aspectj.bridge.IMessage import org.aspectj.bridge.MessageHandler import org.aspectj.tools.ajc.Main apply 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. */ @Aspect public 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: beforecall 05-27 21:01:30.678 25192-25192/? E/TAG: beforecall 05-27 21:01:30.678 25192-25192/? E/TAG: ----------------------------->aroundWeaverPoint 05-27 21:01:30.678 25192-25192/? E/TAG: MainActivity ----------------------------->aroundWeaverPoint 05-27 21:01:30.678 25192-25192/? E/TAG: ----------------------------->aroundWeaverPoint 05-27 21:39:20.108 27390-27390/? E/TAG: MainActivity From MainActivity
DEMO地址
AndroidAOP相关文章推荐
- ASP.NET for WebApi
- ASP.NET for WebApi
- ASP.net MVC基于EntityFrameWork 的 MODEL层控制反转(IOC)架构设计
- asp.net 服务器端缓存与客户端缓存 [转]
- 在ASP.NET MVC 中获取当前URL、controller、action(转)
- 从Microsoft.AspNet.Identity看微软推荐的一种MVC的分层架构
- ASP.NET Identity V2
- asp.net mvc 与js的联合加密解密
- NHibernate with ASP.NET MVC 入门示例
- DataSet对象创建XML文档
- asp.net core 使用EntityFrameWork遇到的问题
- 利用windows的远程桌面完成Raspberry的首次系统安装
- Asp.Net MVC及Web API框架配置会碰到的几个问题及解决方案 [转]
- org.aspectj.lang.JoinPoint-中文简要API 与pointcut expression表达式
- ASP.NET MVC
- MVC程序部署后页面指向login.aspx
- ASP.NET应用程序与页面生命周期
- ASP.NET 5 RC1 升级 ASP.NET Core 1.0 RC2 记录
- ASP.NET配置KindEditor文本编辑器图文教程
- 发布ASP.net网站问题