管理 Google Maps API 密钥 [英] Managing Google Maps API keys

查看:27
本文介绍了管理 Google Maps API 密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用了大量 MapView 屏幕,我正在尝试弄清楚如何在调试环境和生产环境之间管理 API 密钥.显然,在 Eclipse 中无法更改调试应用程序密钥,因此我必须在该环境中使用调试映射 API 密钥.相反,如果没有生产应用程序密钥,则无法导出用于 Beta 测试的包,因此我必须在每个视图中更改地图 API 密钥才能创建工作包.

My application uses a large number of MapView screens and I'm trying to figure out how to manage the API key between the debug environment and production. Apparently, there is no way to change the debug application key in Eclipse so I must use a debug map API key in that environment. Conversely, there is no way to export a package for beta testing without a production application key so I must change the map API key in every view in order to create a working package.

我的第一个想法是这样做:

My first idea was to do this:

所有 MapView.xml 文件都有这个:

All MapView.xml files have this:

android:apiKey="@string/googleMapsAPIKey"

然后在strings.xml中我把这个:

And then in strings.xml I put this:

<string name="googleMapsPIKey">@string/debugGoogleMapsAPIKey</string>
<string name="debugGoogleMapsAPIKey">TheMagicKeyString</string>

如果这行得通,它将允许我更改 strings.xml 中的一行,并且所有 MapViews 都将在重建中更新.但它没有用.我猜 strings.xml 不能对自身进行引用.还有其他想法吗?

If this worked, it would allow me to change a single line in strings.xml and all the MapViews would get updated in the rebuild. But it didn't work. I guess strings.xml can't make references into itself. Any other ideas?

谢谢

推荐答案

我推荐一种更简单的方法,该方法涉及利用一个不太可能但更具体的资产文件夹来存放您的调试密钥.要进行设置,请创建一个包含以下内容的文件 res/values/maps-apikey.xml:

I recommend a simpler approach that involves taking advantage of an unlikely, but more specific asset folder to house your debug key. To set this up, create a file res/values/maps-apikey.xml with the following contents:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="maps_apikey">[productionApiKey]</string>
</resources>

然后每个本地开发人员将在 res/values-v1/maps-apikey.xml 中添加一个派生自该文件的文件,其中提供了他们的调试 API 密钥.在运行时,Android 系统会支持所有 Android 版本的更具体的版本 res/values-v1/maps-apikey.xml(因为所有版本至少都在 API 级别 1 或更高级别).

Then each developer locally will add a file derived from this one in res/values-v1/maps-apikey.xml where their debug API key is provided. At runtime, the Android system will favor the more specific version res/values-v1/maps-apikey.xml for all versions of Android (since all are at least on API level 1 or higher).

git 和 svn 等源代码控制系统允许您添加忽略"文件,该文件指示工具忽略此 res/values-v1/maps-apikey.xml 文件,以便每个开发人员不会意外地将其提交到存储库.要为 git 执行此操作,只需添加一个仅包含行 values-v1 的文件 res/.gitignore,然后提交此文件.

Source control systems like git and svn allow you to add an "ignore" file which directs the tools to ignore this res/values-v1/maps-apikey.xml file so that each developer does not accidentally commit it to the repository. To do this for git, simply add a file res/.gitignore which contains only the line values-v1 then commit this file.

要发布您的软件,只需在导出发布前删除 res/values-v1/maps-apikey.xml 文件即可.然后生成的 APK 将引用 res/values/maps-apikey.xml 中的版本,这是您正确的生产密钥.

To release your software, simply delete the res/values-v1/maps-apikey.xml file before exporting a release. The resulting APK will then reference the version in res/values/maps-apikey.xml which is your correct production key.

这篇关于管理 Google Maps API 密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