1-.使用案例。
我在 Flutter 和 Dart 项目中使用 google_maps_flutter 包。版本是 google_maps_flutter: ^2.4.0
当我在 Flutter 中调用 GoogleMaps 小部件时:
抛出以下 SecurityException:
2-.已实施 API 密钥
我遵循了 google_maps_flutter 包 (https://pub.dev/packages/google_maps_flutter) 中描述的指南,即在 Flutter 中安装 Google Maps SDK 的指南 (https://developers.google.com/maps/flutter) -package/config?authuser=0#step_1_install_the_required_software),以及“Maps SDK for Android Quickstart”文档 (https://developers.google.com/maps/documentation/android-sdk/start) ->
i) 生成 GMP API 密钥 与Maps API相关的api key是第四个 ->
…使用以下 api 密钥 ->
...和限制 ->
ii) android manifest.xml 是 ->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!--<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />-->
<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)settings.graddle 是 ->
iv) Secrets.properties 是 ->
V) local.defaults.properties 是 ->
vi) android/build.gradle 是 ->
vii) app/build.gradle 是 ->
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.*"
}
尽管抛出异常,但地图在通过 USB 连接的设备中正确呈现。 如果生成apk并安装到设备上,则不会渲染地图,并抛出异常。
是的。我在 SMA426B 和 MiT11 中验证了这个过程
您还需要添加INTERNET权限,将此行添加到androidManifes.xml
<uses-permission android:name="android.permission.INTERNET" />