美文网首页安卓开发
安卓集成AOP:AspectJ

安卓集成AOP:AspectJ

作者: 蓝不蓝编程 | 来源:发表于2018-10-12 08:41 被阅读17次

    AOP是什么?

    AOP (Aspect Oriented Programming,面向切面编程),可以在运行时动态地将代码切入到类中指定方法、指定位置上的一种技术。说白了,就是把横切逻辑从业务逻辑 中抽离出来。哪些属于横切逻辑呢?比如,性能监控、日志、权限控制等等。

    AOP相关概念:
    JointPoint(连接点)
    Pointcut(切入点)
    Advice(增强)
    Adivisor(切面)
    Before Advice(前置增强)
    After Advice(后置增强)
    Around Advice(环绕增强)
    Throws Advice(抛出增强)
    Introduction Advice(引入增强)

    接入方法:

    1.拷贝如下文件到工程根目录下(是根目录,不是app目录下),命名为aspectj.gradle

    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
    
    buildscript {
    
        repositories {
            maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
            maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
            jcenter()
        }
        dependencies {
            classpath 'org.aspectj:aspectjtools:1.9.1'
        }
    }
    
    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;
                }
            }
        }
    }
    dependencies {
        implementation 'org.aspectj:aspectjrt:1.9.1'
    }
    
    image.png

    2.app下的build.gradle文件中增加:

    apply from: '../aspectj.gradle'
    

    Demo:

    https://gitee.com/cxyzy1/aop_demo.git

    参考资料:

    https://www.jianshu.com/p/c66f4e3113b3

    https://www.jianshu.com/p/f577aec99e17

    利用aop监控方法耗时(hugo): https://yq.aliyun.com/articles/7104

    安卓开发技术分享: https://www.jianshu.com/p/442339952f26

    相关文章

      网友评论

        本文标题:安卓集成AOP:AspectJ

        本文链接:https://www.haomeiwen.com/subject/lzlwaftx.html