Using Gradle and AspectJ with Android products that have their own source

I searched a lot but didn't see an answer to this. I have a source that comes in different flavors. Something like:

App/src/flavorA/MyFlavor.java
App/src/flavorB/MyFlavor.java
App/src/flavorC/MyFlavor.java

      

And it works fine until I need to run my AOP step. I found this was not possible because he could not find this class referenced by the source from App / src / main /.

This doesn't find the MyFlavor class:

android.applicationVariants.all { variant ->

    variant.javaCompile.doLast {
        def androidSdk = android.adbExe.parent + "/../platforms/" + project.ext.android_version + "/android.jar"
        def iajcClasspath = configurations.compile.asPath + ";" + androidSdk

        // This add project jars as well as support jars.
        project.ext.tree = fileTree(dir: "${project.buildDir}/intermediates/exploded-aar", include: '**/*.jar')
        project.ext.tree.each { jarFile ->
            iajcClasspath += ":" + jarFile
        }

        ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
                classpath: configurations.ajc.asPath)
        ant.iajc(
                source: "1.6",
                target: "1.6",
                destDir: "${project.buildDir}/intermediates/classes/${variant.dirName}",
                fork: "true",
                maxmem: "512m",
                aspectPath: configurations.aspects.asPath,
                inpath: configurations.ajInpath.asPath,
                sourceRootCopyFilter: "**/.svn/*,**/*.java",
                classpath: iajcClasspath
        ) {
            sourceroots {
                android.sourceSets.main.java.srcDirs.each {
                    pathelement(location: it.absolutePath)
                }

                pathelement(location: "${project.buildDir}/generated/source/r/${variant.dirName}")
                pathelement(location: "${project.buildDir}/generated/source/buildConfig/${variant.dirName}")
            }
        }
    }
}

      

I can get it to work if I add something like:

pathelement(location: "$`enter code here`{project.buildDir}/../src/flavorA/java")

      

But that's if I just want to create flavorA. Is there a better way to set it up so that when doing an IAJC task, it can find the source it needs for the specific variation it creates? If I build FlavorARelease my variant name will be something like flavorARelease and I can get the build type "release" by doing variant.buildType.name, but that won't help me. I need to point to the source for the scent I am creating.

I took variant.name and subtracted the assembly type name from it, which was left with the "flavorA" part:

pathelement(location: "${project.buildDir}/../src/${variant.name.substring(0, variant.name.length() - variant.buildType.name.length())}/java")

      

Still seems awkward. What if I want to collectRelease and build all flavors? There must be a better way to approach this which I cannot see.

+3


source to share


1 answer


You can iterate over the available options for your specific option using: def currentFlavor; variant.productFlavors.each { flavor -> flavorToChoose = flavor }

From here, you can take your desired flavor from the list and then add a path element similar to the "main" source, for example:



android.sourceSets."${flavorToChoose}".java.srcDirs.each { pathelement(location: it.absolutePath) }

Be sure to stick to this block in the sourceroots section.

0


source







All Articles