badvok
badvok

Reputation: 561

Jetpack compose, Instrumental tests: java.lang.ClassNotFoundException: Didn't find class "androidx.test.internal.util.Checks"

When running an androidTest(instrumentation) before the test is able to run I get a crash as the app launches. I have provided the crash log. The app gradle and project gradle file, finally the very basic test that i was about to get started on.

The full error log:

2021-08-16 10:44:44.368 20858-20858/com.matrix.install.test D/AndroidRuntime: Shutting down VM
2021-08-16 10:44:44.372 20858-20858/com.matrix.install.test E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.matrix.install.test, PID: 20858
    java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/internal/util/Checks;
        at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.onResume(InstrumentationActivityInvoker.java:159)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355)
        at android.app.Activity.performResume(Activity.java:7117)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3556)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.internal.util.Checks" on path: DexPathList[[zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.matrix.install.test-KWEjjLDHWFQ3kQvgJfaPgg==/base.apk"],nativeLibraryDirectories=[/data/app/com.matrix.install.test-KWEjjLDHWFQ3kQvgJfaPgg==/lib/arm64, /system/lib64, /vendor/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity.onResume(InstrumentationActivityInvoker.java:159) 
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355) 
        at android.app.Activity.performResume(Activity.java:7117) 
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3556) 
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

App Gradle:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'com.google.firebase.crashlytics'
    id 'kotlin-parcelize'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.matrix.install"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 6
        versionName "1.0.6"

        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
        Properties properties = new Properties()
        if (rootProject.file("local.properties").exists()) {
            properties.load(rootProject.file("local.properties").newDataInputStream())
        }
        // Inject the Maps API key into the manifest
        manifestPlaceholders = [mapsApiKey: properties.getProperty("MAPS_API_KEY", "")]
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            manifestPlaceholders = [enableCrashReporting: "true"]
        }
        debug {
            manifestPlaceholders = [enableCrashReporting: "false"]
        }
    }
    buildFeatures {
        compose true
        viewBinding = true
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.0.1'
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    packagingOptions {
        exclude "META-INF/AL2.0"
        exclude "META-INF/LGPL2.1"
    }
}

dependencies {

    def multidex_version = "2.0.1"
    implementation "androidx.multidex:multidex:$multidex_version"
    //Google libs
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.21"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'

    def compose_version = "1.0.1"
    //Compose libs
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation "androidx.compose.foundation:foundation:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.material:material-icons-core:$compose_version"
    implementation "androidx.compose.material:material-icons-extended:$compose_version"

    implementation "androidx.activity:activity-compose:1.3.1"

    implementation "com.google.android.libraries.maps:maps:3.1.0-beta"
    implementation "com.google.maps.android:maps-v3-ktx:2.2.0"

    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07"
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
    implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-beta02"

    //play services
//    implementation "com.google.android.gms:play-services-maps:17.0.0"
    implementation "com.google.android.gms:play-services-location:18.0.0"

    //Firebase
    implementation platform('com.google.firebase:firebase-bom:26.8.0')
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-analytics-ktx'


    //Dagger //2.28-alpha
    implementation "com.google.dagger:hilt-android:2.36"
    kapt "com.google.dagger:hilt-android-compiler:2.36"
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0-alpha03'
    implementation 'androidx.hilt:hilt-work:1.0.0'
    // When using Kotlin.
    kapt 'androidx.hilt:hilt-compiler:1.0.0'

    //DB viewer
    def stetho_version = "1.5.1"
    implementation "com.facebook.stetho:stetho-okhttp3:$stetho_version"

    //Coroutines
    def coroutines_version = "1.5.1"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$coroutines_version"

    //Life Cycle
    def life_cycle_version = "2.2.0"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$life_cycle_version"

    //DB
    def room_version = "2.3.0"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-rxjava2:$room_version"

    //Request libraries
    def retrofit_version = "2.6.0"
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"

    //Json parser
    def moshi_version = "1.9.2"
    implementation "com.squareup.moshi:moshi-adapters:$moshi_version"
    implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"

    //Modal
    def kotlin_result_version = "1.1.7"
    implementation("com.michael-bull.kotlin-result:kotlin-result:$kotlin_result_version")

    //Time
    def joda_version = "2.9.9"
    implementation "joda-time:joda-time:$joda_version"

    //Accompanist
    implementation "com.google.accompanist:accompanist-coil:0.11.1"
    implementation 'com.google.accompanist:accompanist-insets-ui:0.15.0'
    implementation 'com.google.accompanist:accompanist-insets:0.15.0'
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

    //Swipe refresh
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

    //Video player
    implementation 'com.google.android.exoplayer:exoplayer:2.11.7'
    implementation 'com.google.android.exoplayer:exoplayer-hls:2.11.7'

    // barcode scanner
    implementation "com.google.zxing:core:3.3.0"
    implementation "com.journeyapps:zxing-android-embedded:3.4.0@aar"

    def work_version = "2.5.0"
    //Work manager
    implementation "androidx.work:work-runtime-ktx:$work_version"

    // Testing dependencies
    implementation 'androidx.test:runner:1.4.0'

    androidTestImplementation "androidx.arch.core:core-testing:2.1.0"

    // Compose testing dependencies
    androidTestImplementation "androidx.compose.ui:ui-test:$compose_version"
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"

    androidTestImplementation "androidx.test.espresso:espresso-contrib:3.3.0"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"

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

Project Gradle:

buildscript {
    ext.kotlin_version = "1.5.21"
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
        //Dependency injection
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.36"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.0'
    }
}


allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

finally the test class, very little here as this crash is a blocker:

@ExperimentalTestApi
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
class RegisterDeviceFragmentKtTest {

    @get:Rule
    val composeTestRule = createComposeRule()
    

    @Test
    fun registerDeviceViewTest() {
        val mockedViewState = RegisterDeviceViewModel.RegisterDeviceViewState("123abc")
        composeTestRule.setContent {
            RegisterDeviceView(
                modifier = Modifier,
                viewState = mockedViewState,
                imeiChanged = {},
                navigateToScanner = {
                    assertTrue(true)
                },
                registerDevice = {}
            )
        }
        composeTestRule.onNode(hasText("Scan")).performClick()
        Thread.sleep(5000)
    }

}

Upvotes: 4

Views: 2359

Answers (3)

Gk Mohammad Emon
Gk Mohammad Emon

Reputation: 6938

For my case I need to test a recyclerview , but accidentally I put their dependencies in gradle like -

implementation 'androidx.test.espresso:espresso-contrib:3.4.0'
implementation 'androidx.test.espresso:espresso-core:3.4.0'

But it should be

androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

Upvotes: 3

heyheyhey
heyheyhey

Reputation: 1626

Make sure your UI test related libraries are imported with "androidTestImplementation", not "implementation"

eg.:

 androidTestImplementation 'androidx.test:runner:1.4.0'

and not

 implementation 'androidx.test:runner:1.4.0'

Upvotes: 5

USMAN osman
USMAN osman

Reputation: 1030

Compose test runs on Junit4 so you need Junit4 Runner for that.
Annotate your test class with AndroidJUnit4 like this:

@RunWith(AndroidJUnit4::class)
class RegisterDeviceFragmentKtTest  {}

Upvotes: 0

Related Questions