Android的如何从AndroidManifest设置谷歌地图API金钥动态不 [英] Android how to set Google Map Api Key dynamically not from AndroidManifest
问题描述
我有一个要求,设置谷歌地图API密钥动态即形成code没有明显的。目前我得到这个价值形态string.xml。这是根据需要而不是preferred方式。
I have a requirement to set google map Api key dynamically i.e form the code not from manifest .Currently I am getting this value form string.xml . Which is not the preferred way according to the requirement .
<meta-data`android:name="com.google.android.maps.v2.API_KEY"`
android:value="@string/map_api_key"
/>
我如何设置这个值的形式code。
How can I set this value form code .
推荐答案
你这样做的方式是作为一个明显的占位符。
The way you would do this is as a manifest placeholder.
在AndroidManifest.xml后把一个变量的声明是这样的:
In your AndroidManifest.xml put a variable declaration like this:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${mapsApiKey}"/>
然后在build.grade输入:
then in your build.grade enter this:
android {
defaultConfig {
manifestPlaceholders = [ mapsApiKey:"debugKey"]
}
buildTypes {
release {
manifestPlaceholders = [ mapsApiKey:"prodKey" ]
}
}
}
文件是在这里:<一href=\"http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-Placeholder-support\" rel=\"nofollow\">http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-Placeholder-support
请注意我在这里作出假设你的用例。这仅适用,如果你想基于构建类型换出钥匙。
Note I'm making assumptions here about your use case. This only applies if you're trying to swap out keys based on build type.
这篇关于Android的如何从AndroidManifest设置谷歌地图API金钥动态不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!