Android使用Aspectj
使用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的更多相关文章
- Android使用AOP
这里不讲aop的概念,网上资料很多,这里只讲如何配置aop和自定义plugin. 1.使用场景 在android中,有些业务是公共的,例如:登录判断.获取权限.网络判断等一些公用的业务逻辑,这些都可以 ...
- AOP AspectJ 字节码 示例 Hugo MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- GOOGLE CODE ANDROID 开源项目 集合
转:http://blog.csdn.net/dellheng/article/details/7163333 1. ZXing http://code.google.com/p/zx ...
- android开源项目集合
ZXing http://code.google.com/p/zxing/ 条形码.二维码 K-9 Mail http://code.google.com/p/k9mail/ 邮件客户端 Sipdro ...
- AOP 面向切面编程
AOP http://blog.csdn.net/xiang_j2ee/article/details/6851963 Android 支持 AspectJ 这个库来实现面向切面编程. 使用 Apac ...
- 【原创】Android AOP面向切面编程AspectJ
一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...
- 在Android项目中使用AspectJ
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/8888459.html 什么是AOP AOP是 Aspec ...
- Android埋点方案的简单实现-AOP之AspectJ
个人博客 http://www.milovetingting.cn Android埋点方案的简单实现-AOP之AspectJ AOP的定义 AOP为Aspect Oriented Programmin ...
- Android app AOP添加埋点技术总结
目标:通过面向切面编程来实现对源代码无侵入的埋点. 方式 能力 缺点 学习曲线 XPosed 运行期hook 能hook自己应用进程的方法: 能hook别的应用的方法: 能hook系统方法 ...
随机推荐
- canvas小程序-快跑程序员
canvas不用说html5带来的好东西,游戏什么的,么么哒 记得有一天玩手机游戏,就是一个跳跃过柱子那种,其实元素很简单啊,app能开发,借助html5 canvas也可以啊, 于是就开始了. -- ...
- ubuntu下安装ssh服务器方法
由于xshell远程连接ubuntu是通过ssh协议的,所以,需要给ubuntu安装ssh服务器. 1)ubuntu安装ssh服务器 sudo apt-get install openssh-serv ...
- UTF编码检测
最近工作上正好需要进行UTF编码检测,自己写了一个,分享给大家,希望可以帮得上有需要用的朋友 public bool isUtf8(byte[] rawText) { bool result = tr ...
- removeAll
问题:无法移除2个集合中相同元素 方法:移除所包含的其所有元素. 在执行removeAll方法时,会先对集合元素进行比较,如果元素相等才执行移除操作,说到这,相信很多人都已经明白是怎么回事了,因为不相 ...
- Xamarin开发IOS系列教程一:安装黑苹果
经过一番思想挣扎和斗争之后,最终还是选择采用Xamarin来开发跨平台移动应用,好处和优点大家可以搜索其它博文,因为家里面穷加上谈了恋爱,就不买苹果了,开发阶段在Windows上面直接搞定哈,时候不早 ...
- MongoDB学习总结(四) —— 索引的基本用法
说到索引,大家肯定都在关系型数据库或多或少接触过,它的主要目的是加速查询的速度.MongoDB作为一种数据库,当然也提供了索引的操作. 我们先插入1万条测试数据. 首先,我们先来看看不加索引查找nam ...
- Codeforces Round #383 Div 1题解
第一次打Div 1,感觉还是挺难的..把基础题打完就日常划水了.... [A. Arpa's loud Owf and Mehrdad's evil plan](http://codeforces.c ...
- Noip 2014酱油记+简要题解
好吧,day2T1把d默认为1也是醉了,现在只能期待数据弱然后怒卡一等线吧QAQ Day0 第一次下午出发啊真是不错,才2小时左右就到了233,在车上把sao和fate补掉就到了= = 然后到宾馆之后 ...
- BZOJ 1009 :[HNOI2008]GT考试(KPM算法+dp+矩阵快速幂)
这道到是不用看题解,不过太经典了,早就被剧透一脸了 这道题很像ac自动机上的dp(其实就是) 然后注意到n很大,节点很小,于是就可以用矩阵快速幂优化了 时间复杂度为o(m^3 *log n); 蒟蒻k ...
- [Selenium With C#学习笔记] Lesson-02 Web元素定位
使用Selenium来做自动化测试,一般的流程是:查找定位元素--->操作元素--->断言,那么第一步我们需要能够完成查找并定位元素,Selenium目前提供了8种基本定位方法,可根据实际 ...