Java 在 Android Studio 中导入 Google Play 服务库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21081598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:26:43  来源:igfitidea点击:

Import Google Play Services library in Android Studio

javaandroidgradleandroid-studiogoogle-play-services

提问by losttime

I have an Android project that has been developed entirely within Android Studio (currently version 4.2, gradle version 1.9-all). I want to add functionality from Google Play Services.

我有一个完全在 Android Studio 中开发的 Android 项目(当前版本为 4.2,gradle 版本为 1.9-all)。我想添加来自 Google Play 服务的功能。

The project is unable to resolve GooglePlayServicesUtil, and when I enter the import manually (shown below), I get Cannot resolve symbol 'common'.

项目无法解析GooglePlayServicesUtil,当我手动输入导入时(如下所示),我得到Cannot resolve symbol 'common'.

import com.google.android.gms.common.GooglePlayServicesUtil;

Any idea what I need to do to get GooglePlayServicesUtilto resolve?

知道我需要做什么GooglePlayServicesUtil才能解决吗?

What I've Tried

我试过的

From the Google Play Services Setupit looks like I just have to add the com.google.android.gms:play-services:4.+dependency to my build.gradlefile (and resync project files with gradle) to make the SDK available to my project. I do get an "exploded bundle" in ProjectName/module/build/exploded-bundles, but that doesn't seem like it does the trick.

Google Play Services Setup看来,我只需将com.google.android.gms:play-services:4.+依赖项添加到我的build.gradle文件(并使用 gradle 重新同步项目文件)即可使 SDK 可用于我的项目。我确实在 中得到了一个“爆炸包” ProjectName/module/build/exploded-bundles,但这似乎不起作用。

I have Google Play Services, Android Support Repository and Google Repository installed from the SDK Manager already. I've also deleted and reinstalled them multiple times :)

我已经从 SDK Manager 安装了 Google Play Services、Android Support Repository 和 Google Repository。我也多次删除并重新安装它们:)

Edit:

编辑:

Might I need to manually add google_play_services as a Project/Global Library? I've attempted with no success.

我是否需要手动添加 google_play_services 作为项目/全局库?我尝试过但没有成功。

I'm trying to verify that I'm developing against the Platform API with Google Services (if that's even possible), but I'm not sure that's the case. Nothing I change seems to do anything.

我正在尝试验证我是否正在使用 Google 服务针对 Platform API 进行开发(如果可能的话),但我不确定是否是这种情况。没有我改变似乎做任何事情。

The External Libraries of my project shows:

我的项目的外部库显示:

  • < Android API 19 Platform >
  • < 1.7 >
  • joda-time-2.3
  • support-v4-13.0.0
  • < Android API 19 平台 >
  • <1.7>
  • 乔达时间2.3
  • 支持-v4-13.0.0

Source Code

源代码

This is my ProjectName/module/build.gradle file:

这是我的 ProjectName/module/build.gradle 文件:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}

apply plugin: 'android'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.1'
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    signingConfigs {
    }
    productFlavors {
    }
}

dependencies {
    compile 'com.google.android.gms:play-services:4.+'
    compile 'joda-time:joda-time:2.3@jar'
}

The com.google.android.gms.versionnumber resolves fine in my manifest. Here is my ProjectName/module/src/main/AndroidManifest.xml file:

com.google.android.gms.version数字在我的清单中解析得很好。这是我的 ProjectName/module/src/main/AndroidManifest.xml 文件:

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

    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="19"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.android.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>
        <activity android:name=".SecondActivity"/>

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

        <provider
            android:name=".DataProvider"
            android:authorities="com.example.android.provider" >
        </provider>

        <receiver android:name=".WidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/widget_info" />
        </receiver>

        <service android:name=".DatabaseService" />
        <service android:name=".WidgetProvider$UpdateService" />
    </application>

</manifest>

Here is my MainActivity, where I'm trying to check whether GooglePlayServices is Available:

这是我的 MainActivity,我正在尝试检查 GooglePlayServices 是否可用:

