错误: 无法解决依赖项':app@debugAndroidTest/compileClasspath':无法解析 junit:junit:4.12。

6

我创建了一个新的Android项目,但在构建时出现错误。

我尝试了现有的解决方案:

以下是错误信息:

Error:Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve junit:junit:4.12.

Error:Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Could not resolve junit:junit:4.12.

Error:Unable to resolve dependency for ':app@debugUnitTest/compileClasspath': Could not resolve junit:junit:4.12.

Error:Unable to resolve dependency for ':app@debugAndroidTest/compileClasspath': Could not resolve com.google.code.findbugs:jsr305:2.0.1.

这是我的gradle文件中的依赖部分:

 dependencies 
    {
       implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'

    }

这是我的第一个项目,我不太理解。

我的项目 gradle 是:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

我的应用gradle文件如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 28
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
android
        {
            configurations.all
                    {
                        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
                    }
        }

dependencies
        {
    implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'


}

只需删除与测试编译器相关的所有内容。 - Ghasem
1
我已经尝试了,但仍然出现了这些错误 @AlexJolig - Aman
2个回答

3

在您的build.gradle文件中添加以下存储库:

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

你忘记将这个添加到依赖项中了

androidTestCompile 'com.google.code.findbugs:jsr305:3.0.0' 

这个问题是由espresso库引起的,如果你无法删除它,请在你的应用gradle中添加以下内容。
android
{ 
 configurations.all 
  { 
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9' 
  }
}

评论不适合进行长时间的讨论;此对话已被移至聊天室 - Bhargav Rao
添加Maven解决了我的问题。奇怪的是,这个问题出现在Android Studio自带的模板项目的第一次构建期间。 - Hritik

1
我试图混合使用AppCompat和Jetpack依赖项,但这是不允许的。
我认为我可能没有正确设置Jetpack库的存储库(所以它找不到任何Jetpack库)。
不要使用:implementation 'androidx.appcompat:appcompat:1.0.2'。(这是Jetpack)--请使用此类的AppCompat版本(另外,Jetpack与早于28.x的Appcompat版本不一致)。总之,摆脱任何带有(androidx)的东西。
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
}

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