当前位置: 代码迷 >> 综合 >> Android:使用AspectJ 检查项目中的方法执行时间
  详细解决方案

Android:使用AspectJ 检查项目中的方法执行时间

热度:91   发布时间:2024-02-12 13:22:15.0

首先需要集成AspectJ

1)在项目gradle中添加依赖

dependencies {classpath "com.android.tools.build:gradle:3.5.2"classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.6' //aspectj}

2) 在app gradle中添加配置

apply plugin: 'com.android.application'
apply plugin: 'android-aspectjx'  //aspectjandroid {compileSdkVersion 29buildToolsVersion "29.0.3"//aspectjaspectjx {//排除所有package路径中包含`android.support`的class文件及库(jar文件)exclude 'android.support'}//aspectjaspectjx {//关闭AspectJX功能enabled false}//aspectjcompileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}
}dependencies {implementation 'org.aspectj:aspectjrt:1.9.6'  //aspectj
}//aspectj
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Mainfinal def log = project.logger
final 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.javaCompilejavaCompile.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.thrownbreak;case IMessage.WARNING:log.warn message.message, message.thrownbreak;case IMessage.INFO:log.info message.message, message.thrownbreak;case IMessage.DEBUG:log.debug message.message, message.thrownbreak;}}}
}

3)第三步:我们就可以实现方法来获取每个方法的执行时长了

/*** Created by malei on 2020/8/17* Describe:*/
@Aspect
public class PerformanceAop {/*** 获取一个类中所有方法的执行时长* call : 代表的是在方法调用的地方添加代码* 第一个 * : 代表的方法的修饰符* 第二个 * : 代表的是方法的返回值类型* 第三个 * : 类名* 第四个 * : 代表的是方法名* (..)    : 参数?表* 该方法拦截的是: com.example.qydemo1包下的所有类的方法*/@Around("call(** com.example.qydemo1.*.*(..))")public void getTime(ProceedingJoinPoint joinPoint) {Signature signature = joinPoint.getSignature();String name = signature.toShortString();long time = System.currentTimeMillis();try {joinPoint.proceed();} catch (Throwable throwable) {throwable.printStackTrace();}Log.i("MALEI", name + " cost " + (System.currentTimeMillis() - time));}
}

 

执行结果:

08-19 16:09:26.605 15355-15355/com.example.qydemo1 I/MALEI: App.demo2() cost 100
08-19 16:09:26.605 15355-15355/com.example.qydemo1 I/MALEI: App.demo1() cost 200
08-19 16:09:26.686 15355-15355/com.example.qydemo1 I/MALEI: MainActivity.setContentView(..) cost 45
08-19 16:09:26.686 15355-15355/com.example.qydemo1 E/MALEI: insertLog --> 我执行了
08-19 16:09:26.787 15355-15355/com.example.qydemo1 I/MALEI: MainActivity.insertLog() cost 101
08-19 16:09:26.787 15355-15355/com.example.qydemo1 E/MALEI: add --> 我执行了
08-19 16:09:26.787 15355-15355/com.example.qydemo1 E/MALEI: testAspectAround --> 我执行了
08-19 16:09:26.787 15355-15355/com.example.qydemo1 I/MALEI: MainActivity.testAspectAround() cost 0

 

 

 

 

 

  相关解决方案