Android:自动选择调试/发布地图api键? [英] Android: automatically choose debug/release Maps api key?

查看:157
本文介绍了Android:自动选择调试/发布地图api键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OBSOLETED:这个旧问题是指旧版Google Maps v1 API。使用v2 API时,您可以在一个 Google API控制台条目中使用多个证书指纹。 API密钥不再存储在清单中或代码中。






是否可以自动检测哪个证书用于签署APK?我想要在应用程序中同时调试和发布Maps证书,并将有效的一个传递给MapView构造函数。



使用这样的设置,我在发布应用程序时不会出错 - 我我在仿真器和我的设备上使用调试证书,然后在发送应用程序到Market之前签名。



我在考虑检测我的特定设备,或者调试器是否连接但它并不完美。也许某些文件标记需要调试证书?有没有更好的方法?

解决方案

有一种新的方法来确定它是一个调试版本还是在 SDK工具修订版17 。新功能概述摘录:


现在生成一个名为 BuildConfig 的类,其中包含一个 DEBUG 常量,根据您的构建类型自动设置。您可以检查代码中的( BuildConfig.DEBUG )常量来运行仅调试功能。


所以现在你可以这样写:

  if(BuildConfig.DEBUG)
{
/ /你的调试代码到这里
}
else
{
//你的发行代码到这里
}
pre>

更新:我在ADT中遇到错误:有时 BuildConfig.DEBUG 在导出应用程序包后是 true 。说明如下: http://code.google.com/p/android/issues/detail? id = 27940


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?

解决方案

There 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:

Builds now generate a class called BuildConfig containing a DEBUG constant that is automatically set according to your build type. You can check the (BuildConfig.DEBUG) constant in your code to run debug-only functions.

So now you can simply write something like this:

if (BuildConfig.DEBUG)
{
    //Your debug code goes here
}
else
{
    //Your release code goes here
}

UPDATE: I've encountered bug in ADT: sometimes BuildConfig.DEBUG is true after exporting application package. Description is here: http://code.google.com/p/android/issues/detail?id=27940

这篇关于Android:自动选择调试/发布地图api键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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