集成Mapbox新特性时Gradle无法解析新增依赖问题求助
解决Mapbox 10.0.0-rc.8依赖解析失败的问题
我帮你排查了下,你遇到的问题主要出在两个地方:Mapbox仓库的配置范围没覆盖到App模块,以及同时引入了新旧版本的Mapbox依赖导致冲突。下面是一步步的解决方法:
1. 让所有模块都能访问Mapbox的Maven仓库
你现在只在buildscript块里配置了Mapbox的仓库,但这个块里的仓库仅用于加载Gradle插件这类构建脚本依赖,App模块的依赖需要在全局仓库列表里也能访问到Mapbox的私有仓库。
修改Project级build.gradle
在现有代码基础上,添加allprojects块,把Mapbox仓库加入全局仓库列表:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() mavenCentral() maven { url 'https://api.mapbox.com/downloads/v2/releases/maven' authentication { basic(BasicAuthentication) } credentials { username = 'mapbox' 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.20" } } // 新增这个块,确保App模块能找到Mapbox的仓库 allprojects { repositories { google() mavenCentral() maven { url 'https://api.mapbox.com/downloads/v2/releases/maven' authentication { basic(BasicAuthentication) } credentials { username = 'mapbox' password = project.properties['MAPBOX_DOWNLOADS_TOKEN'] } } } } task clean(type: Delete) { delete rootProject.buildDir }
如果你用的是Gradle 7.0+的新结构(settings.gradle里有dependencyResolutionManagement),更推荐把仓库配置移到settings.gradle里,这是现在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 { username = 'mapbox' password = providers.gradleProperty("MAPBOX_DOWNLOADS_TOKEN").get() } } } } rootProject.name = "3dmapbox" include ':app'
2. 移除旧版Mapbox依赖,避免版本冲突
Mapbox 10.x是完全重构的版本,和9.x的SDK、插件完全不兼容。你同时加了9.x和10.x的依赖,不仅会导致冲突,还可能干扰依赖解析。
修改App级build.gradle的dependencies块,删掉所有9.x的Mapbox相关依赖:
dependencies { implementation 'androidx.core:core-ktx:1.6.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // 保留非Mapbox的第三方依赖 implementation "com.gorisse.thomas.sceneform:sceneform:1.20.1" implementation("com.google.android.gms:play-services-location:18.0.0") // 只保留10.x的Mapbox依赖 implementation 'com.mapbox.maps:android:10.0.0-rc.8' implementation 'com.mapbox.plugin:maps-annotation:10.0.0-rc.8' implementation 'com.mapbox.plugin:maps-locationcomponent:10.0.0-rc.8' implementation 'com.mapbox.plugin:maps-gestures:10.0.0-rc.8' implementation 'com.mapbox.plugin:maps-compass:10.0.0-rc.8' implementation 'com.mapbox.plugin:maps-animation:10.0.0-rc.8' implementation 'com.mapbox.plugin:maps-scalebar:10.0.0-rc.8' implementation 'com.mapbox.plugin:maps-logo:10.0.0-rc.8' implementation 'com.mapbox.plugin:maps-attribution:10.0.0-rc.8' }
3. 同步项目并清理缓存
改完之后:
- 点击Android Studio右上角的Sync Project with Gradle Files按钮同步项目
- 执行
File -> Invalidate Caches...,选择Invalidate and Restart清理缓存并重启IDE
这样应该就能成功解析10.0.0-rc.8的Mapbox依赖了。
内容的提问来源于stack exchange,提问作者Kashif Mehmood
相关产品推荐
相关产品推荐

