Skip to main content

Setup

Official docs: https://developers.google.com/maps/flutter-package/config

Step 1: Set the platform version

Set the minimum SDK version for Android.

android/app/build.gradle.kts
  android {

defaultConfig {
applicationId "com.example.google_maps_in_flutter"
minSdkVersion 21 // Set to 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}

}

Step 2: Add your API key to the project

note

To avoid disclosing the API_KEY when publicly releasing the source code, we will not push the key to a remote server.

  1. Create file appconfig.properties in folder android/, with content

    android/appconfig.properties
    MAP_API_KEY=[Put your Map API key here]
  2. Add line to android/.gitignore

    appconfig.properties
  3. Run the command below to clear cache git

    git rm -r --cached .
  4. Open file android/app/build.gradle add the following highlighted lines

    build.gradle

    ...

    def appConfigProperties = new Properties()
    def appConfigPropertiesFile = rootProject.file('appconfig.properties')
    if (appConfigPropertiesFile.exists()) {
    appConfigPropertiesFile.withReader('UTF-8') { reader ->
    appConfigProperties.load(reader)
    }
    }

    def mapApiKey =appConfigProperties.getProperty('MAP_API_KEY')

    android {
    ...

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

    manifestPlaceholders += [MAP_API_KEY: mapApiKey]
    }

    ...
    }
  5. In your AndroidManifest.xml file, go to com.google.android.geo.API_KEY and update the android:value attribute. If the <meta-data> tag does not exist, create it as a child of the <application> tag.

    <meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="${MAP_API_KEY}" />