java - 谷歌 API key 不适用于 Android

标签 java android api google-maps android-studio

我在我的应用程序中使用 Google API,但只能让调试 ssh 与 Google API 一起工作。 我有一个用于签名的 keystore ,我过去对以前的应用程序也做过同样的事情,没有遇到任何问题。我还启用了所有 API。我正在使用发布 apk 签署我的应用程序。我在 SSH1 中使用来自该版本 apk 的 SSH1 作为 Google API 凭据。我在我的应用程序中使用了 Google 的 key 。我已经阅读了所有关于此的堆栈溢出和谷歌文档,并尝试了几乎所有的方法,但我无法让它工作。 它仅在我调试我的应用程序时有效(在这种情况下,我使用由调试 keystore 生成的 SSH1 和我与该 SSH1 一起使用的 key )。 但是,当我与我的人签署发布 keystore 并在应用商店的 Beta 版上发布并下载到我的手机上时,我收到错误消息,指出我的 API key 无效。如果有帮助,这是我的 list 和构建文件以及我的布局文件。谢谢。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dev.cromer.jason.whatsappening" >

    <permission android:name="com.dev.cromer.jason.whatsappening.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.dev.cromer.jason.whatsappening.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@drawable/icon_logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".activities.MapActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".activities.SetMarkerTitleActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".activities.SearchPlaceActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".activities.SetMarkerDescriptionActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".activities.MarkerDescriptionActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

这是我的构建:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.dev.cromer.jason.whatsappening"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 12
        versionName "1.012"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.google.android.gms:play-services:7.8.0'
}

和我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/AppTheme">

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        tools:context="com.dev.cromer.jason.whatsappening.activities.MapActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment">
    </fragment>


    <EditText
        android:id="@+id/searchBarEditText"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:background="@drawable/textview_border"
        android:alpha=".8"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:hint="@string/search_bar_hint"
        android:singleLine="true"
        android:textColor="#000000"
        android:padding="8dp" />


    <ImageView
        android:id="@+id/poweredBy"
        android:contentDescription="@string/powered_by_google_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/powered_by_google_light"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

最佳答案

更新:

事实证明,我将我的 API key 存储在项目调试目录下的资源 xml 文件中。 我需要将它放在我的项目或发布目录中,然后在我的 list 文件中指定该字符串,而不是调试目录下的字符串。这是我遇到的问题。感谢您抽出宝贵时间发表评论。

关于java - 谷歌 API key 不适用于 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32546550/

相关文章:

java - 如何在 Google 开发者控制台中更改目录

api - Stripe Webhooks - 我可以发送 200 以外的响应代码吗?

java - 如何在 JPA 实体中将字段设置为可选字段?

java - JTextPane 无法正确渲染 span 标签

java - 即使在android中发生重大运动后,触发事件监听器的onTrigger方法也不会被调用

应用程序进入后台时Android自动注销

api - 在 Twitter REST API 中,是否可以找到回复计数?

rest - werkzeug.exceptions.BadRequestKeyError : 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'id'

Java 数组更新

java - GWT TreeItem(String) 构造函数