Hi I have a google key for maps. When I have it unrestricted it works as intended but when I add in android restriction with my pagackage and debug SHA1 key it rejects it. I have no idea what is wrong. It does work PERFECTLY fine unrestricted.
The only solution I have found is by forcing the SHA keys in to the header of the HTTPS requests. via:
main.dart
final String buildCert = await platform.invokeMethod('getBuildCert');
kotlin MainActivity
private fun getBuildCert(): String? {
try {
val packageInfo: PackageInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNING_CERTIFICATES)
} else {
packageManager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES)
}
val signatures = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageInfo.signingInfo.apkContentsSigners
} else {
packageInfo.signatures
}
for (signature in signatures) {
val digest = MessageDigest.getInstance("SHA-1")
digest.update(signature.toByteArray())
val hashText = digest.digest()
return bytesToHex(hashText)
}
} catch (e: PackageManager.NameNotFoundException) {
Log.e("SHA1", "Package not found", e)
} catch (e: NoSuchAlgorithmException) {
Log.e("SHA1", "SHA-1 algorithm not found", e)
}
return null
}
- I have create a new key from scratch
- Tried on physical and emulator on a few different PCs and physical devices
- I tried to change the package name
- I have verified the SHA and package name multiple times and looked in my proj to see if there were reminent of old package names.
Google Console
KeyTool
This is my androidappsrcmainAndroidManifest.xml
and
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mazaar.frontend">
<application
android:label="Mazaar"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<!-- Google Maps API Key configuration -->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${googleMapsApiKey}" />
<!-- MainActivity configuration -->
<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"
android:enableOnBackInvokedCallback="true">
<!-- Theme for Flutter UI initialization -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" />
<!-- Main launcher intent filter -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Intent filters for handling HTTP and HTTPS schemes -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent-filter>
</activity>
<!-- Required for Flutter plugins -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Queries for internet browsers (Android 11 and above) -->
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>
</manifest>
plugins {
id "com.android.application"
// START: FlutterFire Configuration
id 'com.google.gms.google-services'
// END: FlutterFire Configuration
id "kotlin-android"
id "dev.flutter.flutter-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'
}
android {
namespace "com.mazaar.frontend"
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.mazaar.frontend"
// 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 flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def googleMapsApiKey = properties.getProperty('googleMapsApiKey')
println "Google Maps API Key: ${googleMapsApiKey}"
manifestPlaceholders = [
googleMapsApiKey: googleMapsApiKey,
applicationName: "android.app.Application"
]
}
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 'androidx.multidex:multidex:2.0.1'
}
Local.properties
sdk.dir=C:\Users\jarvis\AppData\Local\Android\sdk
flutter.sdk=C:\Users\jarvis\Documents\Jump\flutter_sdk\flutter
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1
flutter.minSdkVersion=21
googleMapsApiKey=AIzaSyB...
1