我正在学习如何开发具有Google登录选项的应用,并且我正在遵循此tutorial。但是,我的活动不是在导入时将import com.google.android.gms.auth.api.Auth
和颜色Auth
注册为红色。
package nus.is3261.kotlinapp
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_google_signin.*
import com.google.android.gms.auth.api.Auth
import com.google.android.gms.common.api.GoogleApiClient
import com.google.firebase.auth.FirebaseAuth
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.ConnectionResult
import com.google.firebase.auth.FirebaseUser
import android.content.Intent
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.firebase.auth.GoogleAuthProvider
class GoogleSigninActivity : AppCompatActivity(), View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {
private val TAG = "JSAGoogleSignIn"
private val REQUEST_CODE_SIGN_IN = 1234
private val WEB_CLIENT_ID = "xxxxxx.apps.googleusercontent.com"
private var mAuth: FirebaseAuth? = null
private var mGoogleApiClient: GoogleApiClient? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_google_signin)
btn_sign_in.setOnClickListener(this)
btn_sign_out.setOnClickListener(this)
btn_disconnect.setOnClickListener(this)
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(WEB_CLIENT_ID)
.requestEmail()
.build()
mGoogleApiClient = GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build()
mAuth = FirebaseAuth.getInstance()
}
override fun onStart() {
super.onStart()
// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = mAuth!!.currentUser
updateUI(currentUser)
}
override fun onClick(v: View?) {
val i = v!!.id
when (i) {
R.id.btn_sign_in -> signIn()
R.id.btn_sign_out -> signOut()
R.id.btn_disconnect -> revokeAccess()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent();
if (requestCode == REQUEST_CODE_SIGN_IN) {
val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
if (result.isSuccess) {
// successful -> authenticate with Firebase
val account = result.signInAccount
firebaseAuthWithGoogle(account!!)
} else {
// failed -> update UI
updateUI(null)
Toast.makeText(applicationContext, "SignIn: failed!",
Toast.LENGTH_SHORT).show()
}
}
}
private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
Log.e(TAG, "firebaseAuthWithGoogle():" + acct.id!!)
val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
mAuth!!.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success
Log.e(TAG, "signInWithCredential: Success!")
val user = mAuth!!.currentUser
updateUI(user)
} else {
// Sign in fails
Log.w(TAG, "signInWithCredential: Failed!", task.exception)
Toast.makeText(applicationContext, "Authentication failed!",
Toast.LENGTH_SHORT).show()
updateUI(null)
}
}
}
override fun onConnectionFailed(connectionResult: ConnectionResult) {
Log.e(TAG, "onConnectionFailed():" + connectionResult);
Toast.makeText(applicationContext, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
private fun signIn() {
val intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
startActivityForResult(intent, REQUEST_CODE_SIGN_IN)
}
private fun signOut() {
// sign out Firebase
mAuth!!.signOut()
// sign out Google
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback { updateUI(null) }
}
private fun revokeAccess() {
// sign out Firebase
mAuth!!.signOut()
// revoke access Google
Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback { updateUI(null) }
}
private fun updateUI(user: FirebaseUser?) {
if (user != null) {
tvStatus.text = "Google User email: " + user.email!!
tvDetail.text = "Firebase User ID: " + user.uid
btn_sign_in.visibility = View.GONE
layout_sign_out_and_disconnect.visibility = View.VISIBLE
} else {
tvStatus.text = "Signed Out"
tvDetail.text = null
btn_sign_in.visibility = View.VISIBLE
layout_sign_out_and_disconnect.visibility = View.GONE
}
}
}
我觉得这是一个依赖关系问题,但是我不确定问题出在哪里。
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "nus.is3261.kotlinapp"
minSdkVersion 21
targetSdkVersion 28
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 "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:design:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.google.firebase:firebase-core:16.0.4'
}
这是我的gradle文件
buildscript {
ext.kotlin_version = '1.2.70'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.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
}
答案 0 :(得分:1)
在依赖项内添加:
implementation 'com.google.android.gms:play-services-auth:16.0.1'
,然后在app / build.gradle文件的底部,添加(firebase必需):
apply plugin: 'com.google.gms.google-services'