java 无法解析符号 AndroidJUnit4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43342831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot resolve symbol AndroidJUnit4
提问by Sharmaine Shane Cinense
I'm trying to add loginfacebook for my app. But when I added a repository that is need in doing this. It caused an error. The AndroidJUnit4 cannot resolve now.
我正在尝试为我的应用添加 loginfacebook。但是当我添加了一个需要这样做的存储库时。它导致了错误。AndroidJUnit4 现在无法解析。
ExampleInstrumentedTest.java
ExampleInstrumentedTest.java
package com.example.user.enyatravelbataan;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.*;
/**
* Instrumentation 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() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.example.user.enyatravelbataan",
appContext.getPackageName());
}
}
and this is my build:gradle(app)
这是我的构建:gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
useLibrary 'org.apache.http.legacy'
repositories {
mavenCentral()
}
defaultConfig {
applicationId "com.example.user.enyatravelbataan"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile(name: 'wikitudesdk', ext: 'aar')
// compile 'org.apache.httpcomponents:httpclient:4.5'
// compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile files('libs/MD5Simply.jar')
compile files('libs/GenAsync.1.2.jar')
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:multidex:1.0.0'
compile 'com.google.android.gms:play-services:9.8.0'
compile 'com.google.android.gms:play-services-location:9.8.0'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:design:24.2.1'
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support:support-v4:24.2.1'
testCompile 'junit:junit:4.12'
}
repositories {
flatDir {
dirs 'libs'
}
}
apply plugin: 'com.google.gms.google-services'
回答by Pehlaj
Try
尝试
androidTestImplementation 'com.android.support:support-annotations:23.1.0'
androidTestImplementation 'com.android.support.test:runner:0.4.1'
androidTestImplementation 'com.android.support.test:rules:0.4.1'
Add following above dependencies section
添加以下依赖项部分
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
}
回答by Vincy
In addition to the above answer please ensure you follow these steps strictly:
除了上述答案,请确保您严格遵循以下步骤:
I was banging my head against the wall and/or any object i see in front of me in order to make my SQLite function unit tested. No matter what I do, how strictly follow the suggestions provided my many wise people all over the internet did not work.
我正在用头撞墙和/或我在我面前看到的任何物体,以便对我的 SQLite 功能单元进行测试。无论我做什么,如果我在互联网上的许多聪明人都不起作用,那么严格遵循建议。
I then had to go to preschool and start over to realize my stupid mistake. I learned that AndroidJUnit4 can only be used with Instrumentation Test and JUnit must be used for local tests. That being said, the folder must be src/androidTest/java. I had my test class directly under androidTest folder, hence I had to face that nasty error. However, the moment I moved it under src/androidTest/javaeverything went very clear like "I can see clearly now the rain is gone".
然后我不得不去幼儿园并重新开始意识到我的愚蠢错误。我了解到 AndroidJUnit4 只能与 Instrumentation Test 一起使用,而 JUnit 必须用于本地测试。也就是说,文件夹必须是src/androidTest/java。我的测试类直接位于 androidTest 文件夹下,因此我不得不面对那个讨厌的错误。然而,当我将它移到src/androidTest/java下的那一刻,一切都变得非常清晰,就像“我现在可以清楚地看到雨没了”。
Take a look at this articlewhich says...
看看这篇文章,它说...
Run Instrumented Unit Tests To run your instrumented tests, follow these steps:
Be sure your project is synchronized with Gradle by clicking Sync Project in the toolbar. Run your test in one of the following ways: To run a single test, open the Project window, and then right-click a test and click Run . To test all methods in a class, right-click a class or method in the test file and click Run . To run all tests in a directory, right-click on the directory and select Run tests . The Android Plugin for Gradle compiles the instrumented test code located in the default directory (src/androidTest/java/), builds a test APK and production APK, installs both APKs on the connected device or emulator, and runs the tests. Android Studio then displays the results of the instrumented test execution in the Run window.
运行插桩单元测试 要运行插桩测试,请按照以下步骤操作:
通过单击工具栏中的同步项目,确保您的项目与 Gradle 同步。以下列方式之一运行您的测试: 要运行单个测试,请打开“项目”窗口,然后右键单击一个测试并单击“运行”。要测试类中的所有方法,请右键单击测试文件中的类或方法,然后单击运行。要运行目录中的所有测试,请右键单击该目录并选择 Run tests 。Android Plugin for Gradle 编译位于默认目录 (src/androidTest/java/) 中的插装测试代码,构建测试 APK 和生产 APK,在连接的设备或模拟器上安装这两个 APK,并运行测试。然后,Android Studio 会在“运行”窗口中显示检测的测试执行结果。
Therefore folks, for instrumentation test the folder must be (do not forget the case)
因此,对于仪器测试,文件夹必须是(不要忘记案例)
src/androidTest/java
src/androidtest/java
and for local tests the folder must be
对于本地测试,文件夹必须是
src/test/java
源代码/测试/Java
You can then have your package folder(s) to match your app package
然后你可以让你的包文件夹匹配你的应用包
Hope, this helps for the community!
希望,这对社区有所帮助!
回答by milosmns
Another important thing (that has surfaced this issue for me) is to check if you have changed the build type for tests- that would be the testBuildType
option in your module's build.gradle
.
If you did change it (like I have), then before editing any Android tests:
另一件重要的事情(对我来说已经浮出水面)是检查您是否更改了测试的构建类型- 这将是testBuildType
您模块的build.gradle
.
如果你确实改变了它(就像我一样),那么在编辑任何 Android 测试之前:
- Go to your Android Studio build/flavors tab
- Select the same build type you specified in your
testBuildType
Gradle property - Sync Android Studio with Gradle
- 转到您的 Android Studio build/flavors 选项卡
- 选择您在
testBuildType
Gradle 属性中指定的相同构建类型 - 将 Android Studio 与 Gradle 同步
To run the app normally again, you would obviously need to switch back to 'debug' I suppose.
要再次正常运行应用程序,我想您显然需要切换回“调试”。
Note: Even though this fixes my core issue as described by the question here, I'm still looking for a way to support both custom build types for Android tests, but also allow debug build types; my ultimate goal is to have CI running the tests with a special build type, but let developers run them in 'debug' mode. So if anyone has an idea on how to accomplish that, the comments section is yours. :)
注意:尽管这解决了我的核心问题,如此处的问题所述,我仍在寻找一种方法来支持 Android 测试的自定义构建类型,但也允许调试构建类型;我的最终目标是让 CI 使用特殊的构建类型运行测试,但让开发人员在“调试”模式下运行它们。所以如果有人对如何实现这一点有想法,评论部分就是你的。:)
回答by DAVID KATHOH
Just add compile "com.android.support.test:runner:1.0.1"
to your Gradle.
只需添加compile "com.android.support.test:runner:1.0.1"
到您的 Gradle。
回答by Suragch
For me it was because some of the androidTestImplementation
imports were not up to date in build.gradle. After I updated them allto the newest version, the error was gone.
对我来说,这是因为某些androidTestImplementation
导入在build.gradle中不是最新的。在我将它们全部更新到最新版本后,错误消失了。
Note:
笔记:
My project had multiple modules and I had to update them in everymodule.
我的项目有多个模块,我必须在每个模块中更新它们。
回答by subhash gaikwad
Please try the latest API like following
请尝试最新的 API,如下所示
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-ore:3.0.2'
still, No luck then try "implementation" instead of testImplementation, androidTestImplementation and androidTestImplementation and sync in Gradle.
仍然,没有运气,然后尝试“实现”而不是 testImplementation、androidTestImplementation 和 androidTestImplementation 并在 Gradle 中同步。
回答by Alaa Bouraoui
the solution that worked for me is changing the emulator (one that uses the api you are using) To do that click on the symbol of the emulatr next to the Run button (ex : Pixel 2) then go down to Open AVD Manager (add the api u need (ex API 29)) Thats all
对我有用的解决方案是更改模拟器(一个使用您正在使用的 api 的模拟器)为此单击运行按钮旁边的模拟器符号(例如:Pixel 2)然后转到打开 AVD 管理器(添加你需要的 api (ex API 29)) 就是这样