1-. Use case.
I’m using the google_maps_flutter package, in a Flutter and Dart project. Version is google_maps_flutter: ^2.4.0
When I call to the GoogleMaps widget in Flutter:
the following SecurityException is thrown:
2-. Implemented API key
I followed the guide described in the google_maps_flutter package (https://pub.dev/packages/google_maps_flutter), the guide for installing Google Maps SDK in Flutter (https://developers.google.com/maps/flutter-package/config?authuser=0#step_1_install_the_required_software), and the ‘Maps SDK for Android Quickstart’ documentation (https://developers.google.com/maps/documentation/android-sdk/start) ->
i) GMP API key generated
The api key related to Maps API is the fourth one ->
… with the following api key ->
… and restrictions ->
ii) the android manifest.xml is ->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:label="appname"
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">
<!-- 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"
/>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<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" />
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}"/>
</application>
</manifest>
iii) the settings.graddle is ->
iv) the secrets.properties is ->
V) the local.defaults.properties is ->
vi) the android/build.gradle is ->
vii) the app/build.gradle is ->
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
id "com.google.gms.google-services"
id "com.google.android.libraries.mapsplatform.secrets-gradle-plugin"
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}
android {
namespace "com.byroomnumber.brnapp"
compileSdk 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 {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.appName"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 21
targetSdkVersion 34
multiDexEnabled true
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
signingConfigs {
release {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
storePassword keystoreProperties['storePassword']
}
}
buildTypes {
release {
signingConfig signingConfigs.release
}
}
buildFeatures{
buildConfig true
}
}
flutter {
source '../..'
}
dependencies {
implementation platform("com.google.firebase:firebase-bom:30.7.1")
implementation("androidx.multidex:multidex:2.0.1")
implementation "com.google.firebase:firebase-analytics"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.21"
implementation 'com.google.android.gms:play-services-maps:18.2.0'
}
secrets {
// Optionally specify a different file name containing your secrets.
// The plugin defaults to "local.properties"
propertiesFileName = "secrets.properties"
// A properties file containing default secret values. This file can be
// checked in version control.
defaultPropertiesFileName = "local.defaults.properties"
// Configure which keys should be ignored by the plugin by providing regular expressions.
// "sdk.dir" is ignored by default.
ignoreList.add("keyToIgnore") // Ignore the key "keyToIgnore"
ignoreList.add("sdk.*") // Ignore all keys matching the regexp "sdk.*"
}
- Expected results
The map is correctly rendered in a device connected via usb, although the exception is thrown.
If an apk is generated and installed on the device, the map is not rendered, and the exception is thrown.
- Do you try using other devices?
Yes. I verified this process in a SMA426B and and a MiT11
3
Answers
In order to give a more detailed information to the problem, I enclose the previous and forward information given in terminal, in addition to the error itself:
I suppose it is not a problem of Google Play Services, since it is correctly read...
You also need to add INTERNET permission, add this line to androidManifes.xml
In you project level gradle change version of google play services to 4.3.15 in Plugins section.