62

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.NoClassDefFoundError: junit/textui/ResultPrinter
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: java.lang.ClassNotFoundException: junit.textui.ResultPrinter
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 3 more

Process finished with exit code 1

Doing some web searches leads me to believe this issue is somehow related to JUnit. However, I'm not using JUnit in my project. Maybe I've inadvertently turned on some option? In that case, how do I disable unit testing in my project? Any ideas?

Janne Oksanen
  • 1,524
  • 1
  • 13
  • 14

8 Answers8

57

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

Janne Oksanen
  • 1,524
  • 1
  • 13
  • 14
  • 10
    I had the same problem on Android Studio 0.4.2. I actually tried to run a test as regular JUnit test case instead of Android Test Case. – pkk Jan 21 '14 at 15:58
  • 13
    Why can't you run a TestSuite as a regular JUnit test case? – IgorGanapolsky May 14 '14 at 16:29
  • 2
    @IgorGanapolsky Running as a JUnit test instead of Android test means that it would run on the host system instead of on a device. Running as an android test deploys the test apk to the device and runs it there (under dalvik vm). A long time ago I had Eclipse setup so I could run JUnit tests against code inside an android project; however, it only worked for classes that didn't use any Android classes (Activity, View, Intent, etc). If any were included, there's a stub implementation to support compiling against the signature, but when run, it throws a RuntimeException: "Stub!" – Stan Kurdziel Oct 27 '14 at 04:37
  • 1
    I seem to have the same problem. You tell me to disable the junit under Run->Edit Configurations... Even though I do find a junit tab there, I do not see any check box to disable it, or similar. What exactly should I do to disable it? Could you clarify this for me? – Kagaratsch Mar 15 '15 at 03:15
  • Ok, i'm not sure what i did, but somehow i got it to work having messed around with Run->Edit Configurations... long enough. Thanks! – Kagaratsch Mar 15 '15 at 03:20
  • Detailed description with screen shots: http://stackoverflow.com/questions/27890011/exception-in-thread-main-java-lang-noclassdeffounderror-junit-textui-resultpr?answertab=votes#tab-top – vikoo Jun 22 '15 at 09:52
27

Usually this problem is caused because of wrong test type: Junit instead of Android Test Select Android test instead of JUnit Select Android test instead of JUnit

radistao
  • 14,889
  • 11
  • 66
  • 92
8

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).

powder366
  • 4,351
  • 7
  • 47
  • 79
  • `src/test/java` Ref: [Run Local Unit Tests](http://developer.android.com/training/testing/start/index.html#build) – Ivan Chau Jan 23 '16 at 15:14
7

Force the project to "sync" via menu: Tools > Android > Sync Project with Gradle Files or force the project to "sync" by making a fake change in build.gradle file (add a space and then delete it and then click "sync now").

Pnemonic
  • 1,685
  • 17
  • 17
1

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.

flobacca
  • 936
  • 2
  • 17
  • 42
0

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).

Tom
  • 6,946
  • 2
  • 47
  • 63
0

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'
}
Manisha
  • 813
  • 13
  • 24
-2

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;"

techExplorer
  • 810
  • 7
  • 16