yshrsmz / BuildKonfig

BuildConfig for Kotlin Multiplatform Project
Apache License 2.0
782 stars 32 forks source link

Update readme to show how to configure using new gradle plugin syntax #41

Closed dalewking closed 3 years ago

dalewking commented 4 years ago

The readme shows the old way to configure the plugin using classpath inside buildscript, but since you are publishing in the gradle plugin portal there is no reason to so it that way. The readme should reflect the prefered way using the new plugin syntax.

dalewking commented 4 years ago

I've figured it out how to make it work in gradle kotlin dsl:

You can apply the plugin with:

plugins {
    kotlin("multiplatform")
    id("com.codingfeline.buildkonfig")
}

I added these imports to the file:

import com.codingfeline.buildkonfig.compiler.FieldSpec.Type
import com.codingfeline.buildkonfig.gradle.BuildKonfigExtension
import com.codingfeline.buildkonfig.gradle.TargetConfigDsl

And here is what I did to configure:

configure<BuildKonfigExtension> {
    packageName = "com.genesys.purecloud.wfmshared"

    defaultConfigs {
        buildConfigField(Type.STRING, "OAUTH_CLIENT_ID", "")
        buildConfigField(Type.STRING, "OAUTH_CLIENT_SECRET", "")
        buildConfigField(Type.STRING, "DEFAULT_ENVIRONMENT",
            if(publicReleaseBuild) "PRODUCTION" else "DEVELOPMENT")
        buildConfigField(Type.BOOLEAN, "ALLOW_ENVIRONMENT_SELECTION",
            (!publicReleaseBuild).toString())
    }

    targetConfigs("android", closureOf<TargetConfigDsl> {
        buildConfigField(Type.STRING, "OAUTH_CLIENT_ID",
            project.properties["oauth_client_id_android"].toString())
        buildConfigField(Type.STRING, "OAUTH_CLIENT_SECRET",
            project.properties["oauth_client_secret_android"].toString())
    })
    targetConfigs("ios", closureOf<TargetConfigDsl> {
        buildConfigField(Type.STRING, "OAUTH_CLIENT_ID",
            project.properties["oauth_client_id_ios"].toString())
        buildConfigField(Type.STRING, "OAUTH_CLIENT_SECRET",
            project.properties["oauth_client_secret_ios"].toString())
    })
}

This is hampered by the fact that for target configs you are using Closure, which is Groovy specific and requires the configureOf wrapper. As noted here you should use Action, not Closure. You did that on defaultConfig but not on targetConfigs.

dalewking commented 4 years ago

And I guess I should have tested that before posting as that fails with: org.gradle.api.internal.FactoryNamedDomainObjectContainer_Decorated cannot be cast to com.codingfeline.buildkonfig.gradle.TargetConfigDsl

dalewking commented 4 years ago

OK, this works, but once again would be simplified by you switching to Action from Closure:

targetConfigs(closureOf<NamedDomainObjectContainer<TargetConfigDsl>> {
    create("android") {
        buildConfigField(
            Type.STRING, "OAUTH_CLIENT_ID",
            project.properties["oauth_client_id_android"].toString()
        )
        buildConfigField(
            Type.STRING, "OAUTH_CLIENT_SECRET",
            project.properties["oauth_client_secret_android"].toString()
        )
    }
    create("ios") {
        buildConfigField(
            Type.STRING, "OAUTH_CLIENT_ID",
            project.properties["oauth_client_id_ios"].toString()
        )
        buildConfigField(
            Type.STRING, "OAUTH_CLIENT_SECRET",
            project.properties["oauth_client_secret_ios"].toString()
        )
    }
})