Aspectj doesn't work with kotlin

后端 未结 5 1415
南方客
南方客 2020-12-29 08:41

i want to use aspectj aop in kotlin,here is my code:

my annotation in annotation.lazy_list:

Kotlin:

 package anotation

@Retention(Annotation         


        
5条回答
  •  礼貌的吻别
    2020-12-29 09:22

    For what it's worth, we needed aspectJ weaving in our android project but really wanted to move to kotlin so we had to solve this problem. So the solutions in this thread using spring or maven didn't work for us. This is the solution for android gradle projects however, this WILL break incremental compilation and therefor slow down your build times and/or break something eventually. This gets us by until I can re-think our architecture and phase out aspectJ or (hopefully) android starts supporting it.

    There is confusion in some of the answers and comments to the OP that kapt solves this, but kapt lets you do compile time annotation processing, not weaving. That is, annotation processors let you generate code based on annotations but do not let you inject logic into existing code.

    This builds on top of this blog on adding aspectJ to android: https://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android

    Your kotlin classes get compiled into byte code, just into a different directory. So this solution using the same process to weave the java classes but runs it again on the kotlin class files

    at the top of your App/build.gradle add:

    buildscript {
        ext.aspectjVersion = '1.9.1'
        dependencies {
            classpath "org.aspectj:aspectjtools:$aspectjVersion"
        }
    }
    

    At the bottom of your App/build.gradle add:

    android.applicationVariants.all { variant ->
    
    // add the versionName & versionCode to the apk file name
    variant.outputs.all { output ->
        def newPath = outputFileName.replace(".apk", "-${variant.versionName}.${variant.versionCode}.apk")
        outputFileName = new File(outputFileName, newPath)
    
    
        def fullName = ""
        output.name.tokenize('-').eachWithIndex { token, index ->
            fullName = fullName + (index == 0 ? token : token.capitalize())
        }
    
        JavaCompile javaCompile = variant.javaCompiler
    
        MessageHandler handler = new MessageHandler(true)
        javaCompile.doLast {
            String[] javaArgs = ["-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)]
    
            String[] kotlinArgs = ["-showWeaveInfo",
                                   "-1.8",
                                   "-inpath", project.buildDir.path + "/tmp/kotlin-classes/" + fullName,
                                   "-aspectpath", javaCompile.classpath.asPath,
                                   "-d", project.buildDir.path + "/tmp/kotlin-classes/" + fullName,
                                   "-classpath", javaCompile.classpath.asPath,
                                   "-bootclasspath", project.android.bootClasspath.join(
                    File.pathSeparator)]
    
            new Main().run(javaArgs, handler)
            new Main().run(kotlinArgs, handler)
    
            def log = project.logger
            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
                }
            }
        }
    }
    

提交回复
热议问题