Flutter集成Google Maps API的Release版本运行异常求助
问题描述
我的Flutter应用在模拟器上运行正常,但生成Release构建包并安装到手机后,会卡在以下代码处:
geoloc.Position position = await geoloc.Geolocator.getCurrentPosition(desiredAccuracy: geoloc.LocationAccuracy.high); initialPosition = LatLng(position.latitude, position.longitude);
其中geoloc是Geolocator的别名:
import 'package:geolocator.dart' as geoloc;
initialPosition是LatLng类型变量:
late LatLng initialPosition;
已尝试添加位置权限申请,但问题未解决。由于忘记Release密钥密码且不知如何重置,我使用debug密钥命令获取SHA1:
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
而非官方指定的Release密钥命令:
keytool -list -v -keystore your_keystore_name -alias your_alias_name
不确定修改SHA1是否能解决问题,参考帖子得知需在src/release/res/values/google_maps_api.xml中配置Release密钥,但我既无该目录也无Release密钥。
解决方案
1. 排查Release模式下的权限问题
虽说你加了权限,但Release模式下权限配置容易有遗漏:
- 打开
AndroidManifest.xml,确认加全了必要的位置权限:<!-- 后台定位权限(若应用需要后台获取位置) --> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <!-- 前台精确定位权限 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 粗略定位权限(备用) --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> - Android 10及以上系统,
ACCESS_BACKGROUND_LOCATION需要用户手动授权,光靠代码申请没用,得确保用户安装应用后授予了该权限。 - 建议在获取位置前先检查权限状态,避免程序卡死:
var status = await geoloc.Geolocator.checkPermission(); if (status == geoloc.LocationPermission.denied) { status = await geoloc.Geolocator.requestPermission(); if (status == geoloc.LocationPermission.denied) { // 这里可以加权限拒绝后的处理逻辑,比如提示用户或跳转设置 return; } } // 再执行获取位置的代码
2. 重置Release密钥
既然忘了Release密钥密码,直接重新生成一个新的即可(注意:如果应用已经上线,重置密钥会导致旧版本无法更新,需谨慎操作):
- 执行以下命令生成新的密钥库:
keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000 - 按照提示输入密码和相关信息,务必保存好密钥库文件和密码。
- 在Flutter项目的
android/app目录下创建key.properties文件,添加以下配置:storePassword=你的密钥库密码 keyPassword=你的密钥别名密码 keyAlias=my-key-alias storeFile=../my-release-key.keystore - 修改
android/app/build.gradle,添加签名配置:def keystoreProperties = new Properties() def keystorePropertiesFile = rootProject.file('key.properties') if (keystorePropertiesFile.exists()) { keystoreProperties.load(new FileInputStream(keystorePropertiesFile)) } android { ... signingConfigs { release { keyAlias keystoreProperties['keyAlias'] keyPassword keystoreProperties['keyPassword'] storeFile file(keystoreProperties['storeFile']) storePassword keystoreProperties['storePassword'] } } buildTypes { release { ... signingConfig signingConfigs.release } } }
3. 配置Google Maps的Release SHA1(若使用了Google Maps)
如果应用用到了Google Maps,确实需要把Release密钥的SHA1配置到Google Cloud控制台:
- 用新生成的Release密钥获取SHA1:
keytool -list -v -keystore my-release-key.keystore -alias my-key-alias - 登录Google Cloud控制台,找到对应的Maps API项目,添加这个SHA1和应用包名。
- 若没有
src/release/res/values/google_maps_api.xml,可以手动创建目录和文件,添加Release版的API密钥:<resources> <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">你的Release API密钥</string> </resources>
4. 调试Release模式的卡顿原因
可以给Release包加日志,或者用Android Studio Profiler排查问题:
- 在获取位置的代码前后添加日志:
print("开始获取位置"); try { geoloc.Position position = await geoloc.Geolocator.getCurrentPosition(desiredAccuracy: geoloc.LocationAccuracy.high); print("获取位置成功:${position.latitude}, ${position.longitude}"); initialPosition = LatLng(position.latitude, position.longitude); } catch (e) { print("获取位置失败:$e"); } - 把Release包安装到手机后,通过
adb logcat命令查看日志,就能定位具体错误(比如权限未授予、位置服务未开启、Geolocator的Release模式配置问题等)。
内容的提问来源于stack exchange,提问作者Innarq
相关产品推荐
相关产品推荐

