使用AspectJ

集成步骤:

  1、AS配置Aspectj环境

  2、配置使用ajc编译

  4、定义注解

  5、Aspect

  6、使用

  7、Example

AS配置Aspectj环境。Aspect目前最新版本为 1.8.10,AS中需要集成aspectjtool及aspectjrt并配置ajc编译,具体如下:

build.gradle(project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:${GRADLE_VERSION}" classpath "org.aspectj:aspectjtools:${ASPECT_VERSION}"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
} allprojects {
repositories {
jcenter()
}
} task clean(type: Delete) {
delete rootProject.buildDir
}

build.gradle(app)

dependencies {
compile "org.aspectj:aspectjrt:${ASPECT_VERSION}"
} 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", project.android.bootClasspath.join(File.pathSeparator)
]
def log = project.logger
log.error("aspectj", "----------------------aspect 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:
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
}
}
}
}

  

定义注解

package com.xiaosw.aspectj.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* <p><br/>ClassName : {@link UserBehaviour}
* <br/>Description : 用户行为统计
* <br/>
* <br/>Author : xiaosw<xiaosw0802@163.com>
* <br/>Create date : 2017-03-21 11:11:16</p>
*/ @Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface UserBehaviour { String value() default ""; }

  

Aspect

package com.xiaosw.aspectj.aspacet;

import android.util.Log;

import com.xiaosw.aspectj.annotation.UserBehaviour;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import java.lang.reflect.Method; /**
* <p><br/>ClassName : {@link UserBehaviourAspacet}
* <br/>Description :
* <br/>
* <br/>Author : xiaosw<xiaosw0802@163.com>
* <br/>Create date : 2017-03-21 11:11:17</p>
*/ @Aspect
public class UserBehaviourAspacet { /** @see UserBehaviourAspacet#getClass().getSimpleName() */
private static final String TAG = "UserBehaviourAspacet";

   // 任意类任意方法任意参数使用UserBehaivour注解
private static final String METHOD_INPUTCUT = "execution(@com.xiaosw.aspectj.annotation.UserBehaviour * *(..))";
   // 任意类任意构造方法使用UserBehaivour注解
private static final String CONSTRUCTOR_INPUTCUT = "execution(@com.xiaosw.aspectj.annotation.UserBehaviour *.new(..))";

   // 普通方法切点
@Pointcut(METHOD_INPUTCUT)
public void methodAnnotatedWithUserBehaviour() {}

   // 构造方法切点
@Pointcut(CONSTRUCTOR_INPUTCUT)
private void constructorAnnotatedWithUserBehaviour() {} /**
* 同步方法不作为切点
*/
@Pointcut("execution(!synchronized * *(..))")
private void noSynchronized() {} // @Before("methodAnnotatedWithUserBehaviour() || constructorAnnotatedWithUserBehaviour()")
// public void callBefore() {
// Log.e(TAG, "callBefore()");
// }
//
// @After("methodAnnotatedWithUserBehaviour() || constructorAnnotatedWithUserBehaviour()")
// public void callAfter() {
// Log.e(TAG, "callAfter()");
// }    // Advice
@Around("noSynchronized() && (methodAnnotatedWithUserBehaviour() || constructorAnnotatedWithUserBehaviour())")
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = null;
Log.e(TAG, "weaveJoinPoint: ");
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
if (method.isAnnotationPresent(UserBehaviour.class)) {
UserBehaviour userBehaviour = method.getAnnotation(UserBehaviour.class);
Log.e(TAG, "weaveJoinPoint: parsms = " + userBehaviour.value());
result = joinPoint.proceed();
} else {
Log.e(TAG, "weaveJoinPoint: method not instaceof UserBehaviour.class");
}
return result;
} }

  

使用

@UserBehaviour("call testAop()")
private void testAop() {
Log.e(TAG, "call -----------------> testAop()");
}

  

