justrying
justrying

Reputation: 109

How do you setup variables in build.gradle.kts

In build.gradle I Know you can do this

ext {
    dbUsername = System.getenv("DB_USER").toString()
    dbPassword = System.getenv("DB_PASS").toString()
    libsUserNameNew = System.getenv("LIBS_USERNAME_NEW").toString()
    libsPassNew = System.getenv("LIBS_PASS_NEW").toString()
    gitShortTag = System.getenv("SHORT_SHA").toString()
    repoName = System.getenv("REPO_NAME").toString()
    group = "app.test.customerservicepoc"
    mainClass = "app.test.customerservicepoc.CustomerServicePOC"
}

How can I achieve the same using build.gradle.kts This is what I have tried

var dbUsername =""
var dbPassword =""
var LibsUserNameNew = ""
var LibsPassNew = ""
var gitShortTag  = ""
var repoName = ""

and then

ext {
    dbUsername = System.getenv("DB_USER").toString()
    dbPassword = System.getenv("DB_PASS").toString()
    kyoskLibsUserNameNew = System.getenv("LIBS_USERNAME_NEW").toString()
    LibsPassNew = System.getenv("LIBS_PASS_NEW").toString()
    gitShortTag = System.getenv("SHORT_SHA").toString()
    repoName = System.getenv("REPO_NAME").toString()
    group = "app.test.mms"
}

during build I end up getting errors

  • What went wrong: 945 Cannot invoke "String.toString()" because the return value of "org.gradle.internal.classpath.Instrumented.getenv(String, String)" is null

I am migrating the project to kotlin gradle, how can I define the variables in kotlin gradle?

Upvotes: 2

Views: 1695

Answers (1)

Martin Marconcini
Martin Marconcini

Reputation: 27226

Well, the issue is shown in the other question, System.getenv is a static java method that doesn't guarantee the nullability of the returned String. In Kotlin, this is a compilation error. So what the compiler is telling you is "I cannot guarantee that calling toString() on the returned string won't crash with a NullPointerException because the JVM method getenv doesn't guarantee it". I'd argue that you don't need the toString() call at all.

lateinit var db: String
// or
var db: String? = null //this can now be null


//then
ext {
    db = System.getenv("YOUR_DB") ?: "" //UPDATE: you also need this since getenv can return null, or you need to make the variable `String?`
//or
    db = System.getenv("YOUR_DB") ?: "" // since it can be null, set some fallback, like empty or null
}

This should work no problem.

UPDATE

I've created a new empty android project with compose (which uses build.gradle.kt instead of groovy), and added this:

lateinit var something1: String
var something2: String? = null

ext {
    something1 = System.getenv("something") ?: ""
    something2 = System.getenv("something2") ?: ""
}

Image of Android Studio editor with a short code snippet

It compiled no problem. (And I'd assume it would work if the environment variables were set).

BUILD SUCCESSFUL in 25s

Update 2 a real test

I've actually done this

lateinit var something1: String
var something2: String? = null

ext {
    something1 = System.getenv("SHELL") ?: ""
    something2 = System.getenv("SHELL") ?: ""
}

tasks.register("printSomething") {
    println("Something 1 is $something1")
    println("Something 2 is $something2")
}

Then I ran ./gradle printSomething

And here's the result:

Image of terminal emulator where gradlew printsomething was executed and the output is as expected according the code snipet above this image.

Upvotes: 2

Related Questions