2

I have set up a new project on Android Studio and I'm trying to use v10 of Mapbox SDK. However, my Gradle build fails with the message:

Failed to resolve: com.mapbox.maps:android:10.0.0.

Can someone help me out? How do I correctly import the v10 implementation of Mapbox? Thanks in advance!

Here's my root-level build.gradle:

buildscript {
    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 = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: ""
            }
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"
        classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.0"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    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 = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: ""
            }
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And here's my project-level build.gradle:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "--------" //This is my application's ID, I've removed it for security purposes
        minSdkVersion 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        resValue "string", "mapTilerKey", "udm4bYUze4mksls7yU8o"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation 'com.mapbox.maps:android:10.0.0'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    implementation 'org.maplibre.gl:android-sdk:9.2.1'
    implementation 'com.google.android.gms:play-services-location:18.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Sweta Jain
  • 3,248
  • 6
  • 30
  • 50
Istorian
  • 43
  • 8
  • If you've followed the steps [indicated in their documentation](https://docs.mapbox.com/android/maps/guides/install/) (and you didn't make a mistake/typo) the alternative is you have a connectivity issue. – Martin Marconcini Nov 08 '21 at 10:10
  • I have quadruple-checked all files as well as my connection for any connectivity issues. I do not understand what's going wrong. I even started from scratch, followed the installation instructions in the documentation exactly, and I still get this issue. What could be happening? – Istorian Nov 08 '21 at 15:34
  • 1
    Glad to hear you found the solution! :) – Martin Marconcini Nov 09 '21 at 08:24

2 Answers2

1

I figured it out in the end. In settings.gradle, I had changed this line

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

to

repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)

so the project-level build.gradle file was not being taken into account, the repos being considered came from settings.gradle. All it took was to change the settings.gradle to this:

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 = project.properties['MAPBOX_DOWNLOADS_TOKEN'] ?: ""
        }
    }
}
Istorian
  • 43
  • 8
  • Would you please provide full working example (some repo), i did not come out with solution for this yet. Even though i was basically doing the same thing for a really long time – RickertBrandsen Nov 09 '21 at 11:22
1

After a a few hours of research I came to find out how to properly set up Mapbox 10.0.2

IMO, this first bullet should solve the issue, otherwise keep going.

First, in settings.gradle -> as stated previously set mode to PREFERS.SETTINGS .

dependencyResolutionManagement {
  repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
  repositories {
    google()
    mavenCentral()
    jcenter() // Warning: this repository is going to shut down soon
    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 = MAPBOX_DOWNLOADS_TOKEN
        }
      }
    }
  }

Second, add the dependency as per usual to module-level build.gradle (not project level)

    implementation 'com.mapbox.maps:android:10.2.0'

Third, this is what my project-level build.gradle looks like.

 buildscript {
ext {
    compose_version = '1.2.0-alpha02'
}
repositories {
    google()
    mavenCentral()

}
dependencies {
    classpath 'com.android.tools.build:gradle:7.1.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

Last, in your gradle.property make sure you have

(GOOD)   MAPBOX_DOWNLOADS_TOKEN = SECRET_ACCESS_CODE
(DO NOT) MAPBOX_DOWNLOADS_TOKEN = "SECRET_ACCESS_CODE"
Koch
  • 555
  • 4
  • 15