【问题标题】:Android Studio can't resolve jUnit 4.12 dependencyAndroid Studio 无法解析 jUnit 4.12 依赖
【发布时间】:2018-07-23 22:50:52
【问题描述】:

我遇到了一个在这个平台上似乎很受欢迎的问题, 但似乎没有答案对我有用。我面临着与此类似的问题:

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...)、检查我的代理设置等等。

【问题讨论】:

    标签: java android maven gradle junit


    【解决方案1】:

    我通过从 Settings -> JunitGenerator -> Properties -> Default Template 中选择 Junit4 解决了这个问题:

    【讨论】:

      【解决方案2】:

      缓存出现问题。 我通过点击file -&gt; Invalidate Caches 修复它,然后重新启动

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-19
        • 2018-04-30
        • 1970-01-01
        • 1970-01-01
        • 2019-10-24
        • 2018-04-25
        • 2019-03-31
        • 2017-05-18
        相关资源
        最近更新 更多