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
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).
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'
}
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).
Finally found it. It's in the Run/Debug Configurations dialog. Disabled JUnit and it compiles again.
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.
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;"