我在尝试将Google登录集成到我的Android应用时遇到错误:无法解析符号GoogleSignIn

时间:2017-11-15 03:22:04

标签: android

我正在尝试将Google登录集成到我的Android中,但我收到一个错误,即无法解析符号GoogleSignIn:https://snag.gy/R2Mg3L.jpg

以下是项目的代码和Gradle文件;我按照Google提供的说明进行操作:https://developers.google.com/identity/sign-in/android/

package com.example.GSTest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.auth.api.signin.GoogleSignIn;

import com.google.android.gms.auth.api.signin.GoogleSignInOptions;

public class loginActivity extends AppCompatActivity implements View.OnClickListener{



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

    // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    // Build a GoogleSignInClient with the options specified by gso.
    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);


    Button gButton = findViewById(R.id.sign_in_button);
    gButton.setOnClickListener(this);
}

@Override
public void onClick(View v){

    switch (v.getId()) {
        case R.id.sign_in_button:
            signIn();
            break;
        // ...
    }

}

private void signIn() {
    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);
}


//end of main class
}

Gradle(项目):

// Top-level build file where you can add configuration options common to         
all sub-projects/modules.

buildscript {

repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0'
    classpath 'com.google.gms:google-services:3.1.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    google()
    jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

Gradle(模块):

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.example.GSTest"
    minSdkVersion 15
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.gms:google-services:3.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.1'
apply plugin: 'com.google.gms.google-services'
}

2 个答案:

答案 0 :(得分:2)

与所有导入问题一样,请检查您的依赖项。

您尝试使用至少需要11.6.0 release of Google Play

的代码
  

添加了GoogleSignInClient和GoogleSignIn课程。 GoogleSignInClient提供了与Google登录API互动的切入点。

更新Gradle文件中的版本

答案 1 :(得分:-1)

您需要导入包含GoogleSignIn的包。我假设您正在使用this one