https://kotlinlang.org logo
#gradle
Title
k

Kaj Koivunen

01/12/2023, 2:27 PM
what's the kotlin equivalent to
Copy code
buildConfigField("String", "API_KEY", API_KEY)
and under where should it be placed in the default build script intellij IDEA gives you for a jvm console project? I tried just about anything and it just seems buildConfigField is not recognized on kotlin side of things
s

Sam

01/12/2023, 2:28 PM
buildConfigField
is part of Android, it won’t exist in a basic JVM console app
v

Vampire

01/12/2023, 2:28 PM
buildConfigField
afair is something specific to Android projects, added by the Android Gradle Plugin.
So in a "jvm console project" this will not be available
k

Kaj Koivunen

01/12/2023, 2:29 PM
in that case what would be the ideal way to store an API key in gradle.properties to net get it commited or should I do something else entirely?
m

mbonnin

01/12/2023, 2:30 PM
If you're building an Android, Web or iOS, client, commit your API key, it's not secret anyway
k

Kaj Koivunen

01/12/2023, 2:30 PM
if you read above, I'm not
m

mbonnin

01/12/2023, 2:31 PM
Right sorry
Even for a console project, if you distribute it, you distribute your secret
If you really don't want to commit it, you can create a Gradle task that generates a Kotlin file and add it to your sources
v

Vampire

01/12/2023, 2:32 PM
Doesn't mean it should be in the source repository 😄
m

mbonnin

01/12/2023, 2:32 PM
I'm more and more convinced it should
v

Vampire

01/12/2023, 2:33 PM
But yeah, the question is what you want to do exactly. There are for example online services where you can store your secrets and your application just requests it from there at runtime. If you want to include it in the final application, I usually have a properties file with a placeholder and then use
expand
in
processResources
task configuration to fill the placeholder with the actual value. The code can then read it from that properties file.
k

Kaj Koivunen

01/12/2023, 2:34 PM
in the finished app the api key would be something the user provides themselves through a dialog but in the meantime I was just looking for a way to hold it somewhere not commited but easily usable
I suppose I'm just going to have to get the dialog and input working first if there's no easy way to just have a build variable
v

Vampire

01/12/2023, 2:36 PM
You could e. g. set it as environment variable or system property or commandline argument for the
run
task and then use the
run
task to test your app and read it from where you configured it
if there's no easy way to just have a build variable
The way I described is pretty easy
a

Adam S

01/12/2023, 2:37 PM
what about putting the property in a file,
./src/main/resources/local.properties
, and then adding the file to
.gitignore
? In code you can try and load the file, and if it’s not present, then prompt the user for input?
v

Vampire

01/12/2023, 2:38 PM
Also a possibility, yeah
Many ways lead to rome 😄
a

Adam S

01/12/2023, 2:40 PM
for a more complete way of handling properties with defaults and fallbacks and environment variables, take a look at https://github.com/sksamuel/hoplite
k

kevin.cianfarini

01/12/2023, 9:17 PM
If this is for a CLI project then you should probably be using environment variables
g

gildor

01/13/2023, 2:41 PM
Yep, environment variable or system config
302 Views