android-test: Running a test with ActivityScenario while test coverage is enabled throws a NoClassDefFoundError

Steps to Reproduce

Follow the steps here to get Jacoco coverage for tests

https://about.codecov.io/blog/code-coverage-for-android-development-using-kotlin-jacoco-github-actions-and-codecov/

Try to run this test

@RunWith(AndroidJUnit4::class)
class StringAdapterTest {

    @Test
    fun testStringAdapter() {
        ActivityScenario.launch(
            TestActivity::class.java
        ).onActivity { activity ->

            }

        }

}

Expected Results

No exceptions.

Actual Results

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/jacoco/agent/rt/internal_3570298/Offline;
    at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.$jacocoInit(Unknown Source:13)
    at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.<clinit>(Unknown Source:0)
    at java.lang.Class.newInstance(Native Method)
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3725)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8653)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "org.jacoco.agent.rt.internal_3570298.Offline" on path: DexPathList[[zip file "/data/app/~~h8MxzSzcV5J1qu2yJeU5Dg==/com.example.inventory_manager.test-OTl1KcxOXWG6CFw7svj1Pg==/base.apk"],nativeLibraryDirectories=[/data/app/~~h8MxzSzcV5J1qu2yJeU5Dg==/com.example.inventory_manager.test-OTl1KcxOXWG6CFw7svj1Pg==/lib/arm64, /system/lib64, /system/system_ext/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.$jacocoInit(Unknown Source:13) 
    at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.<clinit>(Unknown Source:0) 
    at java.lang.Class.newInstance(Native Method) 
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) 
    at android.app.Instrumentation.newActivity(Instrumentation.java:1253) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3725) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:246) 
    at android.app.ActivityThread.main(ActivityThread.java:8653) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 

AndroidX Test and Android OS Versions

AndroidX Test version 1.4.0 and Android OS Version 11

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 23 (1 by maintainers)

Most upvoted comments

Wow, so I needed debugImplementation 'androidx.test:core-ktx:1.4.0'. androidTestImplementation 'androidx.test:core-ktx:1.4.0' was not enough.