Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Didn't find class "android.support.test.runner.AndroidJUnitRunner"

Tags:

android

I'm trying to use an instrumented test and I updated my project to AndroidX. But I get the following error while trying to run:

2018-10-09 14:22:29.468 13263-13263/xxx.yyy E/AndroidRuntime: FATAL EXCEPTION: main     Process: xxx.yyy, PID: 13263     java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{xxx.yyy.test/android.support.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5730)         at android.app.ActivityThread.-wrap1(Unknown Source:0)         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)         at android.os.Handler.dispatchMessage(Handler.java:105)         at android.os.Looper.loop(Looper.java:164)         at android.app.ActivityThread.main(ActivityThread.java:6541)         at java.lang.reflect.Method.invoke(Native Method)         at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)      Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)         at java.lang.ClassLoader.loadClass(ClassLoader.java:379)         at java.lang.ClassLoader.loadClass(ClassLoader.java:312)         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5728)         at android.app.ActivityThread.-wrap1(Unknown Source:0)          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)          at android.os.Handler.dispatchMessage(Handler.java:105)          at android.os.Looper.loop(Looper.java:164)          at android.app.ActivityThread.main(ActivityThread.java:6541)          at java.lang.reflect.Method.invoke(Native Method)          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  

My test looks like this:

package xxx.yyy;  import android.content.Context; import androidx.test.InstrumentationRegistry; import androidx.test.runner.AndroidJUnit4;  import org.junit.Test; import org.junit.runner.RunWith;  import static org.junit.Assert.*;  /**  * Instrumented test, which will execute on an Android device.  *  * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>  */ @RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest {     @Test     public void useAppContext() {         // Context of the app under test.         Context appContext = InstrumentationRegistry.getTargetContext();          assertEquals("xxx.yyy", appContext.getPackageName());     } } 
like image 873
gurehbgui Avatar asked Oct 09 '18 08:10

gurehbgui


2 Answers

Inside the build.gradle file there you find the defaultConfig section. With the testInstrumentationRunner attribute the respective test runner can be specified. There the test runner from the android.support package might be specified:

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

this has to be change to the test runner from the androidx package:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 
like image 175
Stefan Avatar answered Oct 02 '22 22:10

Stefan


Didn't find class "android.support.test.runner.AndroidJUnitRunner"

You migrated to AndroidX but as you can see, there is-are still usages of android.support test libraries in your project.

To solve this, you should search for anything in anywhere in your project which starts by android.support and replace it or removing it to use AndroidX libraries.

like image 20
ʍѳђઽ૯ท Avatar answered Oct 02 '22 22:10

ʍѳђઽ૯ท