13

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.+'
}
Community
  • 1
  • 1
maia
  • 302
  • 1
  • 4
  • 12

3 Answers3

28

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'
}
Nitro
  • 624
  • 8
  • 21
  • well i add it the same way you said but the error still remain – maia Nov 19 '15 at 05:07
  • Could you run command "gradle --info build" or "./gradlew --info build" in terminal in the folder where your build.gradle file is located and see if that lists any error when adding the repository? When you added the the urls to the repositories ('http://repo1.maven.org/maven2' and 'http://jcenter.bintray.com/') you added them in the same way: repositories { maven { url 'http://repo1.maven.org/maven2' } jcenter { url "http://jcenter.bintray.com/" } } – Nitro Nov 19 '15 at 08:21
  • i use windows 7 so when you say run in terminal do you mean in windows command prompt and yes add the those URL the same way,by the way after your first answer i remove them do i still need to add them – maia Nov 19 '15 at 08:40
  • 2
    Okey, you should be able to run gradlew --info build from the windows terminal, it was just to try to see if there is any specific errors. I have edited my answer to include the repositories, although I suspect there to be some proxy problems with firewalls or similar so that you can't reach the repositories. – Nitro Nov 19 '15 at 08:44
7

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

buildscript {
    [...]
}

allprojects { // <-- required!!!
    repositories {
        jcenter()
    }
}
electronix384128
  • 6,625
  • 11
  • 45
  • 67
  • 1
    Great answer! I searched everywhere and this was the only thing that worked. In my case it is an exported project from `Eclipse` so there is just one `build.gradle` file in the whole project. – yshahak Feb 09 '17 at 05:53
  • You really saved my day! – Ryan Hoo Mar 08 '17 at 17:11
4
  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

Omid Rostami
  • 524
  • 6
  • 10