Define different source sets for different flavors, such as dev
and prod
, in the platform-specific modules (e.g., androidMainDev
, androidMainProd
).
// Common module
expect class DataManager {
fun fetchData(): String
}
// Android platform module
class AndroidDevDataManager : DataManager {
override fun fetchData(): String {
return "Data for development"
}
}
class AndroidProdDataManager : DataManager {
override fun fetchData(): String {
return "Data for production"
}
}
In this example, DataManager is an interface declared in the common module using an expect
keyword. Platform-specific implementations are provided in the Android platform module using the actual keyword.
Then, in your Android project, you can configure different source sets for dev and prod flavors in the build.gradle.kts
:
kotlin {
android {
// Configuration for dev flavor
sourceSets {
named("androidMainDev") {
kotlin.srcDirs("src/androidMainDev/kotlin")
}
}
// Configuration for prod flavor
sourceSets {
named("androidMainProd") {
kotlin.srcDirs("src/androidMainProd/kotlin")
}
}
}
}