在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Archinamon/android-gradle-aspectj开源软件地址(OpenSource Url):https://github.com/Archinamon/android-gradle-aspectj开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):GradleAspectJ-AndroidA Gradle plugin which enables AspectJ for Android builds.
Supports writing code with AspectJ-lang in Actual version supporting of AGP 4.1.+: 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 featuresAugments Java, Kotlin, Groovy bytecode simultaneously! It is easy to isolate your code with aspect classes, that will be simply injected via cross-point functions, named 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.
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. UsageFirst add a maven repo link into your mavenCentral() Don't forget to add Add the plugin to your Kotlinclasspath("com.archinamon:android-gradle-aspectj:4.3.0") Groovyclasspath 'com.archinamon:android-gradle-aspectj:4.3.0' Apply the `aspectj` plugin: Kotlinplugins {
id("com.android.application")
id("com.archinamon.aspectj")
} Groovyplugins {
id 'com.android.application'
id 'com.archinamon.aspectj'
} Now you can write aspects using annotation style or native (even without IntelliJ IDEA Ultimate edition). Let's write simple Application advice: import android.app.Application;
import android.app.NotificationManager;
import android.content.Context;
import android.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, new NotificationCompat.Builder(app)
.setTicker("Hello AspectJ")
.setContentTitle("Notification from aspectJ")
.setContentText("privileged aspect AppAdvice")
.setSmallIcon(R.drawable.ic_launcher)
.build());
}
} Tune extensionKotlinaspectj {
compileTests = true // default value
ajc = "1.9.4" // default value
java = JavaVersion.VERSION_1_7 // default value
/* @see Ext plugin config **/
includeAllJars = false // default value
includeJar.addAll(arrayOf("design", "support-v4", "dagger")) // default is empty
excludeJar.addAll(arrayOf("support-v7", "joda")) // default is empty
extendClasspath = true // default value
includeAspectsFromJar.addAll(arrayOf("my-aj-logger-lib", "any-other-libs-with-aspects")) // default is empty
ajcArgs.apply {
add("-warn:deprecation")
add("-referenceInfo")
}
weaveInfo = true // default value
debugInfo = false // default value
addSerialVersionUID = false // default value
noInlineAround = false // default value
ignoreErrors = false // default value
breakOnError = true // default value
experimental = false // default value
buildTimeLog = true // default value
transformLogFile = "ajc-transform.log" // default value
compilationLogFile = "ajc-compile.log" // default value
} Groovyaspectj {
dryRun false // default value
compileTests true // default value
ajc '1.9.4' // default value
java = JavaVersion.VERSION_1_7 // default value
/* @see Ext plugin config **/
includeAllJars false // default value
includeJar 'design', 'support-v4', 'dagger' // default is empty
excludeJar 'support-v7', 'joda' // default is empty
extendClasspath true // default value
includeAspectsFromJar 'my-aj-logger-lib', 'any-other-libs-with-aspects' // default is empty
ajcArgs << '-referenceInfo' << '-warn:deprecation'
weaveInfo true // default value
debugInfo false // default value
addSerialVersionUID false // default value
noInlineAround false // default value
ignoreErrors false // default value
breakOnError true // default value
experimental false // default value
buildTimeLog true // default value
transformLogFile 'ajc-transform.log' // default value
compilationLogFile 'ajc-compile.log' // default value
} Note that you may not include all these options! All the extension parameters are have default values (all of them are described above, except of includeJar/Aspects/ajcArgs options). So no need to define them manually.
Extended plugin configKotlinplugins {
id("com.android.application")
id("com.archinamon.aspectj-ext")
} Groovyplugins {
id 'com.android.application'
id 'com.archinamon.aspectj-ext'
} Ext config: - allows usage of `includeJar` and `includeAllJars` parameters, with workaround to avoid `Multiple dex files exception` - supports `multiDex` - supports `Instrumented tests` Currently it has some limitations:
Provider plugin configKotlinplugins {
id("com.android.application")
id("com.archinamon.aspectj-provides")
} Groovyplugins {
id 'com.android.application'
id 'com.archinamon.aspectj-provides'
} Plugin-provider may be useful for that cases when you need to extract aspect-sources into separate module and include it on demand to that modules where you only need it. Therefor this behavior will save you build-time due to bypassing aspectj-transformers in provide-only modules. You ain't limited to describe as much provider-modules as you need and then include them using With example project you could learn how to write such provider-module. DryRun plugin configKotlinplugins {
id("com.android.application")
id("com.archinamon.aspectj-dryRun")
} Groovyplugins {
id 'com.android.application'
id 'com.archinamon.aspectj-dryRun'
} Disables aspectj-compiler and transformation task for the hole project. Working testsKotlinplugins {
id("com.android.application")
id("com.archinamon.aspectj-junit")
} Groovyplugins {
id 'com.android.application'
id 'com.archinamon.aspectj-junit'
} Test scope overloads JUnit compilation flow with AJC instead of JavaC. So any aspects has been written within `test` directory will be compiled with all java sources and aspects will weave them if need. ProGuardCorrect tuning will depends on your own usage of aspect classes. So if you declares inter-type injections you'll have to predict side-effects and define your annotations/interfaces which you inject into java classes/methods/etc. in proguard config. Basic rules you'll need to declare for your project:
If you will face problems with lambda factories, you may need to explicitly suppress them. That could happen not in aspect classes but in any arbitrary java-class if you're using Retrolambda. So concrete rule is:
Changelog4.2.1 -- Improve jar archives
4.2.0 -- Support AGP 4.0.+
4.1.0 -- Support AGP 3.6.+
4.0.1 -- Fix synchronous run
4.0.0 -- Support AGP 3.5.+
3.4.5 -- Fix for Gradle 6.0
3.4.3 -- Once more fix :(
3.4.2 -- Hotfix provides
3.4.1 -- Fix provides
3.4.0 -- Better DryRun mode
3.3.12 -- Fix 'Dependencies resolution fail'
3.3.11 -- Fix legacy AGP support
3.3.10 -- Update AJC
3.3.9 -- Small fix dryRun
3.3.8 -- Fix unitTest variant
3.3.7 -- Fixes ext plugin
3.3.6 -- Fixes
3.3.5 -- Dry run
3.3.3 -- Support AGP 3.3.+
3.3.0 -- JUnit tests support
3.2.0 -- Gradle 3.0.0 support
3.1.1 -- Useful improvements
3.1.0 -- Provider
3.0.3 -- Minor fixes
3.0.0 -- Grand refactoring in Kotlin
2.4.3 -- Hot-fixed two-step compilation
2.4.2 -- Hot-fix
2.4.0 -- Added aspectj-ext plugin
2.3.1 -- New two-step build mechanic
2.3.0 -- Major fixes
|