解决方法:解析失败:com.mapbox.mapboxsdk:mapbox-android-sdk:9.5.0

How to solve : Failed to resolve: com.mapbox.mapboxsdk:mapbox-android-sdk:9.5.0

我在 Android studio 中尝试使用 mapbox 时遇到这个问题
解析失败:com.mapbox.mapboxsdk:mapbox-android-sdk:9.5.0

有什么问题?

我的 build.gradle 依赖关系


dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.5.0'
}

我的build.gradle项目

buildscript {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"
    }
}

allprojects {
    repositories {
  google()
        jcenter()
        maven { url 'https://mapbox.bintray.com/mapbox' }

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

尝试一个实际上 exists 的版本如何?

implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:8.6.7'

版本 9.5.0(和 9.6.0)也存在(参见此处的发行说明:https://github.com/mapbox/mapbox-gl-native-android/blob/main/CHANGELOG.md)。只是随着 Mapbox Maps SDK > v9.4.0.

访问 Maven 存储库的方式发生了变化

我不鼓励您使用像 mapbox-android-sdk:8.6.7 这样的过时版本,但请使用 com.mapbox.mapboxsdk:mapbox-android-sdk:9.6.0

此处记录了访问 mave 存储库的新方法: https://docs.mapbox.com/android/maps/overview/#configure-credentials

您现在需要创建一个秘密访问令牌并使用它来访问库所在的 Maven 存储库。您的模块级别 build.gradle 应包含以下内容:

allprojects {
  repositories {
    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'] ?: ""
      }
    }
  }
}

只是解释得不好。你必须制作一个私人令牌。 public 令牌无效。当您创建令牌时,有一个带有“读取:下载”的字段,只需标记它并生成您的令牌。此令牌应该有效。

只需使用此安全版本即可在您的 gradle.

上使用
implementation ('com.mapbox.mapboxsdk:mapbox-android-sdk:9.2.0'){
        exclude group: 'group_name', module: 'module_name'
    }

让我的项目最终运行的唯一方法是在此处附加的项目中寻找一些指针: https://github.com/mapbox/mapbox-maps-android/issues/614#issue-988592394 它使用 com.mapbox.maps:android:10.0.0-rc.3 并在我的机器上实际工作。

为了让我自己的项目工作,我不得不从这里更改 settings.gradle:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}
rootProject.name = "Mapbox Map"
include ':app'

对此:

rootProject.name = "Mapbox Map"
include ':app'

通过生成私钥找到解决方案,如先前答案中所指出的,并在 Maven Central 上找到实际版本,在撰写本文时,Maven 中央存储库中既没有 9.7 也没有 10,使用的是此处找到的版本 9.2

https://mvnrepository.com/artifact/com.mapbox.mapboxsdk/mapbox-android-sdk

编辑:能够通过更改 settings.gradle 条目

使用 9.7.0 mapbox

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)

和 build.gradle 将 mavenCentral() 和 google() 添加到所有项目存储库

allprojects {
    repositories {
        google()
        mavenCentral()
        
        allprojects {
            repositories {
                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'] ?: ""
                    }
                }
            }
        }
    }
}

Mapbox 为您提供的默认令牌是我认为您正在使用的,您需要创建一个新的私有令牌并检查此权限 DOWNLOAD:READ 然后它应该可以工作。

我创建了一个新令牌并将其命名为 first 在我的例子中,您可以随意命名它。

你应该删除这一行:maven{ url 'https://mapbox.bintray.com/mapbox' }

 repositories {
    google()
    jcenter()
    mavenCentral()
    /**
    if you using => maven { url 'https://mapbox.bintray.com/mapbox' }
     so remove this line because mapbox remove repository from https://mapbox.bintray.com/mapbox
    * */
    maven { url "https://oss.sonatype.org/content/groups/public/" }
    maven { url 'https://jitpack.io' }
    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'] ?: ""
        }
    }
}