java.lang.NoSuchMethodError:没有虚拟方法 setTokenProvider

Posted

技术标签:

【中文标题】java.lang.NoSuchMethodError:没有虚拟方法 setTokenProvider【英文标题】:java.lang.NoSuchMethodError: No virtual method setTokenProvider 【发布时间】:2019-09-29 16:22:07 【问题描述】:

每当我将com.google.firebase:firebase-firestore:19.0.0 添加到 Gradle 并运行应用程序时,都会出现以下错误。

E/androidRuntime: FATAL EXCEPTION: main
    Process: meter.meter, PID: 13588
    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/meter.meter-Qosf8nECYGfI4HI93CwJrw==/split_lib_dependencies_apk.apk!classes2.dex)
        at com.google.firebase.auth.zzp.create(Unknown Source:4)
        at com.google.firebase.components.ComponentRuntime.lambda$new$0(com.google.firebase:firebase-common@@17.0.0:66)
        at com.google.firebase.components.ComponentRuntime$$Lambda$1.get(Unknown Source:4)
        at com.google.firebase.components.Lazy.get(com.google.firebase:firebase-common@@17.0.0:53)
        at com.google.firebase.components.ComponentRuntime.initializeEagerComponents(com.google.firebase:firebase-common@@17.0.0:155)
        at com.google.firebase.FirebaseApp.initializeAllApis(com.google.firebase:firebase-common@@17.0.0:642)
        at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@17.0.0:358)
        at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@17.0.0:321)
        at com.google.firebase.FirebaseApp.initializeApp(com.google.firebase:firebase-common@@17.0.0:305)
        at com.google.firebase.provider.FirebaseInitProvider.onCreate(com.google.firebase:firebase-common@@17.0.0:53)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
        at com.google.firebase.provider.FirebaseInitProvider.attachInfo(com.google.firebase:firebase-common@@17.0.0:47)
        at android.app.ActivityThread.installProvider(ActivityThread.java:6391)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853)
        at android.app.ActivityThread.access$1100(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
        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)

Gradle:

dependencies 
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

    implementation 'com.firebaseui:firebase-ui-auth:4.3.1'


    implementation 'com.facebook.android:facebook-android-sdk:5.0.0'

    implementation 'com.twitter.sdk.android:twitter-core:3.3.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'

    implementation 'com.github.yalantis:ucrop:2.2.3-native'

    def camerax_version = "1.0.0-alpha01"
    implementation "androidx.camera:camera-core:$camerax_version"
    implementation "androidx.camera:camera-camera2:$camerax_version"

    implementation 'de.hdodenhof:circleimageview:3.0.0'
    implementation 'com.google.firebase:firebase-firestore:19.0.0'



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

【问题讨论】:

【参考方案1】:

由于您使用的是最新版本的 firestore,因此您需要将以下依赖项添加到您的 build.gradle

implementation 'com.google.firebase:firebase-auth:17.0.0'

根据docs:

身份验证版本 17.0.0

此 firebase-auth 更新需要使用 firebase-firestorefirebase-functionsfirebase-storagefirebase-database 的新版本。

此外,由于 firebase-ui-auth 使用版本 16.0.5 的 firebase 身份验证,然后添加您自己的 firebase-auth 实现,这将覆盖该版本。


所以你应该有以下dependencies

dependencies 
 implementation fileTree(dir: 'libs', include: ['*.jar'])
 implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
 implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
 testImplementation 'junit:junit:4.12'
 androidTestImplementation 'androidx.test:runner:1.1.0'
 androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

 implementation 'com.google.firebase:firebase-auth:17.0.0'
 implementation 'com.firebaseui:firebase-ui-auth:4.3.1'



 implementation 'com.facebook.android:facebook-android-sdk:5.0.0'

 implementation 'com.twitter.sdk.android:twitter-core:3.3.0'
 implementation 'androidx.recyclerview:recyclerview:1.0.0'

 implementation 'com.github.yalantis:ucrop:2.2.3-native'

 def camerax_version = "1.0.0-alpha01"
 implementation "androidx.camera:camera-core:$camerax_version"
 implementation "androidx.camera:camera-camera2:$camerax_version"

 implementation 'de.hdodenhof:circleimageview:3.0.0'
 implementation 'com.google.firebase:firebase-firestore:19.0.0'



【讨论】:

但我正在实施com.firebaseui:firebase-ui-auth:4.3.1 所以我应该删除它并做我自己的用户界面? 是的,但正如我所说,它使用版本16.0.5,您需要添加implementation 'com.google.firebase:firebase-auth:17.0.0' 来覆盖该版本,因为17.0.0 与最新版本的firestore 兼容 不保留firebaseui身份验证,只需添加implementation 'com.google.firebase:firebase-auth:17.0.0' .... 我在firebaseui的github仓库中添加了一个issue github.com/firebase/FirebaseUI-Android/issues/1633 @Roman 是的,但我认为他们更新了它,现在只需添加 firebase auth github.com/firebase/FirebaseUI-Android/releases/tag/5.0.0【参考方案2】:

我遇到了同样的错误

以下步骤对我有用:

    转到您的 build.gradle(:app) 删除该特定 Firebase 依赖项或所有 Firebase 依赖项。 同步 gradle 文件。 然后转到:Tools -> Firebase -> Cloud Firestore -> Get Started with Cloud Firestore -> Add Cloud Firestore to your App -> Click 'Add the Cloud Firestore SDK to your App' -> Accept all the changes. 同步 gradle 文件。

应该可以的。

【讨论】:

以上是关于java.lang.NoSuchMethodError:没有虚拟方法 setTokenProvider的主要内容,如果未能解决你的问题,请参考以下文章