简体   繁体   中英

I add google sign in button but the app just close

the app doesn't work the app just crashes when I run it. I am doing log in page with google sign in. I tried to comment on some lines to know the problem . it crashed when I add this line particularly

mGoogleSignInClient = GoogleSignIn.getClient(this,gso)

this is my code

class LogInActivity : AppCompatActivity() {

lateinit var mAuth : FirebaseAuth

lateinit var mGoogleSignInClient: GoogleSignInClient
lateinit var gso: GoogleSignInOptions
val RC_SIGN_IN : Int =1

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_log_in)
    FirebaseApp.initializeApp(this)
    val SignInBT = findViewById<View>(R.id.Sign_BT) as SignInButton
    mAuth= FirebaseAuth.getInstance()
    gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken("724129725157-qu45prsbotdtsonbvolm3d2tn99sg84t.apps.googleusercontent.com")
        .requestEmail()
        .build()

    mGoogleSignInClient = GoogleSignIn.getClient(this,gso)

    SignInBT.setOnClickListener {

            view: View? ->  Signingoogle()

    }

    login_BT.setOnClickListener {
        AllowingUserToLogin()
    }

}

override fun onStart() {
    super.onStart()

    val currentUser = mAuth!!.getCurrentUser()

    if (currentUser != null){

        SendToRegister()

    }
}

private fun AllowingUserToLogin() {

    var email: String = email_ET.text.toString()
    var password : String = password_ET.text.toString()

    when {
        email.isEmpty() -> Toast.makeText(this,"ادخل الايميل", Toast.LENGTH_LONG).show()
        password.isEmpty() -> Toast.makeText(this,"ادخل كلمة المرور", Toast.LENGTH_LONG).show()
        else -> mAuth.signInWithEmailAndPassword(email,password)
            .addOnCompleteListener { task ->

                if (task.isSuccessful){

                    SendUserToMain()

                }else {

                    Toast.makeText(this, "الايميل وكلمة المرور غير متطابقة", Toast.LENGTH_LONG).show()

                }

            }
    }
}

private fun SendUserToMain() {
    var i = Intent(this,MainActivityC::class.java)
    startActivity(i)
}

private fun SendToRegister(){
    var i = Intent(this , RegesterActivity::class.java)
    startActivity(i)

}
private fun Signingoogle() {
    val sigInIntent: Intent = mGoogleSignInClient.signInIntent
    startActivityForResult(sigInIntent,RC_SIGN_IN)

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if(requestCode == RC_SIGN_IN){
        val task : Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
        handleResult(task)
    }
}

private fun handleResult(task: Task<GoogleSignInAccount>) {

    try {
        val account : GoogleSignInAccount =task.getResult(ApiException::class.java)
        updateUI(account)
    }catch (e: ApiException){
        Toast.makeText(this,e.toString(), Toast.LENGTH_LONG).show()
    }
}

private fun updateUI(account: GoogleSignInAccount){

    var name = account.displayName.toString()
    var email = account.email.toString()
    var password = "123456"
    mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener { task ->

        if(task.isSuccessful){
            SendToRegister()
        }else {
            Toast.makeText(this,"خطأ في التسجيل", Toast.LENGTH_LONG).show()}
    }

}

}

and this is XML file

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LogInActivity">

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorgreen"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="0dp"
        tools:ignore="MissingConstraints">
    <ImageView
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            app:srcCompat="@drawable/ic_stars_black_24dp"
            android:id="@+id/Logo" tools:ignore="VectorDrawableCompat"/>
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:background="@drawable/input_style"
            android:padding="10dp"
            android:layout_marginTop="50dp"
            android:textColor="@color/colorgreen"
            android:textColorHint="@color/colorgreen"
            android:hint="الايميل"
            android:textAlignment="textStart"
            android:id="@+id/email_ET"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:background="@drawable/input_style"
            android:padding="10dp"
            android:layout_marginTop="10dp"
            android:id="@+id/password_ET"
            android:textColor="@color/colorgreen"
            android:textAlignment="textEnd"
            android:hint="كلمة المرور"
            android:textColorHint="@color/colorgreen"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    <Button
            android:text="الدخول"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/login_BT"
            android:textColor="@color/colorwahit"
            android:background="@drawable/btn_style"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    <com.google.android.gms.common.SignInButton

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/Sign_BT"
            android:layout_marginTop="0dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp">

    </com.google.android.gms.common.SignInButton>

</LinearLayout>

and this is my gradle 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'
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-core:16.0.1'
implementation 'com.google.firebase:firebase-auth:16.0.1'
implementation 'com.google.firebase:firebase-database:16.0.1'
implementation 'com.google.firebase:firebase-storage:16.0.1'
implementation 'com.google.android.gms:play-services-drive:12.0.1'
implementation 'com.google.android.gms:play-services-auth:12.0.1'

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

and this is my logcat

2019-04-08 20:14:41.443 19035-19035/com.example.d_7.alleague E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.d_7.alleague, PID: 19035
java.lang.VerifyError: Verifier rejected class com.google.android.gms.auth.api.signin.GoogleSignInClient: int com.google.android.gms.auth.api.signin.GoogleSignInClient.zzach() failed to verify: int com.google.android.gms.auth.api.signin.GoogleSignInClient.zzach(): [0xF] 'this' argument 'Reference: com.google.android.gms.common.GoogleApiAvailability' not instance of 'Precise Reference: com.google.android.gms.common.zzf' (declaration of 'com.google.android.gms.auth.api.signin.GoogleSignInClient' appears in /data/app/com.example.d_7.alleague-bJU5791vnuL6iBwmq_inYA==/split_lib_dependencies_apk.apk)
    at com.google.android.gms.auth.api.signin.GoogleSignIn.getClient(Unknown Source:0)
    at com.example.d_7.alleague.LogInActivity.onCreate(LogInActivity.kt:38)
    at android.app.Activity.performCreate(Activity.java:7009)
    at android.app.Activity.performCreate(Activity.java:7000)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

the problem is with your this argument which you are passing to the GoogleSignIn.getClient() method. probably because you are missing the client.

try adding this com.google.android.gms:play-services-base:12.1.0 this dependency will provide the client. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM