Build type specific folder not recognized for a product flavor
Asked Answered
P

2

6

I have configured my project to have multiple product flavors. I have the following code in my module build.gradle:

android {
    // Rest of the configuration omitted for clarity
    buildTypes {
        debug {
            // ...
        }

        release {
            // ...
        }
    }

    productFlavors {
        paid
        free
    }
}

If I create a file (any file: Java, resources, ...), in the paid, free, debug or release, it is recognized by Android Studio and I can use it in my project.

However, if the same file is created in paidDebug (or a similar folder) it is not recognized in Android Studio. Do I need any extra configuration for this to work? Is this not supported (yet)?

Proceed answered 25/1, 2017 at 9:57 Comment(5)
Where did you place the paidDebug folder?Nubbin
It is in app/src/paidDebug/{java,res}. If I replace paidDebug with paid it works as expected.Lorrettalorri
Do you have a corresponding source/resource file in the main tree?Nubbin
No, the files are only for this specific variant. But Android Studio should recognize them anyway, no?Lorrettalorri
Please, see my updated answer.Nubbin
N
8
  1. Source code files with the same name should be placed in all of the alternative source sets (but not in the 'main').

    Either:

    app/src/free/java/com/domain/myapp/MainActivity.java
    app/src/paid/java/com/domain/myapp/MainActivity.java

    Or:

    app/src/freeDebug/java/com/domain/myapp/MainActivity.java
    app/src/freeRelease/java/com/domain/myapp/MainActivity.java
    app/src/paidDebug/java/com/domain/myapp/MainActivity.java
    app/src/paidRelease/java/com/domain/myapp/MainActivity.java

  2. Resource files with the same name can be placed in any source set, including 'main'.

    app/src/main/res/layout/activity_main.xml
    app/src/paidDebug/res/layout/activity_main.xml
    app/src/paidRelease/res/layout/activity_main.xml

    In this case, when building the 'free' flavor, the layout file from 'main' set will be used. But, during the build of the 'paid' flavor, the specific version of the layout will be used.

Nubbin answered 25/1, 2017 at 10:53 Comment(1)
This is what I was looking for! I added alternate source code files in flavor directories, but did not remove it from 'main' so changes were not reflecting. This helped me. Thanks!Revue
T
2

You can specify specific source directories in your build.gradle file. For example, to add testFreeRelease to unit test sources and testFree to android integration test sources:

android {
    [...]
    sourceSets {
        main.java.srcDirs += 'src/main/java'
        testFreeRelease.java.srcDirs += 'src/testFreeRelease/java'
        androidTestFree.java.srcDirs += 'src/androidTestFree/java'
    }
}

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

Tetravalent answered 6/2, 2017 at 19:24 Comment(1)
Thanks for the answer! Had trouble with renaming directories to kotlin for various build variants.Ticket

© 2022 - 2024 — McMap. All rights reserved.