Reputation: 21
Is there any way to apply aspectJ pointcuts from library module to whole project? It means that I need attach library to existitng project, and it must intercept method calls from main project with all it dependencies (also including Android SDK methods) For now I trying to do this by script in build.gradle:
android.libraryVariants.all { variant ->
variant.javaCompile.doLast {
// Find the android.jar and add to iajc classpath
def androidSdk = android.adbExe.parent + "/../platforms/" + android.compileSdkVersion + "/android.jar"
println 'Android SDK android.jar path: ' + androidSdk
def iajcClasspath;
iajcClasspath = androidSdk;
project.rootProject.allprojects.each { proj ->
if (proj.configurations.hasProperty("compile"))
iajcClasspath += ":" + proj.configurations.compile.asPath
// handle aar dependencies pulled in by gradle (Android support library and etc)
tree = fileTree(dir: "${proj.buildDir}/exploded-aar", include: '**/classes.jar')
tree.each { jarFile ->
iajcClasspath += ":" + jarFile
}
}
println 'Classpath for iajc: ' + iajcClasspath
ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
println 'ajctPath : ' + configurations.ajc.asPath.toString()
println 'aspectPath : ' + configurations.aspects.asPath.toString()
println 'inpath : ' + configurations.ajInpath.asPath.toString()
ant.iajc(
source: sourceCompatibility,
target: targetCompatibility,
fork: true,
destDir: "${project.buildDir}/classes/${variant.dirName}",
aspectPath: configurations.aspects.asPath,
inpath: configurations.ajInpath.asPath,
sourceRootCopyFilter: "**/*.java",
classpath: iajcClasspath
) {
sourceroots {
android.sourceSets.main.java.srcDirs.each {
pathelement(location: it.absolutePath)
}
// Build config file
pathelement(location: "${project.buildDir}/source/buildConfig/${variant.dirName}")
// Android resources R.***
pathelement(location: "${project.buildDir}/source/r/${variant.dirName}")
}
}
}
}
But pointcuts works only on methods, called from this module. Also, even if I move script to main build.gradle, and replace android.libraryVariants to android.applicationVariants, pointcuts aren't work on attached .jar libraries and modules, but work on gradle dependencies (such as compile 'com.googlecode.mp4parser:isoparser:1.0.1', for example).
And if there no way to do this with AspectJ, maybe there is some other way to intercept method calls in all project from project library? The most important thing is that there should not be any changes in main module code, just in library.
Upvotes: 2
Views: 928
Reputation: 711
After read document from ant ajcTask, I finally implement with my gradle plugin GradleAndroidAspectJPlugin.
I use the iajc classpath and inpath property to specify which classes(jars) will be compile as classpath or will be re-compile from aspectj compiler.
def aopTask = project.task("compile${buildTypeName}AspectJ") {
doFirst {
project.configurations.aspectsInPath.each {
aspectsInPaths.add(it);
aspectsInPathsAbsolute.add(it.absolutePath);
}
}
doLast {
ant.taskdef(
resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
classpath: project.configurations.aspectjTaskClasspath.asPath
)
ant.iajc(
source: project.android.compileOptions.sourceCompatibility,
target: project.android.compileOptions.targetCompatibility,
fork: "true",
destDir: variant.javaCompile.destinationDir,
bootClasspath: project.android.bootClasspath.join(File.pathSeparator),
inpathDirCopyFilter: "java/**/*.class"
) {
classpath {
variant.javaCompile.classpath.each {
if (!aspectsInPathsAbsolute.contains(it)) {
pathElement(location: it)
}
}
}
inpath {
pathElement(location: copyDir)
aspectsInPaths.each {
if (!it.name.startsWith("aspectjrt")) {
pathElement(location: it)
}
}
}
}
}
}
Upvotes: 1