Search code examples
androidkotlingradlegradle-kotlin-dsl

Can I read Gradle properties in the 'settings.gradle.kts' file?


I'm using Gradle Kotlin DSL. I want to know whether it's possible to read Gradle properties in the settings.gradle.kts file?

I have gradle.properties file like this:

nexus_username=something
nexus_password=somepassword

I've tried the following but still it can't read the properties:

dependencyResolutionManagement {
    repositories {
        maven {
            setUrl("https://some.repository/")
            credentials {
                val properties =
                    File(System.getProperty("user.home")+"\\.gradle", "gradle.properties").inputStream().use {
                        java.util.Properties().apply { load(it) }
                    }
                username = properties["nexus_username"].toString()
                password = properties["nexus_password"].toString()
            }
        }
    }
}

Solution

  • You can access values set in gradle.properties in both build.gradle.kts and settings.gradle.kts using delegate properties (Kotlin DSL only, because delegate properties is a Kotlin feature!).

    gradle.properties

    kotlin.code.style=official
    # Your values here
    testValue=coolStuff
    

    build.gradle.kts

    val testValue: String by project
    

    settings.gradle.kts

    val testValue: String by settings