Flutter集成Google Location API遇SecurityException问题求助
解决Flutter集成Google Location API时的SecurityException问题
错误日志
D/MapsInitializer(15510): preferredRenderer: null D/zzcc (15510): preferredRenderer: null I/zzcc (15510): Making Creator dynamically W/ziparchive(15510): Unable to open '/data/user_de/0/com.google.android.gms/app_chimera/m/000001ad/DynamiteLoader.uncompressed.dm': No such file or directory W/ziparchive(15510): Unable to open '/data/user_de/0/com.google.android.gms/app_chimera/m/000001ad/DynamiteLoader.uncompressed.dm': No such file or directory I/DynamiteModule(15510): Considering local module com.google.android.gms.maps_core_dynamite:0 and remote module com.google.android.gms.maps_core_dynamite:241610202 I/DynamiteModule(15510): Selected remote version of com.google.android.gms.maps_core_dynamite, version >= 241610202 V/DynamiteModule(15510): Dynamite loader version >= 2, using loadModule2NoCrashUtils W/System (15510): ClassLoader referenced unknown path: D/nativeloader(15510): Configuring clns-6 for other apk . target_sdk_version=34, uses_libraries=, library_path=/data/app/~~aDJz21s4KF00HrE2fHyMrw==/com.google.android.gms-ok87zpsyyDDP-MOdQOTVYA==/lib/arm64:/data/app/~~aDJz21s4KF00HrE2fHyMrw==/com.google.android.gms-ok87zpsyyDDP-MOdQOTVYA==/base.apk!/lib/arm64-v8a, permitted_path=/data:/mnt/expand:/data/user/0/com.google.android.gms W/obile.asset.tns(15510): Accessing hidden method Ldalvik/system/VMStack;->getStackClass2()Ljava/lang/Class; (unsupported, reflection, allowed) D/nativeloader(15510): Configuring clns-7 for other apk . target_sdk_version=33, uses_libraries=ALL, library_path=/data/user_de/0/com.google.android.gms/app_chimera/m/00000197/dl-MapsCoreDynamite.integ_241610202100400.apk!/lib/arm64-v8a, permitted_path=/data:/mnt/expand I/native (15510): I0000 00:00:1719357308.066152 15510 jni_init.cc:8] Initializing JNI... Caused by: java.lang.SecurityException: GoogleCertificatesRslt: not allowed: pkg=com.apptricity.mobile.asset.tns, sha256=[7090f01d4f12a801e1ecf56b2a8ca456decdf75a240426b2aa316f57bb08ba20], atk=false, ver=242334038.true (go/gsrlt)
已做配置
已在src/main/AndroidManifest.xml的application标签中添加API密钥配置:
<meta-data android:name="com.google.android.geo.API_KEY" android:value="Key"/>
实现代码
class AssetLocationScreen extends StatefulWidget { final String displayLabel; final double? longitude; final double? latitude; AssetLocationScreen( {required this.displayLabel, this.longitude, this.latitude}); @override State<AssetLocationScreen> createState() => _AssetLocationScreenState(); } class _AssetLocationScreenState extends State<AssetLocationScreen> { late GoogleMapController mapController; bool _locationPermissionGranted = false; void _onMapCreated(GoogleMapController controller) async { mapController = controller; var status = await Permission.location.status; if (!status.isGranted) { var result = await Permission.location.request(); if (result.isGranted) { setState(() { _locationPermissionGranted = true; }); } } else { setState(() { _locationPermissionGranted = true; }); } } @override Widget build(BuildContext context) { LatLng position = LatLng(widget.latitude ?? 0.0, widget.longitude ?? 0.0); return Scaffold( body: Column( children: [ Container( width: double.infinity, padding: EdgeInsets.only( top: 60.0, left: 60.0, right: 60.0, bottom: 20.0), decoration: BoxDecoration( color: Color.fromRGBO(2, 153, 204, 1), borderRadius: BorderRadius.only( bottomLeft: Radius.circular(20.0), bottomRight: Radius.circular(20.0), ), ), child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text( 'Inquiry', style: TextStyle( fontFamily: 'Poppins', fontSize: 32, fontWeight: FontWeight.w500, color: Colors.white, ), textAlign: TextAlign.center, ), SizedBox(height: 4), Text( widget.displayLabel, style: TextStyle( fontFamily: 'Poppins', fontSize: 15, fontWeight: FontWeight.w500, color: Colors.white, ), textAlign: TextAlign.center, ), SizedBox(height: 8), Text( 'Location', style: TextStyle( fontFamily: 'Poppins', fontSize: 20, fontWeight: FontWeight.w500, color: Colors.white, ), textAlign: TextAlign.center, ), ], ), ), Expanded( child: Stack( children: [ GoogleMap( mapType: MapType.normal, initialCameraPosition: CameraPosition( target: position, zoom: 14.0, ), onMapCreated: _onMapCreated, myLocationEnabled: _locationPermissionGranted, myLocationButtonEnabled: _locationPermissionGranted, /* markers: Set<Marker>.of([ Marker( markerId: MarkerId('assetLocation'), position: position, ), ]),*/ ), Container( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [ Color.fromRGBO(70, 206, 252, 1), Color.fromRGBO(0, 54, 120, 1), ], stops: [0.2486, 0.833], ), ), ), ], ), ), ], ), ); } }
解决方案
这个SecurityException是Google API密钥的权限验证失败导致的,按以下步骤排查修复:
核对API密钥的SHA-256指纹
错误日志已给出当前应用的SHA-256指纹:7090f01d4f12a801e1ecf56b2a8ca456decdf75a240426b2aa316f57bb08ba20,确保Google Cloud控制台中,你的API密钥已绑定该指纹和应用包名com.apptricity.mobile.asset.tns。启用所需API服务
在Google Cloud控制台中,检查Maps SDK for Android及相关位置服务(如Geocoding API)是否已启用,未启用则开启对应服务。验证API密钥限制设置
确保API密钥的限制配置正确:- 选择
Android apps限制类型 - 准确填写应用包名和对应的SHA-256指纹
- 避免设置过于严格的API范围限制,导致所需服务被拦截
- 选择
清理并重建项目
执行以下操作确保配置生效:- 清理Flutter项目:
flutter clean - 删除Android构建缓存:删除
android/build和android/app/build目录 - 重新构建运行:
flutter run
- 清理Flutter项目:
补充位置权限配置
在AndroidManifest.xml中添加必要的位置权限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />针对Android 12及以上版本,额外添加:
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
内容的提问来源于stack exchange,提问作者Anamika Singh
相关产品推荐
相关产品推荐

