Cannot resolve symbol 'AndroidJUnit4'
AndroidGradlebuild.gradleAndroid UiautomatorTesting Support-LibraryAndroid Problem Overview
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
Android Solutions
Solution 1 - Android
Make sure your app in debug build variant. Go to Build > Select Build Variant... and the following should show up:
Solution 2 - Android
I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.
Solution 3 - Android
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)
Solution 4 - Android
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.
Solution 5 - Android
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.
Solution 6 - Android
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.
Solution 7 - Android
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 .
https://stackoverflow.com/questions/52873173/migrating-junit4-tests-to-androidx-what-causes-delegate-runner-could-not-be-lo 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 ?!
Solution 8 - Android
In my case this helped for release variant:
android {
...
testBuildType "release"
}
Solution 9 - Android
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'
Solution 10 - Android
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'
})
Solution 11 - Android
Move the test class to src/androidTest/java/. Then dependency will resolve.
Solution 12 - Android
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.
Solution 13 - Android
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.
Solution 14 - Android
The classical Invalidate Caches/Restart has helped me! :)
Solution 15 - Android
The same error occurred to me when I follow Google IOSched app and set up my project with three build types [debug,release,staging] where debug and release share the same source directory
sourceSets {
debug.java.srcDir 'src/debugRelease/java'
release.java.srcDir 'src/debugRelease/java'
}
In this case, specify the testBuildType
in your module-level build.gradle file and the project should now be able to resolve symbol 'AndroidJUnit4'.
...
sourceSets {
debug.java.srcDir 'src/debugRelease/java'
release.java.srcDir 'src/debugRelease/java'
}
testBuildType "staging"
...
Reference: https://github.com/google/iosched/blob/master/mobile/build.gradle
Solution 16 - Android
Adding
compile com.android.support.test:runner:0.5'
resolved this exact issue for me.
Solution 17 - Android
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.
Solution 18 - Android
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.
-
https://github.com/codepath/android_guides/wiki/UI-Testing-with-Espresso
-
https://github.com/udacity/AdvancedAndroid_TeaTime/issues/14
-
https://stackoverflow.com/questions/43342831/cannot-resolve-symbol-androidjunit4/43342884
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.
Solution 19 - Android
Make sure you have
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
dependency in your app build.gradle file. I deleted it by mistake and test stopped wroking.