Android Studio运行测试:java.lang.NoClassDefFoundError:android / util / Log

时间:2018-08-29 20:01:14

标签: java android junit4

我在下面运行JUnit测试时遇到此java.lang.NoClassDefFoundError: android/util/Log错误。

import android.support.test.runner.AndroidJUnit4;

import com.joketellingapp.lacourt.displayjoke.DisplayJoke;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertTrue;
/**
 * Created by kev on 3/21/16.
 */

@RunWith(AndroidJUnit4.class)
public class EndPointAsyncTaskTest {

    @Test
    public void testDoInBackground() throws Exception {
        com.udacity.gradle.builditbigger.MainActivityFragment fragment = new com.udacity.gradle.builditbigger.MainActivityFragment();
        fragment.testFlag = true;
        new EndpointsAsyncTask().execute();
        Thread.sleep(5000);
        assertTrue("Error: Fetched Joke = " + DisplayJoke.JOKE_CONTENT, DisplayJoke.JOKE_CONTENT != "");
    }
}

在StackOverflow上似乎并没有完全相同的错误,至少没有使用Android Studio。

我已经在终端上尝试过gradlew test命令,但是没有任何变化。

这是gradle.build(应用模块):

apply plugin: 'com.android.application'
apply plugin: 'com.google.cloud.tools.endpoints-framework-client'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.google.cloud.tools:endpoints-framework-gradle-plugin:1.0.2'
    }
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.3"

    defaultConfig {
        applicationId "com.udacity.gradle.builditbigger"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    configurations {
        all*.exclude group: 'com.google.guava'//I excluded guava for trying to solve duplicate zip entry error.
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    endpointsServer project(path: ':backend', configuration: 'endpoints')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.google.android.gms:play-services-ads:11.8.0'
    implementation 'com.google.api-client:google-api-client:1.23.0'
    implementation 'com.google.http-client:google-http-client-android:1.23.0'
    implementation 'com.google.code.findbugs:jsr305:3.0.1'

    testImplementation 'junit:junit:4.12'
    testApi 'com.android.support.test:runner:1.0.2'
    testImplementation 'com.android.support.test:rules:1.0.2'
//    androidTestImplementation 'com.android.support:support­annotations:26.1.0'
    testImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    api project(':jokeSource')
    api project(':displayjoke')
    compile project(path: ':backend')
}

完整的logcat:

java.lang.NoClassDefFoundError: android/util/Log

    at android.support.test.runner.AndroidJUnit4.loadRunner(AndroidJUnit4.java:73)
    at android.support.test.runner.AndroidJUnit4.loadRunner(AndroidJUnit4.java:63)
    at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.lang.ClassNotFoundException: android.util.Log
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 23 more

0 个答案:

没有答案