Search code examples
androidkotlinmavengradlegradle-kotlin-dsl

How can I access a gradle.properties variables using the new Gradle Kotlin DSL


I have to use these credentials to access a maven repo in my gradle.properties:

repositoryUsername=user_name_placeholder
repositoryIdentityToken=identity_token_placeholder

Then, I need to use them in the setting.gradle.kts file like this:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
            credentials {
                username = repositoryUsername
                password = repositoryIdentityToken
            }
            url = uri("https://some-repository/artifactory/maven")
        }
    }
}

But that is not working, how does it work using the new Gradle Kotlin DSL?


Solution

  • In order to use variables in your gradle.properties like the ones you are asking:

    repositoryUsername=user_name_placeholder
    repositoryIdentityToken=identity_token_placeholder
    

    This is the code that you need to write in your setting.gradle.kts:

    pluginManagement {
        repositories {
            google()
            mavenCentral()
            gradlePluginPortal()
        }
    }
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
            maven {
                credentials {
                    username = providers.gradleProperty("repositoryUsername").get()
                    password = providers.gradleProperty("repositoryIdentityToken").get()
                }
                url = uri("https://repositories.tomtom.com/artifactory/maven")
            }
        }
    }
    

    I've found the information in the official gradle documentation example 1 and example 2: https://docs.gradle.org/current/userguide/build_environment.html