集成GoogleMap正确的签名打包姿势

本文介绍了一种常见的情况,即在集成Google Maps API后,地图在debug模式下正常显示,但在release版本中无法加载。文章详细解释了解决此问题的方法,包括在build.gradle文件中设置manifestPlaceholders字段、正确配置API密钥以及遵循官方指南生成独立的debug和release版本密钥。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在集成了Google Map之后,在debug模式下测试一切正常,地图正常显示,都是签名打包后,运行app发现地图不显示了
debug模式正常显示

签名打包后release模式地图不显示

stackoverflow一番后,这是解决方式

原因就是,申请的google map API_KEY是放在了debug文件夹下,没有对应的release的 API_KEY
这里写图片描述

解决方式

1、build.gradle下添加manifestPlaceholders字段值

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            manifestPlaceholders = [ map_key:"AIzaSyAFhhAGX_9cW87jrdz06uDo96iweddwBl4" ]
        }
        debug {
            manifestPlaceholders = [ map_key:"AIzaSyAFhhAGX_9cW87jrdz06uDo96iweddwBl4" ]
        }
    }

2、修改注册文件

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="${map_key}"/>

再次签名打包运行,就OK了

仔细看Google Map的集成过程,其实都有Google官方的爱心指导,只是我当初没仔细看而已!

google_maps_api.xml

<resources>
    <!--
    TODO: Before you run your application, you need a Google Maps API key.

    To get one, follow this link, follow the directions and press "Create" at the end:

    https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=9B:03:08:37:67:8F:08:E8:D6:F5:ED:66:42:43:2E:EF:58:48:E0:C4%3Bcom.gaoyy.delivery4res

    You can also add your credentials to an existing key, using this line:
    9B:03:08:37:67:8F:08:E8:D6:F5:ED:66:42:43:2E:EF:58:48:E0:C4;com.***.d******s

    Alternatively, follow the directions here:
    https://developers.google.com/maps/documentation/android/start#get-key

    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
    string in this file.
    -->
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyAFhhAGX_9cW87jrdz06uDo96iweddwBl4</string>
</resources>

AndroidManifest.xml
<!--
     The API key for Google Maps-based APIs is defined as a string resource.
     (See the file "res/values/google_maps_api.xml").
     Note that the API key is linked to the encryption key used to sign the APK.
     You need a different API key for each encryption key, including the release key that is used to
     sign the APK for publishing.
     You can define the keys for the debug and release targets in src/debug/ and src/release/. 
-->
<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="${map_key}"/>

解决方式3:还是按照google官方的建议来,生成2个key,一个用于debug,一个用于release,签名打包。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值