Android Instrumentation测试中的ClassNotFoundException

时间:2018-12-12 13:20:09

标签: android unit-testing android-espresso android-instrumentation

我正在尝试为我的应用程序编写检测测试,但出现此错误。我已经看了互联网上几乎所有的答案,但没有一个对我有用。

我已附上我的gradle文件并在此处进行测试以供参考

java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{com.vuclip.viu.test/android.support.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
FATAL EXCEPTION: main
Process: com.company.myapp, PID: 1228
java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{com.vuclip.viu.test/android.support.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5746)
    at android.app.ActivityThread.-wrap1(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1664)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6600)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:772)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.ActivityThread.instantiate(ActivityThread.java:6497)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5743)

build.gradle文件

apply plugin: 'com.android.feature'
apply plugin: 'kotlin-android'
apply plugin: 'io.fabric'
apply from: '../jacoco-core.gradle'
apply plugin: 'kotlin-kapt'


allprojects {
    repositories {
        jcenter()
    }
}

repositories {
    flatDir {
        dirs 'libs'
    }
    maven { url 'https://maven.fabric.io/public' }
    maven {
        url 'https://maven.google.com'
    }
    mavenCentral()
}

android {
    compileSdkVersion buildVersion.targetSdk
    buildToolsVersion buildVersion.buildTools

    baseFeature true
//    testBuildType "release"
    defaultConfig {
        minSdkVersion buildVersion.minSdk
        //do not increment this version, <21 fixes white notif icon issue on lollypop

        targetSdkVersion buildVersion.targetSdk
        versionCode 2
        versionName "1.0.02"
        multiDexEnabled = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        useLibrary 'org.apache.http.legacy'
        vectorDrawables.useSupportLibrary = true
    }

    /*aaptOptios {
        additionalParameters '--utf16'
    }*/

    testOptions {
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true
            }
        }
        unitTests {
            includeAndroidResources = true
        }
    }


    dexOptions {
        jumboMode = true
        javaMaxHeapSize "8g"
    }

    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }


    testOptions {
        unitTests.all {
            jvmArgs '-noverify'
        }
    }

}


dependencies {
    //other dependencies

    androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.google.android.gms', module: 'play-services-gcm'
    }
    androidTestImplementation 'com.android.support.test.espresso:espresso-intents:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.google.android.gms', module: 'play-services-gcm'
    }
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'design'
        exclude module: 'recyclerview-v7'
    }
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.10.0'
}

任何人都可以分享一些灵感吗?

0 个答案:

没有答案