How to add a source directory to an Android Gradle project's source set?

android
plugins

(Alexey Romanov) #1

My Gradle plugin does this:

project.afterEvaluate {
    File outDir = project.file("${project.buildDir}/generated/odmf")
    // will be SourceSet or AndroidSourceSet
    def main
    Set<File> assets
    if (project.hasProperty("android")) {
        main = project.android.sourceSets.main
        assets = main.assets.srcDirs as Set<File>
    } else {
        main = project.sourceSets.main
        assets = main.resources.srcDirs as Set<File>
    }

    main.java.srcDir(outDir)

    // declare task which will generate Java source in outDir
}

in order to add a generated source directory in both java and com.android.application projects. The files get generated in both cases, but gradlew build only sees and compiles the generated files in Java projects, not in Android ones. Looking at the documentation, it looks like this should work for Android as well. Why doesn’t it and how can I fix the problem?