Exception in thread “main” java.lang.NoClassDefFoundError: junit/textui/ResultPrinter

前端 未结 8 1177
长发绾君心
长发绾君心 2020-12-03 06:35

I\'m trying to compile my Android project in Android Studio 0.3.0. Today I get the following error:

Exception in thread \"main\" java.lang.NoClassDefFoundErr         


        
相关标签:
8条回答
  • 2020-12-03 06:48

    For standard JUnit tests, you are missing the JUnit library, try adding this in build.gradle:

    dependencies {
        ...
        testCompile 'junit:junit:4.12'
        ...
    }
    

    And make sure you are putting the tests in the "test" directory parallell to the "main" package (instrumentation tests goes into "androidTest", but that is different setup).

    0 讨论(0)
  • 2020-12-03 06:51

    All the answers are good-

    Make sure you have sourceSets have the test directory registered.

    android{
        ...
    
        defaultConfig {
             ...
             testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
    
        sourceSets {
            androidTest {
                java.srcDirs = ['src/androidTest/java']
            }
        }
    }
    
    dependencies{
        ... 
        androidTestCompile 'junit:junit:4.12'
        androidTestCompile 'com.android.support.test:runner:0.5'
    }
    
    0 讨论(0)
  • 2020-12-03 06:55

    I've twice had this issue after changing some build.gradle plugins and dependencies around.

    Simply turning Android Studio off and on again fixed it for me (versions 2.1.2 and 2.2).

    0 讨论(0)
  • 2020-12-03 06:56

    Finally found it. It's in the Run/Debug Configurations dialog. Disabled JUnit and it compiles again.

    0 讨论(0)
  • 2020-12-03 07:00

    In my Run/Debug Configurations I had MyTest under AndroidTests and under JUnitTests. The MyTest under AndroidTests was configured correctly, but I still got 'NoClassDefFoundError: junit/textui/ResultPrinter' error. Clicked on MyTest under JUnit and pressed '-' in upper left. This allowed MyTest to run through the device got rid of error.

    0 讨论(0)
  • 2020-12-03 07:03

    Just putting the solution which worked for me, this should be tried if you are setting up the environment for first time:

    For windows: 1) in the environment variables add a new "system variables" ANDROID_SDK_HOME=D:\Program Files\android-sdk-windows (select your home directory of android sdk )

    2) modify system variables Path, add "%Android_SDK_HOME%\tools;"

    0 讨论(0)
提交回复
热议问题