集成GoogleMap正確的簽名打包姿勢

在集成了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,簽名打包。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章