The SourceSet 'commonTest' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?
Asked Answered
S

1

13

Seemingly the same root cause as this question, but the answer there will not work for me. I have a custom source set called commonTest that I use for sharing certain test utility code across the test and androidTest source sets. Here is my relevant project config:

sourceSets {
    // This lets us write test utility code that can be used by both unit tests and android tests
    commonTest {
        java
    }
    test {
        java.srcDirs += commonTest.java.srcDirs
    }
    androidTest {
        java.srcDirs += commonTest.java.srcDirs
    }
}

This worked fine with AGP 3.0.1, but breaks on AGP 3.1.0. Are custom source sets no longer supported?

Stammel answered 1/4, 2018 at 18:22 Comment(8)
Why do you need a source set for this? Can't this just be a common Java directory that you add via java.srcDirs? Is there something that you get from having it be a source set?Musket
Can you show me an example of that? I've used the source set approach successfully for years.Stammel
I think the main thing I get is that it "just works" (in the past) and my IDE recognizes it (in the past) as a java source set and renders it prettily.Stammel
See how you add Room schemas to assets. That's not in a separate source set. I can't find an example on my development machine of doing this with Java code; usually I just have that stuff be in a separate module.Musket
Ok, but it still doesn't answer my original question, which is "are custom source sets no longer supported?" For the room stuff, looks like you're referring to this: ``` sourceSets { androidTest.assets.srcDirs += files("$projectDir/schemas".toString()) } ```Stammel
Yes, that's what I'm referring to. $projectDir/schemas is not in a source set, yet we add it anyway. This seems to cover your concern, and this seems to be an outstanding issue on the subject.Musket
@Stammel Hi have you managed to create custom sourceSet because am also strugling with this one?Electrolysis
Nope, gave up on that, although I'm sure it's somehow possible. What I do nowadays is just a separate test-only module and add it as a project dependency like testImplementation(project(":android-test-utils"))Stammel
G
9

Hate to be a necromancer here, but I encountered this problem and found a solution for this, so I thought I'd share.

Android does not allow you to define custom sourcesets anymore like you still can in regular gradle projects. This is probably because it would interfere with flavors, buildtypes and all the generated interdependent stuff that goes along with those. The default sourcesets are: main, debug and release and if you really want another one, you could add a buildtype or a flavor for which android then creates an accompanying sourceset. Here someone gives an example of that.

In your case however it might be better to simply pull out the code into a separate (java) library and add a dependency on it in your android project. This is also what I ended up doing after trying to put multiple things into one project like I would with regular java/gradle projects. You can see here how to set up a separate library using a subproject in case you didn't know about those.

Gargan answered 4/8, 2022 at 7:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.