5

我的应用程序使用了大量的 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 中的一行,并且所有 MapViews 都将在重建中得到更新。但它没有用。我猜 strings.xml 不能对自身进行引用。还有其他想法吗?

谢谢

4

8 回答 8

9

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

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

然后每个本地开发人员将在 res/values-v1/maps-apikey.xml 中添加一个派生自该文件的文件,其中提供了他们的调试 API 密钥。在运行时,Android 系统将支持更具体的版本 res/values-v1/maps-apikey.xml 用于所有 Android 版本(因为所有版本都至少在 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 中的版本,这是您正确的生产密钥。

于 2011-11-11T09:17:44.883 回答
3

你在做正确的事,但不是我认为的正确方式。在 strings.xml 中声明您的字符串,如下所示:

<string name="googleMapsAPIKey">TheMagicKeyString</string>
<!-- You can add this at the end as comment to keep a copy of another key for instance, to exchange it between debug and production-->

请注意,您没有在 2 个字符串中给出相同的名称...一个称为调试,而不是另一个。

于 2010-08-09T17:55:38.783 回答
2

我正在使用 maven 和maven-android-plugin,以及仅在发布配置中使用的 res-overlay 目录。

因此,我有一个res/values/google_api_key.xml包含调试密钥的res-overlay-production/values/google_api_key.xml文件和一个包含生产密钥的文件,后者在生产版本中覆盖了前者。

我也在使用 Eclipse ADT,但这不知道发布配置,所以很高兴看到调试密钥,我可以使用 Android XML 编辑器来设置我的地图视图。

于 2011-02-22T10:03:14.323 回答
1

我认为这是一个很好的方法!通过 Sephy 的修正,它应该可以完美运行。

最重要的是,如果你想在调试和发布版本之间自动切换密钥,你可以看看我刚刚写的这篇文章:http: //blog.cuttleworks.com/2011/02/android-dev-prod -构建/。它帮助我在为不同的环境构建时不再担心配置。

于 2011-02-01T03:04:17.410 回答
1

尝试这样的事情:

String debugMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String releaseMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

String mapKey = BuildConfig.DEBUG ? debugMapKey : releaseMapKey;

MapView mv = new MapView(this, mapKey);
于 2012-11-15T10:08:13.333 回答
1

我正在使用 maven-android-plugin 并且我正在使用一种偷偷摸摸的令牌替换机制来做到这一点。

我正在使用“morseflash”示例,并将其添加到 maven-resources 插件配置中:

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>resources</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
      <delimiters>
        <delimiter>${*}</delimiter>
        <delimiter>&gt;08*&lt;</delimiter>
      </delimiters>
    </configuration>
</plugin>

资源插件允许您为搜索/替换定义奇怪的分隔符。默认分隔符是${*},但我添加>*<了它以匹配 XML 元素内容。(实际上我用过>08*<;我稍后会解释原因。)

然后,在我的 strings.xml 中,我写了这个:

<string name="googleMapsAPIKey">08DEBUGKEYABCDEFBLAHBLAHBLAH</string>

并在发布配置文件中定义了一个 Maven 属性,如下所示:

<profile>
    <id>release</id>
    <!-- via this activation the profile is automatically used when the release is done with the maven release
    plugin -->
    <activation>
        <property>
            <name>performRelease</name>
            <value>true</value>
        </property>
    </activation>
    <properties>
        <DEBUGKEYABCDEFBLAHBLAHBLAH>&gt;08RELEASEKEYABCDEFBLAHBLAHBLAH&lt;</DEBUGKEYABCDEFBLAHBLAHBLAH>
    </properties>
    <-- ... -->
</profile>

这会创建一个以调试 Maps 键命名的 Maven 属性,其值是发布调试键。

不幸的是,Maven 属性不允许以数字开头。我的调试密钥和我的发布密钥都以08Wfj...所以我使用了分隔符>08*<并确保包含>08在我的替换字符串中。

于 2011-05-12T23:25:30.993 回答
0

您可以在 Prefs 窗口 -> Android-> Build 中设置自定义调试密钥库。

于 2010-08-09T18:32:24.683 回答
0

在谷歌开发者控制台中,您可以为多个 sha1 代码创建一个密钥 https://stackoverflow.com/a/13881624/1433372

于 2014-04-21T20:24:25.420 回答