So I\'m trying our Android Studio and testing a project that worked in eclipse. I got everything compiling and the application will launch just fine, but I can\'t get my unit te
Since none of these answers helped me, I wanted to share my solution for anyone who is as desperate as I was. :)
Because of the testing libraries that I was using, I needed to enable multidex support by adding multiDexEnabled true
to my Gradle build. I'm not sure I had multidex support fully implemented to begin with (the proper way of doing it has changed since I last implemented it) but ultimately, I didn't end up needing it and removing that line from my build fixed the error. My team at work has had a few testing issues related to enabling multidex support… in typical Android style.
I solved this problem by changing
android.test.InstrumentationTestRunner
into
com.android.test.runner.MultiDexTestRunner
in EditConfigurations -> Specific Instrumentation Runner (optional) tab.
Turns out, because my app is using MultiDex, I need to change test runner to MultiDexTestRunner as well.
UPDATE:
As @dan comment, InstrumentationTestRunner
is deprecated use AndroidJUnitRunner
instead.
In my case the Run/Debug Configurations were wrong.
One Solution:
Go to Run/Debug Configurations
Run -> Edit Configurations...
Setup a Android-Test for your test class
Select your Android test configuration on the left side
or create a new one with the plus icon and name it e.g. ClassNameTest
Select the module containing your test class. In the simplest case the test class is in your app module so select app.
Select on the next row your test configuration. I use:
Choice your test class
Finally configure your target device and select ok.
This answer is going to explain the history issues and summarise all related settings.
Basically there are 3 possible places for instrumentation test runner configurations.
In EditConfigurations -> General tab -> Specific Instrumentation Runner (optional)
This is existing in Android Studio 2.2 and version before only, in latest version, it is removed already.
In manifest file, the test runner is configured as below.
< instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
android:targetPackage="com.mytestapp.test"/>
Since "android.test.InstrumentationTestRunner" is deprecated in API level 24, this setting is also not necessary already, as long as you configure the runner in gradle file.
If you want to keep this setting, please make sure the runner name should match to the one you set in the gradle file, otherwise you will got this error also.
android {
.......
defaultConfig {
.......
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
This only works in AndroidStudio Version < 2.3
In my case the wrong instrumentation runner was selected.
I fixed this by specifying the instrumentation runner in the Run/Debug Configuration of the test (see below). There you can select a runner from the list.
You find the Run/Debug Configurations: Run -> Edit Configurations ...
If you have a testInstrumentationRunner defined in your build.gradle
such as this:
android {
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
make sure that in the run configuration window you use the exact same "Specific instrumentation runner" in your Android Studio / IntelliJ run configuration for your test.