Home » Android » android – Cannot resolve symbol 'AndroidJUnit4'

android – Cannot resolve symbol 'AndroidJUnit4'

Posted by: admin March 11, 2020 Leave a comment

Questions:

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

import android.support.test.runner.AndroidJUnit4;

When I do that, Android Studio highlights runner in red and complains “Cannot resolve symbol ‘runner'”.

Background

I got to this point by following the tutorials on the Android Developer site for setting up tests using UI Automator. The first problem I encountered 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 from this Android bug report and added the following to allprojects in my project’s build.gradle:

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

This solved the immediate error, but I suspect it lead to my current problems. Does anyone have any suggestions about how to fix this?

Relevent 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
How to&Answers:

Make sure your app in debug build variant. Go to Build > Select Build Variant… and the following should show up:

enter image description here

Answer:

I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.

Answer:

Ok so here is your mistake and mine!

If we are going to write a pice of code for Local Unit Testing we shouldn’t use @RunWith(AndroidJUnit4.class) cause we do not use AndroidJUnit4 but we need Junit4. so we should write @RunWith(JUnit4.class). And of course your java test file is under app/src/test/java/your.package.name directory.

Else if (!!) we want to write some Android Instrumented Unit Test we should put our test java files in app/src/androidTest/java/your.package.name directory and use annotation like @RunWith(AndroidJUnit4.class)

Answer:

Update

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

Original Answer

I found here that there are newer versions of the Testing Support Library than what I was using:

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 most recent versions of these libraries. This question is from a time when the Android Test Support Library was new and the version numbers here are very out of date.

Answer:

I solved the problem by making a small change in the app’s build.gradle file. In the dependencies { ... } section, make sure to include the following line:

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

or whatever version is the newest at that point (...Compile is deprecated and has been replaced by ...Implementation). Note the use of debugImplementation. Android Studio suggested auto-including it with androidTestImplementation, which didn’t work.

I found out how to change it from test to debug by looking in Project Structure under Dependencies of the app module, where you can change the scope of each dependency, see below.

Project structure

Answer:

In my case this helped for release variant:

android {
    ...
    testBuildType "release" 
}

Answer:

Notice that this the OP is now in 2019 , 4 years old so If you are using Android X then AndroidJUnit4.class is deprecated , you have an error there and one more with this androidx.test.ext.junit.runners.AndroidJUnit4. I suggest to read this links to solve the problem .

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

Migrating Junit4 tests to androidx: What causes 'delegate runner could not be loaded'?
For me Android Studio suggested to replace

@RunWith(AndroidJUnit4.class)

which was deprecated with

@RunWith(AndroidJUnit4ClassRunner.class)

and this

androidx.test.ext.junit.runners.AndroidJUnit4

with this

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

After that the error is gone but I don’t know if the future test while run ok ?!

Answer:

If anyone still having this issue:

Cannot resolve symbol ‘AndroidJUnit4’

and using API 27, in the build.gradle which is in the app 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'

Answer:

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'
})

Answer:

If you’re using project with multiple build-type then the selected build-type in build-variant window must be mentioned with testBuildType tag in module’s build.gradle file.

For e.g.: If you’re using build type debug then you should add android{testBuildType "debug" }, if using stage then add android{testBuildType "stage"} statement in android tag.

Answer:

The classical Invalidate Caches/Restart has helped me! 🙂

Answer:

Move the test class to src/androidTest/java/. Then dependency will resolve.

Answer:

Add this dependency in your build.gradle file:

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

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

Answer:

The common cause of this problem is that when adding below dependency:

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

This is a correct dependency if you are going to use instrumented tests (tests in the androidTest java package)

But for implementing local unit tests (tests in test java package) while using above mentioned dependency; then you’ll face Cannot resolve symbol 'AndroidJUnit4'

That is because androidTestImplementation directive is used to import libraries in instrumented tests, but not in the local JVM/unit tests.

If you want to use AndroidJUnit4 in a local JVM/unit test, then use below dependency instead

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

The same thing applies if you add the latter dependency while using AndroidJUnit4 in instrumented test, you will also get Cannot resolve symbol 'AndroidJUnit4'; because you’re using the wrong directive.

Answer:

Adding

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

resolved this exact issue for me.

Answer:

As the list of answers demonstrate, this can be caused by a few things. One more for the list:

I ran an over-zealous LINT which removed all unused imports. This will produce the same errors, and it is easy to miss that this is the problem.

Android-studio will highlight references that are missing in the test code – and the ALT-ENTER popup will appear (this is the bit that is easy to miss).

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

Edit: @Code-Apprentice, the lines that were missing 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.

Answer:

Short Story Version:

Upgrade your Gradle to the latest Version

I am answering this question post on Feb, 15th, 2020.
Unfortunately, I have exhausted every possible solutions mentioned here and elsewhere.

Yes, none of them above workes. I use the built-in function, “Migrate to Andoridx”, it may remind me that I have to upgrade my target SDK versions and my gradle version. After I upgraded my gradle version from 2.0.2 to 3.5.3. They just works, even the old import statement works.