Search code examples
androidandroid-studiogradlemapboxmapbox-android

Can't pass token from gradle.properties to settings.gradle


I followed the guide at https://docs.mapbox.com/android/maps/guides/install/ and everything seems to work except that I can't pick the token stored in gradle.properties to authenticate myself on the Mapbox Maven repository in settings.gradle. The methods shown in the installation guide are not working for me.

Here is my settings.gradle:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
            url 'https://api.mapbox.com/downloads/v2/releases/maven'
            authentication {
                basic(BasicAuthentication)
            }
            credentials {
                // Do not change the username below.
                // This should always be `mapbox` (not your username).
                username = "mapbox"
                // Use the secret token you stored in gradle.properties as the password
                password = System.getenv("MAPBOX_DOWNLOADS_TOKEN")
            }
        }
        jcenter() // Warning: this repository is going to shut down soon
    }
}
rootProject.name = "MyApp Name"
include ':app'

And here is my gradle.properties:

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
MAPBOX_DOWNLOADS_TOKEN=this_is_my_secret_access_token

What could I be doing wrong?


Solution

  • After reading gradle docs, I found out that the thing which the mapbox installation guide is doing wrong is setting the token variable in gradle.properties. You need to specify it as a system property (see https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_system_properties), so essentially all you have to do is to change

    MAPBOX_DOWNLOADS_TOKEN=this_is_my_secret_access_token
    

    in

    systemProp.MAPBOX_DOWNLOADS_TOKEN=this_is_my_secret_access_token
    

    Now move to the settings.gradle file and change the line

    password = System.getenv("MAPBOX_DOWNLOADS_TOKEN")
    

    with

    password = System.properties["MAPBOX_DOWNLOADS_TOKEN"]