发生AndroidX崩溃的Google Auth

时间:2018-12-29 09:55:31

标签: android authentication google-signin androidx

我正在androidX迁移项目上使用google auth。参见[StackOverflow链接] [1]。我收到以下异常:

    2018-12-29 15:02:16.127 8904-8904/com.sumit.movieticketbookingapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sumit.movieticketbookingapp, PID: 8904
    java.lang.IncompatibleClassChangeError: Class 'com.google.android.gms.auth.api.signin.internal.SignInHubActivity' does not implement interface 'androidx.lifecycle.LifecycleOwner' in call to 'androidx.lifecycle.Lifecycle androidx.lifecycle.LifecycleOwner.getLifecycle()' (declaration of 'androidx.lifecycle.LiveData' appears in /data/app/com.sumit.movieticketbookingapp-_SKmr46VFm1sF3eifoZ-kQ==/split_lib_dependencies_apk.apk)
        at androidx.lifecycle.LiveData.observe(LiveData.java:172)
        at androidx.loader.app.LoaderManagerImpl$LoaderInfo.setCallback(LoaderManagerImpl.java:100)
        at androidx.loader.app.LoaderManagerImpl.createAndInstallLoader(LoaderManagerImpl.java:400)
        at androidx.loader.app.LoaderManagerImpl.initLoader(LoaderManagerImpl.java:421)
        at com.google.android.gms.auth.api.signin.internal.SignInHubActivity.zzn(Unknown Source:80)
        at com.google.android.gms.auth.api.signin.internal.SignInHubActivity.onActivityResult(Unknown Source:68)
        at android.app.Activity.dispatchActivityResult(Activity.java:7454)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2018-12-29 15:02:16.128 8904-8904/com.sumit.movieticketbookingapp E/UncaughtException: java.lang.IncompatibleClassChangeError: Class 'com.google.android.gms.auth.api.signin.internal.SignInHubActivity' does not implement interface 'androidx.lifecycle.LifecycleOwner' in call to 'androidx.lifecycle.Lifecycle androidx.lifecycle.LifecycleOwner.getLifecycle()' (declaration of 'androidx.lifecycle.LiveData' appears in /data/app/com.sumit.movieticketbookingapp-_SKmr46VFm1sF3eifoZ-kQ==/split_lib_dependencies_apk.apk)
        at androidx.lifecycle.LiveData.observe(LiveData.java:172)
        at androidx.loader.app.LoaderManagerImpl$LoaderInfo.setCallback(LoaderManagerImpl.java:100)
        at androidx.loader.app.LoaderManagerImpl.createAndInstallLoader(LoaderManagerImpl.java:400)
        at androidx.loader.app.LoaderManagerImpl.initLoader(LoaderManagerImpl.java:421)
        at com.google.android.gms.auth.api.signin.internal.SignInHubActivity.zzn(Unknown Source:80)
        at com.google.android.gms.auth.api.signin.internal.SignInHubActivity.onActivityResult(Unknown Source:68)
        at android.app.Activity.dispatchActivityResult(Activity.java:7454)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2

我正在使用以下依赖项:

    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.vectordrawable:vectordrawable:1.1.0-alpha01'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'com.google.firebase:firebase-crash:16.2.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    compileSdkVersion 28
    minSdkVersion 15
    targetSdkVersion 28

我的LoginFragment:

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.sumit.movieticketbookingapp.framework.Constants;
import com.sumit.movieticketbookingapp.framework.OnFragmentInteractionListener;
import com.sumit.movieticketbookingapp.R;
public class FragmentLogin extends Fragment implements LifecycleOwner {

    private static final String ARG_PARAM1 = "param1";
    GoogleSignInOptions googleSignInOptions;
    GoogleSignInClient googleSignInClient;
    private static final int RC_SIGN_IN = 9001;
    private static final String TAG = "FragmentLogin";
    private String mParam1;

    public FragmentLogin() {
        // Required empty public constructor
    }

    public static FragmentLogin newInstance(String param1) {
        FragmentLogin fragment = new FragmentLogin();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_login, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        googleSignInOptions= new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestProfile()
                .build();
        googleSignInClient= GoogleSignIn.getClient(view.getContext(), googleSignInOptions);
        GoogleSignInAccount googleSignInAccount= GoogleSignIn.getLastSignedInAccount(view.getContext());
        SignInButton signInButton = view.findViewById(R.id.sign_in_button);
        signInButton.setSize(SignInButton.SIZE_STANDARD);
        signInButton.setColorScheme(SignInButton.COLOR_LIGHT);
        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    @Override
    public void onStart() {
        super.onStart();
        GoogleSignInAccount googleSignInAccount= GoogleSignIn.getLastSignedInAccount(getContext());
        updateUI(googleSignInAccount);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }
    private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);
            updateUI(account);
        } catch (ApiException e) {
            Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
            updateUI(null);
        }
    }

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

    private void signOut() {
        googleSignInClient.signOut()
                .addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        updateUI(null);
                    }
                });
    }

    private void revokeAccess() {
        googleSignInClient.revokeAccess()
                .addOnCompleteListener(getActivity(), new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        updateUI(null);
                    }
                });
    }

    private void updateUI(@Nullable GoogleSignInAccount account) {
        if (account != null) {
            SharedPreferences sharedPreferences=getContext().getSharedPreferences(Constants.LOGIN_PREF,Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putBoolean(Constants.LOGIN,true);
            editor.putString(Constants.EMAIL,""+account.getEmail());
            editor.putString(Constants.NAME,""+account.getDisplayName());
            editor.putString(Constants.PROFILE_PIC,""+account.getPhotoUrl());
            editor.apply();
        } else {
            SharedPreferences sharedPreferences=getContext().getSharedPreferences("login_pref",Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.clear();
            editor.apply();
        }
    }
}

出现用户帐户选择屏幕。选择帐户后,应用程序崩溃。在FragmentLogin.java中,SharedPreferences被保存在updateUI()函数中。之后,使用OnFragmentInteractionListener膨胀下一个片段会崩溃。根据堆栈跟踪,问题似乎出在SignInHubActivity中而不实现侦听器。请解决此问题。

0 个答案:

没有答案