android studio aspectj 配置,Android studio配置AspectJ
一、首先說(shuō)說(shuō)使用AspectJ的意義
使用切點(diǎn)插入,減少冗余代碼,但是因?yàn)槭蔷幾g時(shí)插入,所以運(yùn)行時(shí)會(huì)有性能損耗,但是總的來(lái)說(shuō)微乎其微
二、運(yùn)行原理
這是我原來(lái)的代碼
@BehaviorTrace("點(diǎn)擊方法")
public void test(){
SystemClock.sleep(3000);
Toast.makeText(MainActivity.this, "hahha", Toast.LENGTH_SHORT).show();
}
這是運(yùn)行編譯后的代碼
@BehaviorTrace("點(diǎn)擊方法")
public void test() {
JoinPoint var1 = Factory.makeJP(ajc$tjp_0, this, this);
BehaviorAspect var10000 = BehaviorAspect.aspectOf();
Object[] var2 = new Object[]{this, var1};
var10000.waveJoinPoint((new MainActivity$AjcClosure1(var2)).linkClosureAndJoinPoint(69648));
}
他是在對(duì)應(yīng)切點(diǎn)插入插入代碼,然后使用Aspectj的規(guī)則去編譯運(yùn)行
二、實(shí)現(xiàn)
我是直接放在一個(gè)model下的,沒用新建model你也可以新建一個(gè)model
1、首先在項(xiàng)目的gradle文件中引入
classpath 'org.aspectj:aspectjtools:1.8.9'
classpath 'org.aspectj:aspectjweaver:1.8.9'
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'org.aspectj:aspectjtools:1.8.9'
classpath 'org.aspectj:aspectjweaver:1.8.9'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2、在app下的gradle文件中引入AspectJrt文件,和編譯替換文件
implementation 'org.aspectj:aspectjrt:1.8.9'
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
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.8",
"-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;
}
}
}
}
以上就配置好了編譯環(huán)境,接下來(lái)是使用AspectJ
1、先創(chuàng)建注解文件
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BehaviorTrace {
String value();
}
2、創(chuàng)建AOP文件,并實(shí)現(xiàn)
@Aspect
public class BehaviorAspect {
//切點(diǎn),執(zhí)行com.xiaofan.customcontrol.annotation.BehaviorTrace路徑下的注解
@Pointcut("execution(@com.xiaofan.customcontrol.annotation.BehaviorTrace * *(..))")
public void annoBaviorTrace() {}
//執(zhí)行annoBaviorTrace方法
@Around("annoBaviorTrace()")
public Object waveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
BehaviorTrace anno = methodSignature.getMethod().getAnnotation(BehaviorTrace.class);
String functionName = anno.value();
Log.i("wxf","方法:"+functionName);
long begin = System.currentTimeMillis();
//方法執(zhí)行
Object reuslt= joinPoint.proceed();
long duration = System.currentTimeMillis() - begin;
Log.i("wxf","方法耗時(shí):"+duration+" ms");
return reuslt;
}
}
最后使用,在你需要使用的地方直接添加注解即可,
以上就是一個(gè)簡(jiǎn)單的AspectJ使用案例,當(dāng)然它能做的事情不知這一點(diǎn)
注意:我也試過(guò)全使用jar包,但是在gradle替換代碼中,找不到包
總結(jié)
以上是生活随笔為你收集整理的android studio aspectj 配置,Android studio配置AspectJ的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 【Python爬虫】爬取微信公众号文章信
- 下一篇: 让qt应用程序拥有管理员权限
