有没有办法将图书馆模块中的aspectJ切入点应用到整个项目?这意味着我需要附加库来存在项目,它必须拦截来自主项目的方法调用及其所有依赖项(也包括Android SDK方法) 现在我尝试通过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}")
}
}
}
}
但是切入点仅适用于从此模块调用的方法。此外,即使我将脚本移动到main build.gradle,并将android.libraryVariants替换为android.applicationVariants,切入点也不适用于附加的.jar库和模块,但是处理gradle依赖项(例如compile&#39) ; com.googlecode.mp4parser:isoparser:例如1.0.1')。
如果没有办法用AspectJ做这个,也许还有其他方法可以拦截项目库中所有项目中的方法调用?最重要的是,主模块代码中不应该有任何更改,只需在库中。
答案 0 :(得分:1)
在阅读了来自ant ajcTask的文档后,我终于使用我的gradle插件GradleAndroidAspectJPlugin实现了。
我使用iajc classpath和inpath属性指定哪些类(jar)将作为类路径编译,或者将从aspectj编译器重新编译。
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)
}
}
}
}
}
}