yshrsmz / BuildKonfig

BuildConfig for Kotlin Multiplatform Project
Apache License 2.0
729 stars 33 forks source link

Specify flavor in build time #105

Closed Borlehandro closed 11 months ago

Borlehandro commented 11 months ago

Hi there! Are there any way to specify flavor in build time? As I understand it, now plugin get flavor from properties only once when it's applied. But i want to change buildkonfig.flavor dynamically, for example based on my android-app flavor. I try to ext.set("buildkonfig.flavor", <someFlavor>) before :generateBuildKonfig task but it doesn't work for me.

yshrsmz commented 11 months ago

buildkonfig.flavor is just a property, so you can pass it via gradlew command

 ./gradlew build -Pbuildkonfig.flavor=release

See detail here: https://docs.gradle.org/current/userguide/build_environment.html#sec:project_properties

Also there's a note in README: https://github.com/yshrsmz/BuildKonfig#product-flavor

jadar commented 10 months ago

It doesn't seem that there's a ton of guidance on this. Perhaps not everyone has the same build environment that this was designed for. One thing I am trying to figure out is how I would specify the flavor when I am using Cocoapods to keep the library in sync with my Xcode project. It's not super apparent how I can vary the flavor from my Xcode build configuration, through Cocoapods, and to Gradle.

jadar commented 10 months ago

Here is what I have done in the meantime. However, this doesn't seem to leverage the actual "flavor" feature.

buildkonfig {
    packageName = "com.example.mykmpproject"

    defaultConfigs {
        when (project.findProperty(KotlinCocoapodsPlugin.CONFIGURATION_PROPERTY)) {
            "Debug" ->
                buildConfigField(FieldSpec.Type.STRING, "defaultEnvironment", "Debug", const = true)
            "Beta  " ->
                buildConfigField(FieldSpec.Type.STRING, "defaultEnvironment", "Beta", const = true)
            else ->
                buildConfigField(FieldSpec.Type.STRING, "defaultEnvironment", "Production", const = true)
        }
    }
}
yshrsmz commented 10 months ago

For the OP case, you can get an actual task name that a user passes to gradlew via project.gradle.startParameter.taskNames. So it's possible to dynamically change the value of buildkonfig.flavor property.

yshrsmz commented 10 months ago

For cocoapods, please open a separate issue as this one is already closed. (And I'm not sure how Cocoapods & Kotlin Cocoapods Plugin build the project.)

yshrsmz commented 10 months ago

Or why not just update buildkonfig.flavor property with that project.findProperty(KotlinCocoapodsPlugin.CONFIGURATION_PROPERTY) value? I think that should work.

jadar commented 10 months ago

Ah, that worked well. I put a setProperty based on that value I was using before, and now I can use buildkonfig as it was designed. Thanks!