'java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/platform/io/FileTestStorage;

Firstly I did not have androidTest Package in my App, So I created using Run-> Record Espresso Test, So it Created an androidTest package. But Getting this Exception in Logcat while running an Instrumented test. I have Gone through lot of solutions on Stackoverflow and Searched a lot on Google, Nothing works as of now.

E/AndroidRuntime: FATAL EXCEPTION: Instr: androidx.test.runner.AndroidJUnitRunner
        java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/test/platform/io/FileTestStorage;
            at androidx.test.runner.AndroidJUnitRunner.registerTestStorage(AndroidJUnitRunner.java:660)
            at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:432)
            at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
         Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.platform.io.FileTestStorage" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/data/app/net.one97.paytm.test-jZXGpy2zE0ToMIAtdjZZ_w==/base.apk", zip file "/data/app/net.one97.paytm-BEX-7xHHgkWWpGzITJCpTA==/base.apk"],nativeLibraryDirectories=[/data/app/net.one97.paytm.test-jZXGpy2zE0ToMIAtdjZZ_w==/lib/x86, /data/app/net.one97.paytm-BEX-7xHHgkWWpGzITJCpTA==/lib/x86, /data/app/net.one97.paytm.test-jZXGpy2zE0ToMIAtdjZZ_w==/base.apk!/lib/x86, /data/app/net.one97.paytm-BEX-7xHHgkWWpGzITJCpTA==/base.apk!/lib/x86, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
            at androidx.test.runner.AndroidJUnitRunner.registerTestStorage(AndroidJUnitRunner.java:660) 
            at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:432) 
            at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145) 

Here is my code.

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    @Test
    fun mainActivityTest() {
        assertEquals(2, 1+1)
    }
}

And build.gradle is

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.firebase-perf'
apply plugin: 'com.google.firebase.crashlytics'

android {
    compileSdkVersion 30
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "****"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "10.0.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true

    }

    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
        keepRuntimeAnnotatedClasses false
    }
       
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }

    testOptions {
        unitTests {
            includeAndroidResources true
            returnDefaultValues = true
        }
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

}

repositories {
    google()
    mavenCentral()
    maven { url "https://artifactory.paytm.in/libs-release-local"}
    maven { url 'https://maven.google.com' }
    maven {url "https://midgar.bintray.com/Pai-Network" }
}

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
        maven { url "https://artifactory.paytm.in/libs-release-local"}
        maven { url 'https://maven.google.com' }
        maven {url "https://midgar.bintray.com/Pai-Network" }
    }

}


dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "androidx.appcompat:appcompat:1.2.0"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation project(path: ':**')
    
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation project(path: ':chuck_library')
    implementation project(path: ':appmanager')
    implementation('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') {
        transitive = true
    }




    implementation 'androidx.core:core-ktx:1.4.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-alpha04'

    androidTestImplementation 'androidx.test:core:1.4.0'
    testImplementation 'androidx.test.ext:junit-ktx:1.1.3'
    testImplementation 'org.robolectric:robolectric:4.4'
    testImplementation 'androidx.test.ext:truth:1.4.0'
    testImplementation 'com.google.truth:truth:1.0'
    testImplementation 'org.mockito:mockito-core:3.3.3'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'com.google.truth:truth:1.0'

    testImplementation "androidx.test:monitor:1.4.0"
    testImplementation 'androidx.test:runner:1.4.0'

}

And On the test results Window. It Shows this enter image description here enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source