Test Source Directories for Multi Dimension Flavors not recognized in Android Studio
Asked Answered
E

1

6

I have an issue with test and androidTest source directories for multi-dimension flavors.

Given the following flavors:

flavorDimensions "taste", "serving"

productFlavors {
    chocolate {
        flavorDimension "taste"
    }
    strawberry {
        flavorDimension "taste"
    }
    kiwi {
        flavorDimension "taste"
    }
    sample {
        flavorDimension "serving"
    }
    whole {
        flavorDimension "serving"
    }
}

There are no issues with "non-test" source directories (of any flavor combination) being recognized in Android Studio:

src/sample, src/whole, src/chocolate, src/strawberry, src/kiwi, src/chocolateSample, src/chocolateWhole, src/strawberrySample, src/strawberryWhole, src/kiwiSample, src/kiwiWhole

My issue is with "test" source directories.
Only single dimension flavors are recognized: src/testSample, src/testWhole, src/testChocolate, src/testStrawberry, src/testKiwi.
Multi-Dimension flavors are not: src/testChocolateSample, src/testChocolateWhole, src/testStrawberrySample, src/testStrawberryWhole, src/testKiwiSample, src/testKiwiWhole

This is also the case for the "androidTest" equivalent source directories.

I am under the impression that it is the app.iml which is not correctly generated. While understanding that we should NEVER do this, the folders are correctly recognized if I were to manually add in the missing entries.

Why should this work with non-test source directories but fail with test source directories? Is this a known issue or a limitation by the gradle plugin?

I tried researching this, but only found topics related single flavor dimensions for test source folders or mult-dimension flavors for non-test source folders. There is nothing with regards to multi-dimension flavors for test source folders.

Ed answered 29/6, 2015 at 15:25 Comment(4)
Did you managed to solve this problem somehow?Yonder
Answered in my post on GoogleGroups: hereEd
Yes. Need to update your gradle to at least 'com.android.tools.build:gradle:1.3.0'Ed
Hi, I am having the exact same issue now and I'm using 'com.android.tools.build:gradle:4.0.1'. Do you have any ideea how it might work now?Varietal
S
3

You should be able to fix this by modifying your android app build.gradle file. For example, to add testChocolateSample to unit tests and testChocolate to android integration tests:

android {
    [...]
    sourceSets {
        main.java.srcDirs += 'src/main/java'
        testChocolateSample.java.srcDirs += 'src/testChocolateSample/java'
        androidTestChocolate.java.srcDirs += 'src/androidTestChocolate/java'
    }
}

This also works for Kotlin. Just switch java with kotlin.

Stamin answered 6/2, 2017 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.