Perry Hoekstra
Perry Hoekstra

Reputation: 2783

Access build.gradle.kt values

The question was: how to access from your Kotlin code, a arbitrary value that exists in build.gradle.kt?

If you have the following entry in my build.gradle.kt file (trying to add a feature flag):

android {
    ...


buildTypes {
    getByName("debug") {
        buildConfigField("Boolean", "FOO", "true")
    }
    getByName("release") {
        buildConfigField("Boolean", "FOO", "false")
    }
}
    ...
}

A way to access the value of FOO within your Kotlin code is:

class FeatureFlag {
    fun isFeatureEnabled(): Boolean = BuildConfig.FOO
}

Upvotes: 2

Views: 1118

Answers (1)

Tenfour04
Tenfour04

Reputation: 93834

Use buildConfigField instead of resValue. Same arguments. Then in Kotlin code:

class FeatureFlag {
    fun isFeatureEnabled(): Boolean =
        BuildConfig.FOO.toBoolean()
}

Although I'm not sure why you don't just use a "boolean" type to begin with instead of a String that you have to convert to Boolean at runtime.

Upvotes: 1

Related Questions