android / android-test

An extensive framework for testing Android apps
https://android.github.io/android-test
Apache License 2.0
1.16k stars 314 forks source link

Espresso-contrib 3.4 - protobuf-lite:3.0.1 dependency causing exception on startup when used with Firebase libraries #999

Open AlmightyCZ opened 3 years ago

AlmightyCZ commented 3 years ago

Description

After update from 'androidx.test.espresso:espresso-contrib:3.3.0' to 'androidx.test.espresso:espresso-contrib:3.4.0' all my instrumentation tests fail.

Steps to Reproduce

use androidTestImplementation ('androidx.test.espresso:espresso-contrib:3.4.0') and Firebase Analytics, Performace, Crashlytics

Run Espresso test.

Expected Results

No exception, working tests.

Actual Results

Exception on startup: java.lang.NoSuchMethodError: No static method registerDefaultInstance(Ljava/lang/Class;Lcom/google/protobuf/GeneratedMessageLite;)V in class Lcom/google/protobuf/GeneratedMessageLite; or its super classes (declaration of 'com.google.protobuf.GeneratedMessageLite' appears in /data/app/~~STQIHCcHI7MbyT9nY0yblg==/cz.aldor.espresso34.test-QbLfUoHzQe8ZCBAb4Mzrlg==/base.apk)

works with this exclude

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

AndroidX Test and Android OS Versions

Tested on API 30 device and API 29 AVD.

androidTestImplementation 'androidx.test:core-ktx:1.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation ('androidx.test.espresso:espresso-contrib:3.4.0')

implementation 'com.google.firebase:firebase-analytics-ktx:19.0.0'
implementation 'com.google.firebase:firebase-crashlytics-ktx:18.1.0'
implementation 'com.google.firebase:firebase-perf-ktx:20.0.1'

Link to a public git repo demonstrating the problem:

https://github.com/AlmightyCZ/Espresso_3_4_protobuf_bug

xyz-fly commented 3 years ago

I have the same question:

java.lang.NoSuchMethodError: No static method registerDefaultInstance(Ljava/lang/Class;Lcom/google/protobuf/GeneratedMessageLite;)V in class Lcom/google/protobuf/GeneratedMessageLite; or its super classes (declaration of 'com.google.protobuf.GeneratedMessageLite' appears in /data/app/com.xxx.myapplication.test-C9aDSAone8CVcJaHfm4n5Q==/base.apk)
    at com.xxx.myapplication.data.payment.User.<clinit>(User.java:280)
    at com.xxx.myapplication.data.payment.User.getDefaultInstance(User.java:285)
    at com.xxx.myapplication.UserGoodSerializer.<clinit>(Data.kt:24)
    at com.xxx.myapplication.DataKt.<clinit>(Data.kt:20)
    at com.xxx.myapplication.DataKt.getUser(Unknown Source:0)
    at com.xxx.myapplication.ExampleInstrumentedTest$useAppContext$1.invokeSuspend(ExampleInstrumentedTest.kt:27)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
    at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
    at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
    at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source:1)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
    at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source:1)
    at com.xxx.myapplication.ExampleInstrumentedTest.useAppContext(ExampleInstrumentedTest.kt:23)
gildor commented 3 years ago

Have the same issue. Looks like it conflicts with protobuf-javalite (used for example by some Firebase libraries like InAppMessages)

Solved it by excluding this dependency from all configurations (probably can be excluded in case of espresso only from androidTestImplementation)

configurations.configureEach {
            exclude group: "com.google.protobuf", module: "protobuf-lite"
}
himamis commented 2 years ago

I think it's through this the dependency com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:3.1 which pulls in com.google.protobuf:protobuf-lite:3.0.1 see https://github.com/google/Accessibility-Test-Framework-for-Android/blob/master/build.gradle

lkesteloot commented 2 years ago

I have a similar problem because I use protobuf 2.6.1 in my app. When compiling my test app, version 3.0.1 wins and I get this error:

com.headcode.ourgroceries.protocol.FGProtocol$List.getParserForType() overrides final method in class Lcom/google/protobuf/GeneratedMessageLite

The getParserForType() method must be overridden in 2.6.1 but must not be overridden in 3.0.1.

The exclusion from @gildor's comment worked; I put it at the top level of the app's build file, at the very end.

srinivaasang1 commented 2 years ago

I have the same issue excluding "protobuf-java" or "protobuf-lite" from espresso contrib is not working am still getting the error and the app crashes?. Anyone know how to resolve this

jenmo917 commented 1 year ago

+1

ryancfogarty commented 1 year ago

I'm also experiencing this issue with espresso 3.5.1 and firebase perf plugin 1.4.2 (firebase bom 32.4.1). Excluding protobuf / profobuf-lite does not fix it for me.

UPDATE: I was able to fix my issue by excluding protobuf-lite from all dependencies

configurations {
    androidTestImplementation.exclude module: 'protobuf-lite'
}
brettchabot commented 1 year ago

I'd recommend trying

androidTestImplementation ('androidx.test.espresso:espresso-contrib:3.4.0'){ exclude group: '[com.google.android.apps.common.testing.accessibility.framework', module: 'accessibility-test-framework]' }

As pointed out earlier, the protobuf dependency is brought in transitively via com.google.android.apps.common.testing.accessibility.framework. This dependency is only needed for the deprecated class androidx.test.espresso.contrib.AccessbilityChecks, which was moved to its own artifact a while ago.

In the future we'll just remove androidx.test.espresso.contrib.AccessbilityChecks and the com.google.android.apps.common.testing.accessibility.framework dependency, but that will require a major version bump - hence the delay.