Java.lang.NoSuchMethodError: Firebase Performance 和 Espresso Instrumented Tests 中没有名为 registerDefaultInstance 的静态方法。

24

当我添加implementation 'com.google.firebase:firebase-perf-ktx:19.1.0'时,我无法启动espresso有关仪器测试的测试(“app”方案构建正常)。 当我尝试启动Espresso测试时,我遇到

Test running failed: Process crashed.
java.lang.NoSuchMethodError: No static method registerDefaultInstance(Ljava/lang/Class;Lcom/google/protobuf/GeneratedMessageLite;)V in class Lcom/google/protobuf/GeneratedMessageLite

但是如果我删除implementation 'com.google.firebase:firebase-perf-ktx:19.1.0',一切都能正常工作。如何保留Firebase Performance库和Espresso仪表测试?


请发布完整的异常信息。另外,您是否正在运行Proguard? - dazza5000
6个回答

37

根据我的情况,似乎是由最近更新的 androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0' 依赖项中的 protobuf-lite:3.0.1 引起的,我能够通过这个排除来解决它。

androidTestImplementation (androidx.test.espresso:espresso-contrib:3.4.0'){
    exclude module: "protobuf-lite"
}

在KTX的情况下

testImplementation('androidx.test.espresso:espresso-contrib:3.4.0') {
        exclude(module = "protobuf-lite")
    }

3
有关此问题的更多信息,请访问 https://github.com/android/android-test/issues/999 - cosic
我仍然遇到这个问题。 - JCutting8
我一直以为Espresso 3.4.0及以上版本根本无法工作。结果发现这一直是问题所在。谢谢。 - Sean Blahovici

1

这是工作:

androidTestImplementation("androidx.test.espresso:espresso-contrib:3.4.0") 
{
    exclude(group: "com.google.protobuf", module: "protobuf-lite")
}

但是这个也会排除“androidx.test.espresso:espresso-contrib:3.4.0”中的“protobuf-lite”,对吧?我的意思是Espresso会尝试从应用程序级别使用protobuf-lite,否则就会崩溃。 - Vladimir Fisher

1
最近我在运行React Native的Android Detox测试时遇到了一个非常相似的错误。将protobuf-lite模块排除在外解决了这个问题。

Changing: androidTestImplementation('com.wix:detox:+')

到: androidTestImplementation('com.wix:detox:+') { exclude module: "protobuf-lite" }android/app/build.gradle文件中解决了我的问题。

1

这是工作:

androidTestImplementation('androidx.test.espresso:espresso-contrib:3.4.0') {
    exclude module: "protobuf-lite"
}
androidTestImplementation ('androidx.test.espresso:espresso-accessibility:3.4.0') {
    exclude module: "protobuf-lite"
}

0
正如所指出的那样,这是protobuf-lite运行时,它随着androidx.test.espresso:espresso-contrib依赖项一起提供。
在检查依赖项时,似乎只有在以调试模式运行Instrumentation测试(debugAndroidTestRuntimeClasspath)时才会使用此依赖项。
 ./gradlew app:dependencies

在应用程序的 build.gradle 文件中添加以下行,将会在 debugAndroidTestRuntimeClasspath 配置中排除它,这样你的测试就可以正常运行了 :)
configurations
{
    debugAndroidTestRuntimeClasspath.exclude group: 'com.google.protobuf' module: 'protobuf-lite'
}

-1

@ВладимирФишер,你找到解决方案了吗? - Nikola-Milovic
是的,Nicola。我已经为该项目启用了MultiDex。 - Vladimir Fisher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接