Android使用Aspectj的更多相关文章

  1. Android使用AOP

    这里不讲aop的概念,网上资料很多,这里只讲如何配置aop和自定义plugin. 1.使用场景 在android中,有些业务是公共的,例如:登录判断.获取权限.网络判断等一些公用的业务逻辑,这些都可以 ...

  2. AOP AspectJ 字节码 示例 Hugo MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. GOOGLE CODE ANDROID 开源项目 集合

    转:http://blog.csdn.net/dellheng/article/details/7163333 1.        ZXing  http://code.google.com/p/zx ...

  4. android开源项目集合

    ZXing http://code.google.com/p/zxing/ 条形码.二维码 K-9 Mail http://code.google.com/p/k9mail/ 邮件客户端 Sipdro ...

  5. AOP 面向切面编程

    AOP http://blog.csdn.net/xiang_j2ee/article/details/6851963 Android 支持 AspectJ 这个库来实现面向切面编程. 使用 Apac ...

  6. 【原创】Android AOP面向切面编程AspectJ

    一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...

  7. 在Android项目中使用AspectJ

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/8888459.html 什么是AOP AOP是 Aspec ...

  8. Android埋点方案的简单实现-AOP之AspectJ

    个人博客 http://www.milovetingting.cn Android埋点方案的简单实现-AOP之AspectJ AOP的定义 AOP为Aspect Oriented Programmin ...

  9. Android app AOP添加埋点技术总结

    目标:通过面向切面编程来实现对源代码无侵入的埋点.     方式 能力 缺点 学习曲线   XPosed 运行期hook 能hook自己应用进程的方法: 能hook别的应用的方法: 能hook系统方法 ...

随机推荐

  1. Java 异步 IO

         新的异步功能的关键点,它们是Channel 类的一些子集,Channel 在处理IO操作的时候需要被切换成一个后台进程.一些需要访问较大,耗时的操作,或是其它的类似实例,可以考虑应用此功能. ...

  2. 免越狱tweak应用逆向开发

    对于已越狱的设备,系统默认安装了mobilesubstrate动态库,提供一个系统级的入侵管道,所有的tweak都可以依赖它来进行开发.而对于没有越狱的手机,我们需要向目标app注入libsubstr ...

  3. STM32单片机学习心得——概述

    我校的课程真是跟不上时代发展,甚至还在教授8051/8052单片机的内容,于是不甘寂寞的我就自己踏入了STM32单片机的坑-- 首先,我现在大二,刚学完模拟电子技术,还没有学习数字电路技术,于是自学单 ...

  4. swift 2.0 用代码写一个简单地UIWebView

    其实写一个UIWebView 挺简单的,但是今天就被9.0 的新特性给坑了,不知道上一个项目中有没有遇到这个问题,反正是时间成了,自己也忘记了.今天还是再说一次吧. 我们先简单的创建一个UIWebVi ...

  5. Oracle instant client在windows下的安装和使用

    安装 * 从oracle官方网站下载instant client文件,一般来说,有basic.sqlplus.odbc.jdbc,就足够用的了: instantclient-basic-win32-1 ...

  6. vm虚拟机Kali2.0实现与物理机之间的文件拖动共享

    MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...

  7. js、css3实现图片的放大效果

    今天看网易的网站上,当我把鼠标放上去的时候发现图片放大,移开图片缩小,于是自行尝试,结果如下. 方法一:使用js和css3 效果如图: 这样的实现非常简单,就是利用js的mouseover和 mous ...

  8. Angular2 + Webpack项目搭建Demo

    本文将从头开始编写实际的代码来完成一个angular2的demo. 题外话是其实angular2官网的快速开始项目已经很酷炫了,但其侧重快速二字,只够拿来练习玩耍,倒是github上确实已经有了一些不 ...

  9. E. Change-free

    Student Arseny likes to plan his life for n days ahead. He visits a canteen every day and he has alr ...

  10. 基于ES6模块标准通过webpack打包HTM5项目

    本篇主要演示通过webpack打包phaser项目,webpack安装方法在此处就不一一赘述了 经常用phaser来写html5游戏的朋友可能会发现,当游戏场景比较多时,如果都写在一个js文件中那么将 ...