-->

Google Sign-in button error in Android

2019-08-15 16:24发布

问题:

I try to include Google sign-in button in my android app by following steps:

1) Including the button:

<com.google.android.gms.common.SignInButton
    android:id="@+id/sign_in_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

2) Editing Top-level build file

classpath 'com.google.gms:google-services:1.5.0-beta2'

3) Editing App-level build file

apply plugin: 'com.google.gms.google-services'

Now I am getting error in button UI as

Blockquote The following classes could not be found: - com.google.android.gms.common.SignInButton (Fix Build Path, Create Class)

Please help me solve this issue!

回答1:

First of all we need to clarify that you should have 2 build.gradle files. One of them is a project-level file and the other one is an app-level Gradle file:

These files should look like this:

Project: StackOverlfow build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
        classpath 'com.google.gms:google-services:1.5.0-beta2'

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

allprojects {
    repositories {
        jcenter()
    }
}

Module: app gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.package"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services-auth:8.4.0'
}

If you don't have Google Play Services installed, please do the following:

  1. Go to Android Studio -> Preferences -> Appearance & Behaviour -> System Settings -> Android SDK.
  2. You'll see on the bottom part a "Launch Standalone SDK Manager".
  3. Click that and you'll have the standalone SDK Manager window opened.
  4. Scroll all the way down to "Extras" pick "Google Play Services" and hit update.

This will install the proper play services packages. Once done, go back to the Android Studio and re-sync your Gradle file.



回答2:

Take a look at my question over here - Android - Google Login and Play Services version incomptablity

You need:

classpath 'com.android.tools.build:gradle:2.0.0-alpha5'
classpath 'com.google.gms:google-services:2.0.0-alpha5'

and

compile 'com.google.android.gms:play-services-auth:8.4.0'

Also, Google's sample here was helpful to me https://github.com/googlesamples/google-services/blob/master/android/appinvites/app/build.gradle