Ask Your Question
4

How can build variables be accessed in code in Kotlin multiplatform?

asked 2023-05-15 23:31:27 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-15 23:43:01 +0000

huitzilopochtli gravatar image

Build variables in Kotlin multiplatform can be accessed by creating a separate Gradle module and defining the variables in the build.gradle file.

In the common module, create an interface for the variable and implement it in the platform-specific modules.

For example, in the common module:

interface BuildConfig {
    val API_KEY: String
}

In the Android module:

object BuildConfigImpl : BuildConfig {
    override val API_KEY: String = com.example.android.BuildConfig.API_KEY
}

In the iOS module:

actual object BuildConfigImpl : BuildConfig {
    actual override val API_KEY: String = NSBundle.mainBundle.objectForInfoDictionaryKey("API_KEY") as String
}

Then, in your code, you can access the build variable like this:

val apiKey = BuildConfigImpl.API_KEY
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-15 23:31:27 +0000

Seen: 20 times

Last updated: May 15 '23