Failed to resolve : Error:(23, 17) junit:junit:4.1

2019-03-11 04:45发布

问题:

This question already has an answer here:

  • Error:(23, 17) Failed to resolve: junit:junit:4.12 44 answers

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

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

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

here is my latest build.gradle code

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.+'
}

回答1:

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

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

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

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'
}


回答2:

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

buildscript {
    [...]
}

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


回答3:

  1. go to the android studio libs directory (C:\Program Files\Android\Android Studio\lib)
  2. find junit-4.12.jar file
  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.gradle file
  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)

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