Android:自动选择调试/发布地图 API 密钥?
已废弃:这个老问题是指已废弃的 Google 地图 v1 API。使用 v2 API 时,您可以在一个 Google API Console 条目中使用多个证书指纹。 API Key不再存储在Manifest或代码中。
是否可以自动检测哪个证书用于签署APK?我希望在应用程序中同时拥有调试和发布地图证书,并将有效的证书传递给 MapView 构造函数。
通过这样的设置,我在发布应用程序时不会犯错误 - 我在模拟器和我的设备上使用调试证书,然后在将应用程序发送到市场之前使用版本一进行签名。
我正在考虑检测我的特定设备或调试器是否已连接,但它并不完美。也许某些文件标记需要调试证书?还有更好的办法吗?
OBSOLETED: this old question refers to obsoleted Google Maps v1 API. When using v2 API, you can use multiple certificate fingerprints in one Google API Console entry. API Key is no longer stored in Manifest nor code.
Is it possible to automatically detect, which certificate was used for signing APK? I'd like to have both debug and release Maps certificates in application and pass valid one to MapView constructor.
With such setup I will not make mistake while releasing application - I'm using debug certificate on emulator and my device, then sign with release one before sending app to Market.
I was thinking about detecting my particular device or whether debugger is connected but it is not perfect. Maybe some file marking need for debug certificate? Is there any better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
SDK 工具,修订版 17 中有一种新方法可以确定它是调试版本还是发布版本。新功能概述摘录:
所以现在你可以简单地编写如下内容:
更新:我在ADT中遇到了错误:有时
BuildConfig.DEBUG
在导出应用程序后为true
包裹。说明如下:http://code.google.com/p/android/issues/detail? id=27940There is a new way to determine is it a debug build or release one in SDK Tools, Revision 17. An excerpt from new features overview:
So now you can simply write something like this:
UPDATE: I've encountered bug in ADT: sometimes
BuildConfig.DEBUG
istrue
after exporting application package. Description is here: http://code.google.com/p/android/issues/detail?id=27940API 密钥也有同样的麻烦。这是基于上面的链接和 来自 Bijarni 的示例(不知何故对我不起作用),我现在使用这个方法:
你必须找出你的调试签名的 hashValue() 一次,只需输出 sigs[i].hashCode() 。
然后,我不想动态添加MapView,而是使用xml文件。您无法在代码中设置 api key 属性并使用 xml 布局,因此我使用这个简单的方法(尽管复制 xml 布局不太美观):
在我的 MapActivity 中:
Had the same hassle with the API key. Here's a full solution, based on the above link and example from Bijarni (which somehow didn't work for me), I use now this method:
You have to find out your debug signature's hashValue() once, just output sigs[i].hashCode().
Then, I didn't want to dynamically add the MapView, but rather use the xml file. You cannot set the api key attribute in the code and use an xml layout, so I use this simple method (though copying the xml layout isn't so beautiful):
In my MapActivity:
确定是否是调试版本的更简单方法是检查应用程序信息上的调试标志,而不是签名哈希。
找到调试版本后,您可以使用不同的资源来显示地图,也可以在应用程序中创建地图视图并添加到布局中。
Much easier way to determine whether it is a debug build is by checking the debug flag on the application info than the signature hash.
Once debug build is found, either you can use different resource for showing map or create the mapview within the app and add to a layout.
这里的所有答案似乎都已经过时了,如果您使用的是 android studio,那么 gradle 是正确的选择
在 build.gradle 中使用不同的键
以及在 AndroidManifest.xml
来源
如果您想保存一些密码以进行调试并以不同的方式发布,那么您应该遵循这个
All answers here seem outdated, if you are using android studio then gradle is the way to go
Use different keys in your build.gradle
And in your AndroidManifest.xml
source
And if you want to save some passwords for debug and release differently then you should follow this
我通过将 API 密钥设置为存储在
local.properties
中的属性,解决了将 api 密钥错误集成到构建过程和源代码控制中的可怕问题。我必须将以下内容添加到build.xml
中:现在,当然我必须在我的项目根目录中创建
mapview.xml.tpl
(它不能转到 < code>res/layout 因为它会破坏构建过程):在预编译期间,模板被复制到正确的位置,@apiKey@ 被替换为真实的密钥。不幸的是,在此阶段我还没有找到区分调试和发布版本的方法,因此要编译发布版本,我只需将发布 apiKey 添加到 ant 参数中:
这种方法与 SCM 集成得很好(我不需要签入键)并且可以接受构建过程。
I have worked around the horrendous mis-integration of the api keys into the build process and source control by making it a property stored in
local.properties
. I had to add the following tobuild.xml
:Now, of course I had to create
mapview.xml.tpl
in my projects root (it can't go tores/layout
because it will break the build process):During pre-compilation, the template is copied to the right place and @apiKey@ is replaced with the real key. Unfortunately I have not found a way to distinguish between debug and release builds in this phase, so to compile for release, I just add the release apiKey to the ant parameters:
This approach integrates well with SCM (I do not need to check in the keys) and acceptably with the build process.
如果您仍然感兴趣,我刚刚在博客中介绍了另一种方法。通过对 Android 构建脚本进行简单更改,您可以切换 Map API 密钥以及所有其他所需的发布更改。我喜欢这一点的是,该版本中没有任何与调试相关的内容,并且您可以保持 XML 布局与以前一样。
http://blog.cuttleworks.com/2011/02/android-开发-产品-构建/
If you're still interested I just blogged about another way to do this. With a simple change to the Android build script, you can switch the Map API key as well as all other required release changes. What I like about this is that nothing debug-related goes into the release, and you can keep the XML layouts just the way they were before.
http://blog.cuttleworks.com/2011/02/android-dev-prod-builds/
我认为在 Google API 的控制台中创建一个包含发布密钥和调试密钥(都映射到同一个包)的条目效果很好,并且是一种更简单的方法,不必担心您是在调试还是编译发布版本。
此处概述了解决方案
I think that creating an entry in the Google API's console which includes both your release key and your debug key (both mapping to the same package) works great and is a much simpler way to not have to worry about whether you are debuging or compiling a release version.
The solution is outlined here
我最终在 SD 卡上得到了特殊文件 - 如果存在,请使用调试密钥;缺失 - 使用版本一。它有效。
编辑:查看新接受的答案,效果更好
I've ended up with the special file on SD card - if present, use debug key; missing - use release one. And it works.
EDIT: see new accepted answer, it works better
我不知道这是否对任何人有帮助,但我在这里合并了一些其他建议以生成以下 MapViewActivity。
在此示例中,仅当这是调试版本且文件存在时才使用 R.layout.map_dbg(将此文件添加到 .gitignore)。
这种方法的优点是:
这种方法的缺点是:
您需要记住每次更新map.xml时更新map_dbg.xml
I don't know if this helps anyone but I have merged some of the other suggestions here to produce the following MapViewActivity.
In this example R.layout.map_dbg is only used if this is a debug build and the file exists (add this file to your .gitignore).
The advantages of this approach are :
The disadvantages of this approach are :
you need to remember to update map_dbg.xml every time map.xml is updated
我设置了一个简单的 ant 目标,用调试密钥或发布密钥替换 apikey。这非常简单,并且可以使代码中没有不需要的逻辑。
I have setup a simple ant target that replaces the apikey with either a debug key or a release key. This is really simple and keeps the code free of unwanted logic.
在 Map V2 中,使用 Android Studio Gradle 工具可以轻松发送单独的密钥。我为此实施了一种简单的方法。请查看此处的链接。
In Map V2 Its easy to send seperate keys using Android Studio Gradle tool. I have implemented an easy way for that. please check the link here.