Android Studio 无法解决 Espresso 3.0.0。

6
根据Android Espresso文档,迄今为止:

添加Espresso依赖项

要将Espresso依赖项添加到您的项目中,请完成以下步骤:

  1. 打开应用程序的build.gradle文件。这通常不是顶级build.gradle文件,而是app/build.gradle。
  2. 在dependencies内添加以下行:
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'
androidTestCompile 'com.android.support.test:runner:1.0.0'

我创建了一个新项目,生成的 app/gradle 文件如下所示:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.app.test"
        minSdkVersion 24
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    testCompile 'junit:junit:4.12'
}

当我将它改为以下内容时:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.app.test"
        minSdkVersion 24
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:26.+'
    testCompile 'junit:junit:4.12'
    // App's dependencies, including test
    compile 'com.android.support:support-annotations:22.2.0'

    // Testing-only dependencies
    androidTestCompile 'com.android.support.test:runner:1.0.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'
}

我遇到了以下错误:

Error:(29, 24) Failed to resolve: com.android.support.test:runner:1.0.0
    安装存储库并同步项目
Error:(30, 24) Failed to resolve: com.android.support.test.espresso:espresso-core:3.0.0
    安装存储库并同步项目

我尝试点击“安装存储库并同步项目”链接,但没有任何反应。我也尝试在SDK管理器中查找,但看不到任何东西。

2
你是否从Google的maven导入依赖项? - R. Zagórski
天啊!又是那些碎片化的文档在困扰我!我在项目 gradle 上尝试的所有方法都不起作用,除了这个。谢谢。 - Quintin Balsdon
4个回答

16

由于评论中的解决方案解决了问题,我将其添加为其他人的答案:

请务必在主build.gradle文件中添加Google的Maven链接:

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

作为一条注释,我确实需要将我的支持注解升级到“compile 'com.android.support:support-annotations:25.4.0'”在app/gradle文件中。 - Quintin Balsdon

1

简单来说,将google()添加到allprojects > repositories中即可解决问题...

allprojects {
    repositories {
        google()
        jcenter()
    }
}

1
你可以降级版本,有时目标版本在仓库中不可用。 使用IDE-UI选择最新版本:
1. 右键单击项目文件夹,选择“打开模块设置”; 2. 在依赖项中查找所需包,例如 enter image description here
这样,您就永远不会得到一个不可用的版本 (当然也要记得在build.gradle中设置正确的仓库)。

0
请使用这些版本。
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接