What do I need to import to use launchActivity<>() in UnitTests?

In Kotlin it seems that it needs to be like this for some reason:

val activityScenario = launch(MainActivity::class.java)

The import statement for this is:

import androidx.test.core.app.ActivityScenario.launch

Or maybe it's not the same thing? Don't know, but it works.


You need to import following dependency into your gradle.

androidTestImplementation 'androidx.test:core-ktx:1.1.0'

Moreover, also add this in gradle file to avoid compile time error after adding launchActivity method in your test code.

kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }