Home > Blockchain >  How do you setup variables in build.gradle.kts
How do you setup variables in build.gradle.kts

Time:01-11

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?

CodePudding user response:

Well, the issue is shown in 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.

  • Related