How to define custom source set in android gradle.kts?

829 Views Asked by At

I want to release my application in three stores. I have the rate section and update dialog in my application (which has a button to go to the store for update application). to manage these features I decide to make three flavors and three sources set to have different implementations of rate and update sections in different kotlin file. this is my Gradle file:

android {
    flavorDimensions("appStores")
    productFlavors{
        create("p1Flavor"){
            dimension="appStores"
            sourceSets {
                create("p1SourceSet"){
                    java.srcDirs("src/main/java","src/firstStoreDir")
                }
            }
        }
        create("p2Flavor"){
            dimension="appStores"
            sourceSets {
                create("p2SourceSet"){
                    java.srcDirs("src/main/java","src/secondStoreDir")
                }
            }
        }
        create("p3Flavor"){
            dimension="appStores"

            sourceSets {
                create("p3SourceSet"){
                   java.srcDirs("src/main/java","src/thirdStoreDir")
                }
            }
        }
    }
}

I want to add a directory that contains two files (updateStore.kt and rate.kt) to src for every flavor. I mean I have three directories with three different names which every directory contains updateStore.kt and rate.kt with different implementations.

when I build my project I get this error:

The SourceSet 'p1SourceSet' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?

What should I do?

1

There are 1 best solutions below

0
On

I realized that using source set is not appropriate for this situation and i decide to use buildConfigField.

android {
    flavorDimensions("appStores")
    productFlavors{
        create("p1Flavor"){
            dimension="appStores"
            buildConfigField("String","APP_STORE_NAME","\"p1store\"")
        }
        create("p2Flavor"){
            buildConfigField("String","APP_STORE_NAME","\"p2store\"")
            dimension="appStores"
        }
        create("p3Flavor"){
            buildConfigField("String","APP_STORE_NAME","\"p3store\"")
            dimension="appStores"
        }
    }
}

and every where that i need to understand which flavor is selected, i use these codes :

when(BuildConfig.APP_STORE_NAME){
        "p1store" -> {
            //TODO
        }
        "p2store" -> {
            //TODO
        }
        "p3store" ->{
            //TODO
        }
    }