skip to Main Content

I am trying to implement Mapbox in my Flutter project because I need offline maps. I am using the mapbox_gl package. I have followed all the steps provided in the documentation, from creating tokens to the configurations in Gradle. However, I keep encountering the following error:

(Include the error message here)

I am attaching my configuration files for reference:

1. android/app/src/main/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.google_maps_api">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:label="google_maps_api"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:taskAffinity=""
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <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>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />

    </application>
    <!-- Required to query activities that can process text, see:
         https://developer.android.com/training/package-visibility and
         https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

2. android/build.gradle

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = "../build"
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

3. android/gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-8.3-all.zip

4. android/app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "com.example.google_maps_api"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.google_maps_api"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = 21
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig = signingConfigs.debug
        }
    }
}




flutter {
    source = "../.."
}


dependencies {
    implementation 'com.mapbox.maps:android:11.6.1'
}

5. android/gradle.properties

org.gradle.jvmargs=-Xmx4G -XX:MaxMetaspaceSize=2G -XX:+HeapDumpOnOutOfMemoryError
android.useAndroidX=true
android.enableJetifier=true
MAPBOX_DOWNLOADS_TOKEN=skmyprivetetoken

6. android/settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        // Mapbox Maven repository
        maven {
            url = uri("https://api.mapbox.com/downloads/v2/releases/maven")
            // Do not change the username below. It should always be "mapbox" (not your username).
            credentials.username = "mapbox"
            // Use the secret token stored in gradle.properties as the password
            credentials.password = providers.gradleProperty("MAPBOX_DOWNLOADS_TOKEN").get()
            authentication { basic(BasicAuthentication) }
        }

    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.1.0" apply false
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

include ":app"

Could someone help me identify the root cause of this issue? I am using Flutter 3.24 and mapbox_gl: ^0.16.0 with the default Android folder created by Flutter, which has undergone some recent changes. Could this be related to using an incorrect Gradle, Android, or Java version? Any insights would be appreciated!

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Thanks, @AjitSharma. What I did was modify the build.gradle generated by mapbox_gl: ^0.16.0. I went to the following path: C:UsersmyuserAppDataLocalPubCachehostedpub.devmapbox_gl-0.16.0androidbuild.gradle.

    The content was as follows:

    group 'com.mapbox.mapboxgl'
    version '1.0-SNAPSHOT'
    
    buildscript {
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:4.2.0'
        }
    }
    
    rootProject.allprojects {
        def token = System.getenv('SDK_REGISTRY_TOKEN') ?: project.properties['MAPBOX_DOWNLOADS_TOKEN']
        if (token == null || token.empty) {
            throw new Exception("SDK Registry token is null. See README.md for more information.")
        }
        repositories {
            google()
            mavenCentral()
            maven {
                url 'https://api.mapbox.com/downloads/v2/releases/maven'
                authentication {
                    basic(BasicAuthentication)
                }
                credentials {
                    username = "mapbox"
                    password = token
                }
            }
        }
    }
    
    apply plugin: 'com.android.library'
    
    android {
        namespace 'com.mapbox.mapboxgl'
        compileSdkVersion 31
        ndkVersion "20.1.5948944"
    
        defaultConfig {
            minSdkVersion 20
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
        lintOptions {
            disable 'InvalidPackage'
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        dependencies {
            implementation "com.mapbox.mapboxsdk:mapbox-android-sdk:9.6.2"
            implementation "com.mapbox.mapboxsdk:mapbox-android-plugin-annotation-v9:0.9.0"
            implementation "com.mapbox.mapboxsdk:mapbox-android-plugin-localization-v9:0.12.0"
            implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-offline-v9:0.7.0'
            implementation 'com.squareup.okhttp3:okhttp:4.9.0'
        }
        compileOptions {
            sourceCompatibility 1.8
            targetCompatibility 1.8
        }
    }
    

    I added the line namespace 'com.mapbox.mapboxgl' inside the android section.

    I also made another change in my android/app/build.gradle dependencies section:

    dependencies {
        implementation('com.mapbox.mapboxsdk:mapbox-android-sdk:9.6.2') {
            exclude group: 'com.mapbox.common', module: 'common'
        }
    }
    

    This worked correctly. However, directly modifying the library (mapbox_gl) might work temporarily, but it's not optimal. The issue is that any updates or reinstalls of the library will overwrite your changes.

    When generating the APK, it might work if no further modifications are made, but there’s a risk of future errors. It's better to find a solution that doesn’t involve changing the library itself, such as using Gradle configurations or Mapbox's own alternatives.


  2. Seems you didn’t give token in AndroidManifest inside application and verify android/app/build.gradle dependencies as well.
    Don’t forgot to give token in main.dart as well for initialise.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search