Hey, Hope you guys are doing well, I just want to ...
# multiplatform
p
Hey, Hope you guys are doing well, I just want to know how can I define an environment variable in the KMM project. I’m an Android Developer so for reference it is similar to
BuildConfig.java
file. But for a good reason I can’t access this file in the project. Could you please help me with this? Thank you!
p
Check
yshrsmz/BuildKonfig
on GitHub , see if good enough
m
I have something similar. I am using a gradle Pluging “com.github.gmazzo.buildconfig” to create class with consts f.e. build.gradle.kts
Copy code
buildConfig {
  val prefix = "CONST_CONFIG_";
  useKotlinOutput { topLevelConstants = true }
  useKotlinOutput { internalVisibility = false }
  className("ConstConfig")
  packageName("de.masterthesis.shared")
  com.android.build.gradle.internal.cxx.configure.gradleLocalProperties(projectDir).filterKeys {
    it.toString().contains(prefix)
  }.map {
    buildConfigField("String", it.key.toString().replace(prefix, ""), it.value.toString())
  }
}
local.properties
Copy code
CONST_CONFIG_DBFILE="task.db"
CONST_CONFIG_NINJAS_API_KEY="xxxx"
after build you can use “ConstConfig.DBFILE”
gradle build task:
p
That's actually a nice way. I was using the
buildKonfig
library but it still lacks the support of accessing the variables defined in
gradle.properties
or maybe I didn't explore it enough. I'll try that. PS: I'm the same person.
p
gradle.properties works, is not related to buildkonfig. Not sure if I understand your problem
p
In Android, you can put sensitive information such as API keys and secrets in the
gradle.properties
file as it's not included in the build and the variables are accessible in the
build.gradle
file to utilise them in the
buildConfigField()
method. But in KMM, I couldn't access those variables in the method. For example: gradle.properties:
Copy code
apiKey="api_key_value"
build.gradle
Copy code
...
buildConfigField("String", "SOME_API_KEY", apiKey)
...
Now, the above example works with Android but
apiKey
is not accessible in KMM.
p
It works for me, I use it in commonMain. Like I said Buildkonfig just copy that value into the config class. The fact that your value is not accessible in build.gradle is a different problem.