Flutter集成Mapbox Map SDK遇依赖冲突及构建停滞问题求助
Flutter集成Mapbox SDK遇到依赖冲突与构建停滞问题
我是Flutter新手,尝试集成Mapbox SDK,按照Mapbox官方Android文档完成配置后地图无法正常工作。运行项目时触发:app:checkDebugDuplicateClasses任务失败,提示com.mapbox.android.core相关类在jetified-common-23.4.0-runtime和jetified-mapbox-android-core-5.0.0-runtime模块中重复。我使用了mapbox_gl: ^0.16.0包,尝试依赖排除方案后冲突错误消失,但Gradle的assembleDebug任务陷入循环停滞,无法完成。相关配置文件及代码如下:
错误日志
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:checkDebugDuplicateClasses'. > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckDuplicatesRunnable > Duplicate class com.mapbox.android.core.location.LocationEngine found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.location.LocationEngineCallback found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.location.LocationEngineProvider found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.location.LocationEngineRequest found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.location.LocationEngineRequest$1 found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.location.LocationEngineRequest$Builder found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.location.LocationEngineResult found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.permissions.PermissionsListener found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.permissions.PermissionsManager found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Duplicate class com.mapbox.android.core.permissions.PermissionsManager$AccuracyAuthorization found in modules jetified-common-23.4.0-runtime (com.mapbox.common:common:23.4.0) and jetified-mapbox-android-core-5.0.0-runtime (com.mapbox.mapboxsdk:mapbox-android-core:5.0.0) Go to the documentation to learn how to <a href="d.android.com/r/tools/classpath-sync-errors">Fix dependency resolution errors</a>
gradle.properties文件
org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true MAPBOX_DOWNLOADS_TOKEN=已确认有效
styles.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off --> <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar"> <!-- Show a splash screen on the activity. Automatically removed when the Flutter engine draws its first frame --> <item name="android:windowBackground">@drawable/launch_background</item> </style> <!-- Theme applied to the Android Window as soon as the process has started. This theme determines the color of the Android Window while your Flutter UI initializes, as well as behind your Flutter UI while its running. This Theme is only used starting with V2 of Flutter's Android embedding. --> <style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar"> <item name="android:windowBackground">?android:colorBackground</item> </style> <string name="mapbox_access_token">已确认有效</string> </resources>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mapbox_try2"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:label="mapbox_try2" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <meta-data android:name="flutterEmbedding" android:value="2" /> </application> </manifest>
android/build.gradle
buildscript { ext.kotlin_version = '1.7.10' repositories { google() mavenCentral() maven { url 'https://api.mapbox.com/downloads/v2/releases/maven' authentication { basic(BasicAuthentication) } credentials { username = "mapbox" password = "已确认有效" } } } dependencies { classpath 'com.android.tools.build:gradle:7.2.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() } } rootProject.buildDir = '../build' subprojects { project.buildDir = "${rootProject.buildDir}/${project.name}" } subprojects { project.evaluationDependsOn(':app') } task clean(type: Delete) { delete rootProject.buildDir }
app/build.gradle(原配置)
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } } def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found.") } def flutterVersionCode = localProperties.getProperty('flutter.versionCode') ?: '1' def flutterVersionName = localProperties.getProperty('flutter.versionName') ?: '1.0' apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { compileSdkVersion flutter.compileSdkVersion ndkVersion flutter.ndkVersion compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = '1.8' } sourceSets { main.java.srcDirs += 'src/main/kotlin' } defaultConfig { applicationId "com.example.mapbox_try2" minSdkVersion 21 targetSdkVersion flutter.targetSdkVersion versionCode flutterVersionCode.toInteger() versionName flutterVersionName } buildTypes { release { signingConfig signingConfigs.debug } } } flutter { source '../..' } dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.mapbox.maps:android:10.12.1' }
尝试修改后的app/build.gradle依赖配置
dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.mapbox.maps:android:10.12.1' implementation ('com.mapbox.maps:android:10.12.1'){ configurations { all { exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-android-core' exclude group: 'com.mapbox.common', module: 'common' exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-sdk-services' exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-sdk-turf' exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-sdk-geojson' exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-sdk-constants' exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-sdk-core' } } }
Flutter代码
import 'package:flutter/material.dart'; import 'package:mapbox_gl/mapbox_gl.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData(primarySwatch: Colors.blue), home: const MyHomePage(), ); } } class MyHomePage extends StatelessWidget { const MyHomePage({super.key}); @override Widget build(BuildContext context) { return Container( child: MapboxMap( accessToken: "已确认有效", onMapCreated: (MapboxMapController controller) {}, initialCameraPosition: CameraPosition( target: LatLng(37.7749, -122.4194), zoom: 12, ), ), ); } }
内容的提问来源于stack exchange,提问作者Nikhil K
相关产品推荐
相关产品推荐

