How are gradle extra properties set in the Kotlin DSL?

前端 未结 1 958
轻奢々
轻奢々 2021-02-13 04:49

I\'m trying to organize my build files as I would in groovy, by having values in a separate file to reuse. But I cannot understand the syntax to do the same thing in the kotlin

相关标签:
1条回答
  • 2021-02-13 05:18

    A correct fix: Gradle collects and applies the buildscript { ... } blocks from your script strictly before executing anything else from it. So, to make your properties from config.gradle.kts available inside the buildscript, you should move applyFrom("config.gradle.kts") to your buildscript { ... } block:

    buildscript {
        applyFrom("config.gradle.kts")
    
        /* ... */
    }
    

    Another possible mistake is using an extra property as extra["minSdkVer"] in a scope of another ExtensionAware, like a task in this example:

    val myTask = task("printMinSdkVer") {
        doLast {
            println("Extra property value: ${extra["minSdkVer"]}")
        }
    }
    

    In this case, extra.get(...) uses not the project.extra but the extra of the task.

    To fix that, specify that you work with the project. Direct usage:

    println(project.extra["minSdkVer"])
    

    And for delegation.

    val minSdkVer by project.extra
    
    0 讨论(0)
提交回复
热议问题