管理 Google 地图 API 密钥
我的应用程序使用大量 MapView 屏幕,我正在尝试找出如何管理调试环境和生产环境之间的 API 密钥。显然,无法更改 Eclipse 中的调试应用程序密钥,因此我必须在该环境中使用调试映射 API 密钥。相反,如果没有生产应用程序密钥,则无法导出包进行 beta 测试,因此我必须在每个视图中更改地图 API 密钥才能创建工作包。
我的第一个想法是这样做:
所有 MapView.xml 文件都有这个:
android:apiKey="@string/googleMapsAPIKey"
然后在 strings.xml 中我放了这个:
<string name="googleMapsPIKey">@string/debugGoogleMapsAPIKey</string>
<string name="debugGoogleMapsAPIKey">TheMagicKeyString</string>
如果这有效,它将允许我更改 strings.xml 中的一行,并且所有 MapView 都会更新在重建中。但这没有用。我猜想 strings.xml 无法对其自身进行引用。还有其他想法吗?
谢谢
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:
All MapView.xml files have this:
android:apiKey="@string/googleMapsAPIKey"
And then in strings.xml I put this:
<string name="googleMapsPIKey">@string/debugGoogleMapsAPIKey</string>
<string name="debugGoogleMapsAPIKey">TheMagicKeyString</string>
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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我建议采用一种更简单的方法,即利用一个不太可能但更具体的资产文件夹来存放调试密钥。要进行此设置,请创建一个包含以下内容的文件 res/values/maps-apikey.xml:
然后每个开发人员在本地将在 res/values-v1/maps-apikey.xml 中添加一个从此文件派生的文件,其中包含调试 API提供了钥匙。在运行时,Android 系统将支持所有 Android 版本的更具体的版本 res/values-v1/maps-apikey.xml (因为所有版本都至少处于 API 级别 1 或更高级别)。
git 和 svn 等源代码控制系统允许您添加一个“忽略”文件,该文件指示工具忽略此 res/values-v1/maps-apikey.xml 文件,以便每个开发人员不会意外地将其提交到存储库。要对 git 执行此操作,只需添加一个文件 res/.gitignore,其中仅包含行values-v1,然后提交该文件。
要发布您的软件,只需在导出版本之前删除 res/values-v1/maps-apikey.xml 文件即可。然后,生成的 APK 将引用 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:
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).
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.
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.
你做的是正确的事,但不是我认为的正确方式。在 strings.xml 中声明您的字符串,如下所示:
请注意,您没有在 2 个字符串中给出相同的名称...一个称为 debug,另一个称为 debug。
you are doing the right thing but not the right way I think. Declare your string in strings.xml like this :
Note that you didn't give the same name at your 2 strings... One is called debug and not the other.
我正在使用 maven 和 maven-android-plugin,带有 res -overlay 目录,仅在发布配置中使用。
因此,我有一个包含调试密钥的
res/values/google_api_key.xml
文件和一个包含生产密钥的res-overlay-product/values/google_api_key.xml
文件,并且后者在生产版本中覆盖前者。我也在使用 Eclipse ADT,但是它不知道发布配置,因此很高兴看到调试密钥,并且我可以使用 Android XML 编辑器来设置我的地图视图。
I'm using maven and the maven-android-plugin, with a res-overlay directory that is only used in the release configuration.
Consequently, I have a
res/values/google_api_key.xml
file containing the debug key and ares-overlay-production/values/google_api_key.xml
file containing the production key, and the latter overrides the former in the production release.I'm also using Eclipse ADT, but this doesn't know about the release configuration, so it's happy to see the debug key and I can use the Android XML editors to set up my map views.
我正在使用 maven-android-plugin 并使用偷偷摸摸的令牌替换机制来执行此操作。
我正在使用“morseflash”示例,并将其添加到 maven-resources 插件配置中:
资源插件允许您为搜索/替换定义奇怪的分隔符。默认分隔符是
${*}
,但我添加了>*<
,以便它与 XML 元素内容匹配。 (实际上我使用了>08*<
;我稍后会解释原因。)然后,在我的 strings.xml 中,我这样写:
并在发布配置文件中定义了一个 Maven 属性,像这样:
创建一个以调试映射键命名的 Maven 属性,其值是发布调试键。
不幸的是,Maven 属性不允许以数字开头。我的调试密钥和发布密钥均以
08Wfj...
开头,因此我使用了分隔符>08*<
并确保包含>08
在我的替换字符串中。I'm using maven-android-plugin and I'm using a sneaky token replacement mechanism to do this.
I'm using the "morseflash" sample, and added this to the maven-resources plugin configuration:
The resources plugin allows you to define weird delimiters for search/replace. The default delimiter is
${*}
, but I added>*<
so it would match XML element content. (Actually I used>08*<
; I'll explain why in a moment.)Then, in my strings.xml, I wrote this:
And defined a Maven property in the release profile, like this:
That creates a Maven property named after the debug Maps key, whose value is the release debug key.
Unfortunately, Maven properties are not allowed to start with numbers. My debug key and my release key both started with
08Wfj...
So I used the delimiter>08*<
and made sure to include>08
in my replacement string.我认为这是一个很好的方法!经过 Sephy 的修正,它应该可以完美地工作。
最重要的是,如果您想在调试和调试之间自动切换按键;发布版本,你可以看看我刚刚写的这篇文章: http ://blog.cuttleworks.com/2011/02/android-dev-prod-builds/。它帮助我在构建不同环境时不再担心配置。
I think this is a good way to do it! With Sephy's correction it should work perfectly.
On top of that, if you want to switch keys automatically between the debug & release builds, you can have a look at this post I just wrote: http://blog.cuttleworks.com/2011/02/android-dev-prod-builds/. It helped me stop worrying about configuration when building for different environments.
尝试这样的事情:
Try something like this:
您可以在“首选项”窗口 -> 中设置自定义调试密钥库Android->构建。
You can set custom debug keystore in Prefs window -> Android->Build.
在谷歌开发者控制台中,您可以为多个 sha1 代码创建一个密钥
https://stackoverflow.com/a/13881624/1433372
In the google developer console you can create one key for multiple sha1 codes
https://stackoverflow.com/a/13881624/1433372