Android:自动选择调试/发布Maps v2 api密钥?

问题描述 投票:42回答:4

我在我的项目中使用Google Maps v2 API。在Google Maps v2中,调试/发布API密钥在AndroidManifest.xml中定义。我见过the link但是在那个map键中是在xml布局文件中定义的,而不是在AndroidManifest.xml中。那么我可以在AndroidManifest.xml中为我的项目定义调试和释放密钥吗?

我想在AndroidManifest.xml中这样的事情:

如果是调试模式:

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="@string/debug_map_api_key"/>

如果发布模式:

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="@string/release_map_api_key"/>
android google-maps key android-manifest google-maps-android-api-2
4个回答
66
投票

我使用以下步骤解决了这个问题:

在Google Developer API控制台中

  1. 点击Create New Android key ...
  2. 在cmd.exe /终端:keytool -list -v -keystore mystore.keystore
  3. 密码:android
  4. 现在输入SHA1 key;package name进行调试,然后按Enter键
  5. 输入SHA1 key;package name进行发布
  6. 单击“创建”

现在使用这个API密钥你的项目

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="@string/common_map_api_key"/>

25
投票

One of the Best way to use build.gradle file in latest Android 5.0 and Android 6.0 (API 20, 21,22,23)

那么你可以简单地使用它们,而无需在gradle中创造产品风味。这是我们可以通过Gradle实现的另一个例子。您可以通过两个简单的步骤实现它。

  • 将自定义值添加到manifestplaceholders build.gradle文件。

见下文

buildTypes {
    debug {
        manifestPlaceholders = [ mapApiKeyValue:"GHjaSyAjlyp3O831lgaonHMXsd-_DpQ3002x3S4"]
    }

    release {
        manifestPlaceholders = [ mapApiKeyValue:"AIzaSyAuMGDLr2HeuRed4JA0CrdYYdZRjeC3EA"]
    }
}
  • 编辑清单文件,如下所示。

我的清单文件的一部分

  <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="${mapApiKeyValue}" />

此解决方案适用于最新的Android 5.0和Android 6.0(API 20,21,22,23)


在5/3/2018更新了Xamarin表单和Xamarin Native Apps

在Android Project中打开qazxsw poi并添加以下代码

AssemblyInfo.cs

要检查AndroidManifest文件,请转到#if DEBUG [assembly: MetaData("com.google.android.maps.v2.API_KEY", Value = "DebugKey123123123")] #else [assembly: MetaData("com.google.android.maps.v2.API_KEY", Value = "ReleaseKey123123123")] #endif 文件夹并打开清单文件并检查元信息,

obj/Debug/android

15
投票

对于需要维护单独密钥的组织,您可以将它们放在Android Studio的单独目录中。确保您使用的<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="DebugKey123123123" /> 子目录与flavor或src匹配

来自buildType

Building Your Project with Gradle

To build each version of your app, the build system combines source code and resources from: src/main/ - the main source directory (common to all variants) src/<buildType>/ - the build type source directory src/<flavorName>/ - the flavor source directory

projectroot/yourapp/build.gradle

buildTypes { debug { runProguard false debuggable true } release { runProguard true debuggable false ... }

projectroot/yourapp/src/main/AndroidManifest.xml

... <application android:name=".MyApplication" android:theme="@style/Theme"> <!-- Don't put your key here --> ... 中,完全符合应用程序的名称。

projectroot/yourapp/src/debug/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:name="com.hipmunk.android.HipmunkApplication"> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="yourdebugkey" /> </application> </manifest>

projectroot/yourapp/src/release/AndroidManifest.xml

7
投票

由于您使用的是gradle,因此可以执行以下操作:

的build.gradle

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:name="com.hipmunk.android.HipmunkApplication">
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="release key" />
    </application>
</manifest>

并在您的AndroidManifest.xml中

android {
  .. .. ...
    buildTypes {
       debug {
          resValue "string", "google_maps_api_key", "[YOUR DEV KEY]"
       }
       release {
           resValue "string", "google_maps_api_key", "[YOUR PROD KEY]"
       }
    }
  }

这样,您只有一个AndroidManifest.xml,并根据您的构建类型设置值。希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.