AOP is what here will not expand to speak, want to know can search search. So how do you implement AOP? Android uses ASM and AspectJ for the most part. For ASM, check out the author’s colleague’s article or the hand’s article to start with AspectJ

A: effect demonstration

Scenario: I have a piece of code that needs to have some permissions before it can be executed: one line of comment gets the job done

Two: preparation

A lot of people get stuck here and don’t get through the configuration. The first way: use the doctor’s library https://github.com/HujiangTechnology/gradle_plugin_android_aspectjx second way: native way Here about the second

  1. The rootbuild.gradleIncreased reliance on
        classpath 'org. Aspectj: aspectjtools: 1.8.9'
        classpath 'org. Aspectj: aspectjweaver: 1.8.9'
Copy the code
  1. Create a newAndroid Library ModuleAnd in thebuild.gradleAdd the code
import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
Copy the code
compile 'org. Aspectj: aspectjrt: 1.8.9'
Copy the code
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", android.bootClasspath.join(
                File.pathSeparator)]

        MessageHandler handler = new MessageHandler(true);
        new Main().run(args, 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; }}}}Copy the code

Add black label note: “- bootclasspath,” android. Bootclasspath. Join the bootclasspath here earlier with android many people according to the configuration not compile because through online here. Because the higher gradle version (author gradle version 2.3.2) needs to look like this. Go back to your app (your project) Module and add build.gradle

import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
Copy the code
compile project(': just module')
Copy the code
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.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.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; }}}}Copy the code

Go ahead and compile it and see if it passes

The code to

AspectJ
@Aspect
@point
class
@Around
@point

AspectJ
@Around
@ Before, @ After