Java 无法解决:错误:(23, 17) junit:junit:4.12 in android studio 1.4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33782401/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:01:51  来源:igfitidea点击:

Failed to resolve : Error:(23, 17) junit:junit:4.12 in android studio 1.4

javaandroidjunit

提问by maia

I am using Android Studio 1.4 and every time I create a new project same error happen

我使用的是 Android Studio 1.4,每次创建新项目时都会发生同样的错误

Error:(23, 17)"Failed to resolve: junit:junit:4.12".

I read previous post about same problem
Error:(23, 17) Failed to resolve: junit:junit:4.12

我阅读了上一篇关于相同问题的帖子
错误:(23, 17)无法解决:junit:junit:4.12

and done all the the given answer but despite adding URL ('http://repo1.maven.org/maven2' and 'http://jcenter.bintray.com/') for missing repository the error remains

并完成了所有给定的答案,但尽管为缺少存储库添加了 URL(' http://repo1.maven.org/maven2' 和 ' http://jcenter.bintray.com/'),错误仍然存​​在

here is my latest build.gradle code

这是我最新的 build.gradle 代码

apply plugin: 'com.android.application'

android {
  compileSdkVersion 14
  buildToolsVersion "23.0.1"

  defaultConfig {
    applicationId "com.example.myapplication"
    minSdkVersion 14
    targetSdkVersion 14
    versionCode 1
    versionName "1.0"
  }
  buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:22.+'
}

采纳答案by Nitro

If you are adding them to buildscript you will get an error.

如果您将它们添加到 buildscript,您将收到错误消息。

It is a difference between the buildscript repository and the repositories for the dependencies. The buildscript is used for gradle build dependencies.

这是 buildscript 存储库和依赖项存储库之间的区别。buildscript 用于 gradle 构建依赖项。

You will need to add the following to your build.gradle file and it should work fine.

您需要将以下内容添加到 build.gradle 文件中,它应该可以正常工作。

repositories {
    maven { url 'http://repo1.maven.org/maven2' }
    jcenter { url "http://jcenter.bintray.com/" }
}

Following should work:

以下应该工作:

apply plugin: 'com.android.application'

repositories {
    maven { url 'http://repo1.maven.org/maven2' }
    jcenter { url "http://jcenter.bintray.com/" }
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.application"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}

回答by electronix384128

For me it was that allprojects section was missing in the top level gradle build file:

对我来说,顶级 gradle 构建文件中缺少 allprojects 部分:

buildscript {
    [...]
}

allprojects { // <-- required!!!
    repositories {
        jcenter()
    }
}

回答by Omid Rostami

  1. go to the android studio libs directory (C:\Program Files\Android\Android Studio\lib)
  2. find junit-4.12.jarfile
  3. copy the jar file to your project libs folder (myproject\app\libs)
  4. open android studio and delete(comment) this line (testCompile 'junit:junit:4.12')in build.gradlefile
  5. in android studio open (libs) directory and right click the jar file then choose add as a library

  6. just build your project (for more info see below picture)

  1. 转到android studio libs目录 (C:\Program Files\Android\Android Studio\lib)
  2. 查找junit-4.12.jar文件
  3. 将 jar 文件复制到您的项目 libs 文件夹中 (myproject\app\libs)
  4. 打开Android工作室和删除(注释)这一行 (testCompile 'junit:junit:4.12')build.gradle文件
  5. 在android studio中打开(libs)目录并右键单击jar文件然后选择 add as a library

  6. 只需构建您的项目(有关更多信息,请参见下图)

solved Error:(23, 17) Failed to resolve: junit:junit:4.12

已解决错误:(23, 17) 无法解决:junit:junit:4.12