在 Android Studio 中导入 Google Play 服务库 [英] Import Google Play Services library in Android Studio

查看:54
本文介绍了在 Android Studio 中导入 Google Play 服务库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

项目无法解析GooglePlayServicesUtil,当我手动输入导入时(如下图),出现Cannot resolve symbol 'common'.

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

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

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

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

Google Play 服务设置 看来我只有将 com.google.android.gms:play-services:4.+ 依赖项添加到我的 build.gradle 文件(并使用 gradle 重新同步项目文件)以使SDK 可用于我的项目.我确实得到了一个爆炸包"在 ProjectName/module/build/exploded-bundles 中,但这似乎没有效果.

From the Google Play Services Setup it looks like I just have to add the com.google.android.gms:play-services:4.+ dependency to my build.gradle file (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.

我已经从 SDK 管理器安装了 Google Play 服务、Android Support Repository 和 Google Repository.我也多次删除并重新安装它们:)

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 :)

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

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

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

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.

我项目的外部库显示:

  • <1.7 >
  • joda-time-2.3
  • support-v4-13.0.0

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

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

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'
}

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

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

<?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>

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

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

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());
    }
}

推荐答案

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

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

打开<代码>文件>项目结构并检查以下所有内容.如果显示错误,请单击标记的红色灯泡,然后单击添加到依赖项".

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

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

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

这篇关于在 Android Studio 中导入 Google Play 服务库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