Cannot resolve character "AndroidJUnit4"

Obviously, to solve this problem I need the right import. According to docs for AndroidJUnit4 , this should be

 import android.support.test.runner.AndroidJUnit4; 

When I do this, Android Studio highlights the runner red and complains of "Cannot resolve the runner symbol."

Background

I got to this by following the instructions on the Android developer site to configure tests using the UI Automator . The first problem I ran into was that com.android.support:support-v4:22.2.0 and com.android.support.test:runner:0.2 depend on different versions of com.android.support:support-annotations . I followed the suggestions of this Android error report and added the following to allprojects :

 configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:22.1.0' } 

This resolved the immediate mistake, but I suspect that it led to my current problems. Anyone have suggestions for fixing this?

Relevant sections from .. / gradlew: app: dependencies

 androidTestCompile - Classpath for compiling the androidTest sources. +--- com.jayway.android.robotium:robotium-solo:5.2.1 +--- com.squareup:fest-android:1.0.8 | \--- org.easytesting:fest-assert-core:2.0M10 | \--- org.easytesting:fest-util:1.2.5 +--- com.android.support.test:runner:0.2 | +--- junit:junit-dep:4.10 | | \--- org.hamcrest:hamcrest-core:1.1 | +--- com.android.support.test:exposed-instrumentation-api-publish:0.2 | \--- com.android.support:support-annotations:22.0.0 -> 22.2.0 +--- com.android.support.test:rules:0.2 | \--- com.android.support.test:runner:0.2 (*) \--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0 compile - Classpath for compiling the main sources. +--- com.android.support:appcompat-v7:22.2.0 | \--- com.android.support:support-v4:22.2.0 | \--- com.android.support:support-annotations:22.2.0 +--- com.android.support:support-v4:22.2.0 (*) +--- com.google.android.gms:play-services:6.1.71 | \--- com.android.support:support-v4:20.0.0 -> 22.2.0 (*) +--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13 \--- com.jakewharton:butterknife:5.1.2 
+128
android build.gradle gradle android-uiautomator testing-support-library
Jun 02 '15 at 18:10
source share
15 answers

Update

Android Test Library is now part of AndroidX. Be sure to use the correct Gradle dependencies listed in the official documentation .

Original answer

here I discovered that there are newer versions of the test support library than what I used:

 dependencies { androidTestCompile 'com.android.support.test:runner:0.5' androidTestCompile 'com.android.support.test:rules:0.5' androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2' } 

Note. Be sure to use the latest versions of these libraries. This question dates back to when the Android Testing Support Library was new and the version numbers are very outdated.

+32
Jun 03 '15 at 18:05
source share

Make sure your application is in a debug build option. Go to Build> Select Build Variant ... and the following should appear:

enter image description here

+178
May 22 '16 at 6:12
source share

I made a mistake to put test classes in src / test . After moving them to src / androidTest / java / the dependency was fixed.

+108
Apr 05 '16 at 12:54 on
source share

So, here is your mistake and mine!

If we are going to write code for Local Unit Testing , we should not use @RunWith(AndroidJUnit4.class) because we do not use AndroidJUnit4, but we need Junit4. therefore we have to write @RunWith(JUnit4.class) . And, of course, your java test file is in the app/src/test/java/your.package.name .

Otherwise, if (!!) we want to write the Android Instrumented Unit Test , we must put our Java test files in the app/src/androidTest/java/your.package.name and use the annotation as @RunWith(AndroidJUnit4.class)

+64
Apr 6 '16 at 8:17
source share

I solved the problem by making a small change to the build.gradle file of the application. In the dependencies {... } section dependencies {... } be sure to add the following line:

 debugImplementation 'com.android.support.test:runner:1.0.1' 

or some version is the newest at that time ( ...Compile deprecated and has been replaced by ...Implementation ). Note the use of debugImplementation . Android Studio offered to automatically enable it with androidTestImplementation , which did not work.

I learned how to change it from test to debugging by looking at the Project Structure in the Dependencies section of the application module, where you can change the scope of each dependency, see below.

Project structure

+23
Feb 11 '18 at 18:28
source share

In my case, this helped for the release option:

 android { ... testBuildType "release" } 
+8
Feb 17 '18 at 17:02
source share

If anyone still has this problem:

Cannot resolve character "AndroidJUnit4"

and using API 27, in build.gradle which is in the application module, add the following lines:

 testImplementation 'junit:junit:4.12' // AndroidJUnitRunner and JUnit Rules androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:rules:1.0.2' // Espresso dependencies androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 
+5
Apr 7 '18 at 22:04 on
source share

Please note that this OP is now in 2019, 4 years old, so if you use Android X, then AndroidJUnit4.class out of date, you have an error and another one with this androidx.test.ext.junit.runners.AndroidJUnit4 . I suggest reading this link to solve the problem.

AndroidJUnit4.class deprecated: how to use androidx.test.ext.junit.runners.AndroidJUnit4?

Migrating Junit4 tests to androidx. Reasons why the delegate program & # 39; failed to load? For me, Android Studio was offered to replace

 @RunWith(AndroidJUnit4.class) 

which is outdated as

 @RunWith(AndroidJUnit4ClassRunner.class) 

and this

 androidx.test.ext.junit.runners.AndroidJUnit4 

with this

 import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner; 

After that, the error disappeared, but I do not know if the future test will pass normally ?!

+5
Aug 22 '19 at 18:32
source share

put this code in your Dependencies

 compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) 
+3
Jan 04 '17 at 9:10
source share

If you are using a project with several assembly types, then the selected assembly type in the assembly option window should be indicated with the testBuildType tag in the module build.gradle file.

For example: if you use the debug assembly type, then you need to add android{testBuildType "debug" } , if you use stage , then add the android{testBuildType "stage"} statement to the android{testBuildType "stage"} tag.

+1
Mar 05 '19 at 19:20
source share

Move the test class to src / androidTest / java /. Then the dependence will be resolved.

+1
Apr 24 '19 at 14:16
source share

Adding

 compile com.android.support.test:runner:0.5' 

solved exactly this problem for me.

0
Mar 31 '18 at 15:53
source share

As the answer list shows, there are several reasons for this. Another one for the list:

I ran an excessive LINT that removed all unused imports. This will lead to the same errors, and it is easy not to notice that this is a problem.

Android studio will select links that are not in the test code - and an ALT-ENTER pop-up window will appear (this is a bit that is easy to skip).

Next, I need to remove the tests from LINT - or at least disable this warning.

Edit: @ Code-Apprentice, the missing lines were:

 import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; 

So, the first error in the file was with @RunWith(AndroidJUnit4.class) at the beginning of my test class.

0
Oct 20 '18 at 2:01
source share

Classical Invalidate Caches / Restart helped me! :)

0
Apr 24 '19 at 7:20
source share

Add this dependency to your build.gradle file:

 androidTestImplementation 'androidx.test.ext:junit:1.1.1' 

Update the final version ( 1.1.1 ) with the latest released version.

0
Sep 30 '19 at 9:06
source share



All Articles