AspectJ Gradle

Additional

Language
Kotlin
Version
3.2.0 (Nov 5, 2017)
Created
Jun 10, 2015
Updated
Sep 2, 2019
Owner
Eduard (Archinamon)
Contributors
divankov
Eduard (Archinamon)
StrLght
Badya
jdappel
5
Activity
Badge
Generate
Download
Source code

Promotion

GradleAspectJ-Android



A Gradle plugin which enables AspectJ for Android builds. Supports writing code with AspectJ-lang in .aj files and in java-annotation style. Full support of Android product flavors and build types. Support Kotlin, Groovy, Scala and any other languages that compiles into java bytecode.

Actual version: com.archinamon:android-gradle-aspectj:3.3.12. Friendly with jRebel for Android!

This plugin is completely friendly with APT (Android Annotation Processing Tools) and Retrolambda project (but Java 8 not supported in .aj files). AndroidAnnotations, Dagger are also supported and works fine.

This plugin has many ideas from the others similar projects, but no one of them grants full pack of features like this one. Nowadays it has been completely re-written using Transform API.

Key features

Augments Java, Kotlin, Groovy bytecode simultaneously! Works with background mechanics of jvm-based languages out-of-box!How to teach Android Studio to understand the AspectJ! May not work properly for AS 3.0 :(



It is easy to isolate your code with aspect classes, that will be simply injected via cross-point functions, named advices, into your core application. The main idea is — code less, do more!

AspectJ-Gradle plugin provides supply of all known JVM-based languages, such as Groovy, Kotlin, etc. That means you can easily write cool stuff which may be inject into any JVM language, not only Java itself! :)

To start from you may look at my example project. And also you may find useful to look at reference manual of AspectJ language and simple code snippets. In case aspectj-native not supported by Android Studio (even with IDE-plugin it's using is complicated), you may write a java-classes with aspectj annotations.

Two simple rules you may consider when writing aspect classes.

  • Do not write aspects outside the src/$flavor/aspectj source set! These aj-classes will be excluded from java compiler.
  • Do not try to access aspect classes from java/kotlin/etc. In case java compiler doesn't know anything about aspectj, it will lead to compile errors on javac step.

These rules affects only in case you're writing in native aj-syntax. You may write aspects in java-annotation style and being free from these limitations.

Usage

First add a maven repo link into your repositories block of module build file:

mavenCentral()

Don't forget to add mavenCentral() due to some dependencies inside AspectJ-gradle module.

Add the plugin to your buildscript's dependencies section:

Kotlin
classpath("com.archinamon:android-gradle-aspectj:3.3.12")
Groovy
classpath 'com.archinamon:android-gradle-aspectj:3.3.12'
Apply the `aspectj` plugin:
Kotlin
plugins {
    id("com.android.application")
    id("com.archinamon.aspectj")
}


Groovy
plugins {
    id 'com.android.application'
    id 'com.archinamon.aspectj'
}
importandroid.app.Application;
importandroid.app.NotificationManager;
importandroid.content.Context;
importandroid.support.v4.app.NotificationCompat;

aspect AppStartNotifier {

    pointcut postInit(): within(Application+) && execution(*Application+.onCreate());

    after() returning: postInit() {
        Application app = (Application) thisJoinPoint.getTarget();
        NotificationManager nmng = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
        nmng.notify(9999, newNotificationCompat.Builder(app)
            .setTicker("Hello AspectJ")
            .setContentTitle("Notification from aspectJ")
            .setContentText("privileged aspect AppAdvice")
            .setSmallIcon(R.drawable.ic_launcher)
            .build());
    }
}