无法解决所有依赖项:来自Maven Central的第三方库。

3
在我的build.gradle文件中,我定义了一些第三方库,这些库都可以在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'
}

我们还有手动管理的JAR包在libs目录下(适用于Eclipse用户),我曾经在Gradle构建中也使用过这些JAR包,方法是使用compile fileTree(dir: 'libs', include: '*.jar')。但是,我正试图摆脱这种方式,转向更简单、自动化的依赖管理。
无论如何,由于某些原因,获取依赖项失败了:
* 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

我做错了什么?

我在想是否应该有

repositories {
    mavenCentral()
}

...还要在构建文件的顶层(不仅是在buildscript内部)添加,但是这并没有帮助解决问题。

解决方案

我太着急了;那确实帮助解决了“无法解析依赖项”的问题。之后我遇到了编译错误,但实际上是因为代码和libs中有更多的依赖项(超过了上面dependendies的四个条目)。

所以在我的情况下,我必须添加顶级repositories指向Maven中央库,以及一些我们实际需要的其他依赖项:

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'

原始的、有问题的 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')
    }

}

顶层仓库声明在哪里? - Peter Niederwieser
@Peter:如果你读完整个问题,特别是“分辨率”部分以及答案和评论,应该就很清楚了。 - Jonik
因为原始、不完整的 build.gradle 文件出现在 resolution 后,所以感到困惑。 - Peter Niederwieser
2个回答

19

实际上,您需要添加一个

repositories {
    mavenCentral()
}

在您的build.gradle的顶层指定搜索依赖项的位置。


很难相信这不会有所帮助。尝试使用 --refresh-dependencies - Peter Niederwieser
刚刚尝试了一下我的一个项目,经过 gradlew clean build 后如预期般工作正常。 - MagicMicky
啊,等等!我用那个会得到不同的错误:编译不起作用。所以我想我必须将获取的JAR文件添加到类路径中...你知道怎么做吗(请参见我的build.gradle)? - Jonik
我的错:编译错误是关于我在“libs”目录中有但没有包含在“build.gradle”中的不同依赖项(例如OkHttp)。我只需要添加它们。谢谢并请原谅。 - Jonik
@Jonik 我遇到了同样的问题。在我的情况下,我不小心注释掉了必要的行。对于像我和你一样在libs目录中有依赖项的人,请将以下内容添加到您的build.gradle文件中:repositories { flatDir { dirs 'libs' } } - riper

0
buildscript {
    repositories {
        google()
=>        mavenCentral()     <=

    }

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