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
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
Asked: 2023-05-15 23:31:27 +0000
Seen: 20 times
Last updated: May 15 '23
Why can't IntelliJ IDEA detect the Kotlin multiplatform library?
How do compileSdkVersion and targetSdkVersion differ from each other?
How can we add animation to the user interface using jetpack compose?
What is the correlation between coroutines and the Kotlin Gradle Plugin?
How can JTS Topology Suite be integrated with Android Studio?