Hey, i was wondering if one could use a variable w...
# gradle
a
Hey, i was wondering if one could use a variable which is used/defined in the
gradle.build.kts
(e.g. val
versionNumber  = "1.0.1"
) in code. Would be nice to set the version in one place and use it for the build and for an about page or something similar
s
Generate a file with it and include it from your code
☝️ 1
m
If you're on android, you also have buildconfigfield
👍 1
a
I now create a file in resources and read the version number from it. Although this works fine, i thought there would be a more direct way to get a variable ... thanks anyway
After playing around a little bit i changed it from a version.txt in resources to a kotlin object which holds the version number and compileKotlin depends on this task.
t
if you put an object in buildSrc all your build files will be able to read from that. For accessing that from your application, you need to write that to some sort of properties file. For using version number there is
gradle.properties
and a bunch of plugins that takes advantage on that, and it generates a manifest you can read from application.
m
You could even generate some kotlin file with kotlinpoet and add that to the kotlin sourceSet. That way you'll be able to Ctrl-click in the IDE and have type safety
a
This is more or less exactly what i imagined in the first place
d
You can use processResources task. Make a resource file with the content
@project.version@
and register the
project.version
token with the current version string in the processResources task.
This does not work when running/building with IntelliJ. I reckon other solutions here dont either. You can work around this by adding a URL to the build/processedResources directory to the system class loader at runtime.