“Could not resolve all dependencies” with 3rd party libs (from Maven Central)

In my build.gradle, I define some 3rd party libs, all of which are available in Maven Central.

dependencies {    
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.google.guava:guava:15.0'
    compile 'com.netflix.rxjava:rxjava-core:0.14.2'
    compile 'com.netflix.rxjava:rxjava-android:0.14.2'
}

(We also have manually managed jars under libs (for Eclipse users), and I used to use those for Gradle builds too, with compile fileTree(dir: 'libs', include: '*.jar'), but I’m trying to move away from that towards simpler, automated dependency management.)

Anyway, for some reason fetching the deps fails:

* What went wrong:
A problem occurred configuring root project 'MyApp-Android'.
> Could not resolve all dependencies for configuration ':_debugCompile'.
   > Could not find com.google.code.gson:gson:2.2.4.
     Required by:
         :MyApp-Android:unspecified
   > Could not find com.google.guava:guava:15.0.
     Required by:
         :MyApp-Android:unspecified
   > Could not find com.netflix.rxjava:rxjava-core:0.14.2.
     Required by:
         :MyApp-Android:unspecified
   > Could not find com.netflix.rxjava:rxjava-android:0.14.2.
     Required by:
         :MyApp-Android:unspecified

What am I doing wrong?

I was wondering if I should have

repositories {
    mavenCentral()
}

…also on the top level of the build file (not just inside buildscript), but adding that didn’t help.

Resolution

I was too hasty; that did help with “Could not resolve dependencies”. After that I got compilation errors, but that was due to in fact having some more dependendies in the code (and in libs) than the four entries in dependendies above.

So in my case I had to add the top-level repositories pointing to Maven Central plus some additional dependiencies that we actually had:

compile 'com.squareup.okhttp:okhttp:1.2.1'   
compile 'com.android.support:support-v4:13.0.0'
compile 'com.android.support:support-v13:13.0.0'

The (original, broken) full build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.1'
    }
}
apply plugin: 'android'

dependencies {
    // compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.google.guava:guava:15.0'
    compile 'com.netflix.rxjava:rxjava-core:0.14.2'
    compile 'com.netflix.rxjava:rxjava-android:0.14.2'
}

android {
    compileSdkVersion 19
    buildToolsVersion "19"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

}

Answer

Indeed you need to add a

repositories {
    mavenCentral()
}

at the top level of your build.gradle to specify where to search for your dependencies.