FirebaseAuth.getInstance() 抛出异常 java.lang.NoSuchMethodError

时间:2021-03-24 18:02:08

标签: java android authentication firebase-authentication google-signin

我正在使用 firebase 实现 google 登录,当我获取 firebaseauth 类的实例时,它会引发异常,

<块引用>

java.lang.NoSuchMethodError

但我的代码是从文档中复制的(https://firebase.google.com/docs/auth/android/google-signin)。请帮忙。

代码

    public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity_Firebase";
    private static final int RC_SIGN_IN = 9001;
    private FirebaseAuth mAuth;
    private GoogleSignInClient mGoogleSignInClient;

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

        mAuth = FirebaseAuth.getInstance(); //this line is shown as error

//finding button and setting click listener
        findViewById(R.id.signInButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
signIn();
            }
        });
    }

    void setupGoogleSignIn(){
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    }
    private void signIn() {
        setupGoogleSignIn();

        Intent signInIntent = mGoogleSignInClient.getSignInIntent(); //getting signIn intent from googleclient
        startActivityForResult(signInIntent, RC_SIGN_IN);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                Log.d(TAG, "firebaseAuthWithGoogle: " + account.getIdToken());
                firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w(TAG, "Google sign in failed", e);
            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {
        Log.d(TAG, "firebaseAuthWithGoogle: called");
        AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);

        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();

                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                        }
                    }
                });
    }
}

完整的 Logcat

    Process: com.recipeapp.marathi, PID: 13369
java.lang.NoSuchMethodError: No virtual method setTokenProvider(Lcom/google/firebase/internal/InternalTokenProvider;)V in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.recipeapp.marathi-3W2YRoeaSMJdnEELQ9KFbw==/base.apk)
    at com.google.firebase.auth.FirebaseAuth.zza(Unknown Source:22)
    at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:4)
    at com.recipeapp.marathi.activities.LoginActivity.onCreate(LoginActivity.java:37)
    at android.app.Activity.performCreate(Activity.java:7964)
    at android.app.Activity.performCreate(Activity.java:7953)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3472)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3636)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2222)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:228)
    at android.app.ActivityThread.main(ActivityThread.java:7782)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)

依赖项:

    //auth firebase
implementation platform('com.google.firebase:firebase-bom:26.7.0')
implementation "com.google.firebase:firebase-database:$firebase_google_version"
implementation "com.google.firebase:firebase-auth:$firebase_google_version"
implementation "com.google.android.gms:play-services-auth:$firebase_google_version"
implementation "com.google.firebase:firebase-ads:$firebase_google_version"
implementation 'com.firebaseui:firebase-ui-database:7.1.1'

请帮我解决这个问题。

0 个答案:

没有答案