我遇到了一个在这个平台上似乎很受欢迎的问题,
但似乎没有答案对我有用。我面临与此类似的问题:
Error:(23, 17) Failed to resolve: junit:junit:4.12
但是 43 个答案都不适用于我的问题。
(我希望这不会被标记为重复)
我尝试手动将 org.junit 包导入到测试类,但无法解决。我认为这是因为 Gradle 无法加载 JUnit 库,但我不知道如何更改它。
即使在创建一个新的 android studio 项目时,我在设置 JUnit 时也遇到了问题。甚至无法运行示例测试:
package de.ckc.azubis.fleetparkmanagement;
import static junit.framework.Assert.assertEquals;
/**
* Example local unit test, which will execute on the development machine
(host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
当我尝试导入 Test 注释时,JUnit 依赖项被添加到 gradle 文件中,但没有被导入到测试类中。
这是我的模块级build.gradle:
应用插件:'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "de.ckc.azubis.fleetparkmanagement"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:cardview-v7:27.0.2'
}
和我的项目级别 build.gradle
//顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项。
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我已经尝试了我能想到的一切,包括重新配置依赖项、手动添加我能想到的每个存储库(mavenCentral、jcenter...)、检查我的代理设置等等。
最佳答案
缓存出现问题。
我通过单击 file -> Invalidate Caches
修复了它然后重新启动
关于java - Android Studio 无法解析 jUnit 4.12 依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768989/