#Inject value on Gradle build
1 messages · Page 1 of 1 (latest)
<@&987246554085740594> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Inject into actual project code?
Don't think so unless you're okay with manually parsing your file and replacing text
I'm not aware of a tool to directly inject values into source
But you could use java.util.Properties to generate and save name during compile time and then load during runtime
something like this
// ./gradlew someTask -Dname="John"
tasks.named('someTask') {
doLast {
def properties = new Properties()
properties.setProperty('name', getProperty('name'))
layout.buildDirectory.file("x.properties").withWriter { writer -> properties.write(writer, '') }
}
}
you can, hypothetically, do this with any precompile step
android has their R.* convention that works similarly
but if you don't want to add a separate kind of build step or load at runtime you could build an annotation processor which generates a source file holding the constant
This is not a bad idea, thank you so much. 🙂