does anyone know how can we get the version number...
# multiplatform
p
does anyone know how can we get the version number of the application stored on build gradle for showing it during app execution?
Copy code
compose.desktop {
    application {
        mainClass = "com.valenbyte.valenbushelpercompose.MainKt"
        nativeDistributions {
            packageVersion = "1.0.0"
I mean that "packageVersion" value. BuildConfig is not available under Desktop for example.
same 1
p
đź’Ż 1
p
maybe is there a way to do it without a third party library? using proguard has become a nightmare with KMP Desktop, I'm 3 days stuck trying to obfuscate code and I'm starting to think that it's impossible. Each time I must add a third party library for a very simple feature like this I feel panic.
m
I do it without third-party tools. I add a JSON config file as a resource to my code. I can then read that at runtime inside my code, e.g., to show a version number. I can also read it inside the build.gradle.kts file at build time and can configure the build with it. This is an example copied from my build file:
Copy code
import kotlinx.serialization.json.*

buildscript {
    dependencies {
        classpath(libs.kotlinx.serialization.json)
    }
}

val appConfig = Json.decodeFromString<JsonObject>(project.file("src/commonMain/composeResources/files/config.json").readText());
val release_vendor    = appConfig["release"]?.jsonObject?.get("vendor")?.jsonPrimitive?.content
val release_appname   = appConfig["release"]?.jsonObject?.get("appname")?.jsonPrimitive?.content
val release_copyright = appConfig["release"]?.jsonObject?.get("copyright")?.jsonPrimitive?.content
val release_version   = appConfig["release"]?.jsonObject?.get("version")?.jsonPrimitive?.content
val release_build     = appConfig["release"]?.jsonObject?.get("build")?.jsonPrimitive?.content

logger.quiet("Application - release.vendor  : $release_vendor")
logger.quiet("Application - release.appname : $release_appname")
logger.quiet("Application - release.version : $release_version")
logger.quiet("Application - release.build   : $release_build")
I don’t consider Kotlin JSON serialization as a third-party lib here because I need it in the code anyway.
p
maybe is possible to do it setting the variables in composeResources/values/strings.xml? app_version, app_name etc... and read them from the build.gradle.kts doing some trick like the one you did to read from a json file?
if that is possible it whould be even better and simpler
m
That’s probably possible but not without an extra library which I otherwise won’t need.