【问题标题】:Multiple API keys in the same Android project同一个 Android 项目中的多个 API 密钥
【发布时间】:2013-05-12 10:43:56
【问题描述】:

是否可以在同一代码库中为 Google Maps Android API 指定多个键?

看起来我每次更改密钥库时都必须更改清单文件中的密钥。这不是很方便,恕我直言,如果您需要测试使用密钥形式调试和发布密钥库的应用程序签名。

【问题讨论】:

    标签: android android-studio api-key


    【解决方案1】:

    我同时在清单中添加了 both 键。像这样

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        ..
        android:versionCode="1"
        android:versionName="1.0" >
    
            <!-- RELEASE key -->
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="my-release-keu" />
    
            <!-- DEBUG key -->
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="my-debug-key" />
    
        </application>
    
    </manifest>
    

    显然,这行得通。看起来谷歌代码足够聪明,可以自动使用相关密钥。

    【讨论】:

    • 这可能听起来很奇怪,但您必须按照 API 控制台页面上看到的完全相同的顺序放置您的密钥
    【解决方案2】:

    我不认为这是你想要做的。您应该在 Google Developer API 控制台上将调试和发布 SHA1 密钥添加到 API 密钥。看看this answer

    【讨论】:

      【解决方案3】:

      AFAIK,这样做没有编程形式。对于商品,您可以在 strings.xml 中定义 API 密钥,并从清单中检索它

      【讨论】:

        【解决方案4】:
        String debugMapKey   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String releaseMapKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        
        String mapKey = BuildConfig.DEBUG ? debugMapKey : releaseMapKey;
        
        MapView mv = new MapView(this, mapKey);
        

        【讨论】:

          猜你喜欢
          • 2016-02-28
          • 1970-01-01
          • 2019-12-18
          • 2021-02-24
          • 1970-01-01
          • 1970-01-01
          • 2021-08-20
          • 1970-01-01
          • 2012-10-24
          相关资源
          最近更新 更多