Cannot resolve symbol 'AndroidJUnit4'

≯℡__Kan透↙ 提交于 2019-11-27 09:30:42

问题


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

回答1:


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




回答2:


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




回答3:


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)




回答4:


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.




回答5:


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.




回答6:


In my case this helped for release variant:

android {
    ...
    testBuildType "release" 
}



回答7:


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'



回答8:


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 ?!




回答9:


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



回答10:


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.




回答11:


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




回答12:


Adding

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

resolved this exact issue for me.




回答13:


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.




回答14:


The classical Invalidate Caches/Restart has helped me! :)




回答15:


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.



来源:https://stackoverflow.com/questions/30603487/cannot-resolve-symbol-androidjunit4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!