package com.example.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.GooglePlayServicesUtil;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onResume() {
        Log.i(TAG, "onResume");
        GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
    }
}

采纳答案by pyus13

Try this once and make sure you are not getting any error in project Structure saying that "ComGoogleAndroidGmsPlay not added"

尝试一次,确保您在项目结构中没有收到任何错误,提示“未添加 ComGoogleAndroidGmsPlay”

Open File > Project Structureand check for below all. If error is shown click on Red bulb marked and click on "Add to dependency".

打开File > Project Structure并检查以下所有内容。如果显示错误,请单击标记的红色灯泡,然后单击“添加到依赖项”。

GMS dependency

GMS 依赖

This is a bug in Android Studio and fixed for the next release(0.4.3)

这是 Android Studio 中的一个错误,已在下一个版本 (0.4.3) 中修复

回答by quinnjn

I just tried out your build.gradleand it worked fine for me to import GMS, so that's not the issue.

我刚刚尝试了你的build.gradle,导入 GMS 对我来说效果很好,所以这不是问题。

This was with Google Play services (rev 13)and Google Repository (rev 4). Check out those are installed one more time :)

这是用Google Play services (rev 13)Google Repository (rev 4)。再看看那些安装了一次:)

回答by Prakash

I had similar issue Cannot resolve com.google.android.gms.common.

我有类似的问题无法解决 com.google.android.gms.common。

I followed setup guide http://developer.android.com/google/play-services/setup.htmland it works!

我遵循了安装指南http://developer.android.com/google/play-services/setup.html并且它有效!

Summary:

概括:

  1. Installed/Updated Google Play Services, and Google Repository from SDK Manager
  2. Added dependency in build.gradle: compile 'com.google.android.gms:play-services:4.0.30'
  3. Updated AndroidManifest.xml with <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
  1. 安装/更新 Google Play 服务,以及来自 SDK 管理器的 Google 存储库
  2. 在 build.gradle 中添加了依赖项: compile 'com.google.android.gms:play-services:4.0.30'
  3. 更新了 AndroidManifest.xml <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

回答by Cristiana Chavez

I solved the problem by installing the google play services package in sdk manager.

我通过在 sdk 管理器中安装 google play 服务包解决了这个问题。

After it, create a new application & in the build.gradle add this

之后,创建一个新的应用程序 & 在 build.gradle 中添加这个

compile 'com.google.android.gms:play-services:4.3.+'

Like this

像这样

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.google.android.gms:play-services:4.3.+'
}

回答by Viswa

//gradle.properties

//gradle.properties

systemProp.http.proxyHost=www.somehost.org

systemProp.http.proxyHost=www.somehost.org

systemProp.http.proxyPort=8080

systemProp.http.proxyPort=8080

systemProp.http.proxyUser=userid

systemProp.http.proxyUser=userid

systemProp.http.proxyPassword=password

systemProp.http.proxyPassword=密码

systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost

systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost

回答by Angel Balderas

After hours of having the same problem, notice that if your jar is on the libs folder will cause problem once you set it upon the "Dependencies ", so i just comment the file tree dependencies and keep the one using

在遇到相同问题数小时后,请注意,如果您的 jar 位于 libs 文件夹中,一旦您将其设置为“依赖项”,就会导致问题,所以我只评论文件树依赖项并保持使用

dependencies

依赖

//compile fileTree(dir: 'libs', include: ['*.jar'])  <-------- commented one 
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:appcompat-v7:22.2.1'

and the problem was solved.

问题就解决了。

回答by Asad

I got the same problem. I just tried to rebuild, clean and restart but no luck. Then I just remove

我遇到了同样的问题。我只是尝试重建,清理并重新启动,但没有运气。然后我只是删除

compile 'com.google.android.gms:play-services:8.3.0'

from build.gradle and resync. After that I put it again and resync. Next to that, I clean the project and the problem is gone!!

从 build.gradle 和重新同步。之后,我再放一次并重新同步。接下来,我清理了项目,问题就消失了!!

I hope it will help any of you facing the same.

我希望它会帮助你们中的任何人面临同样的问题。