在我的
我们还有手动管理的JAR包在
无论如何,由于某些原因,获取依赖项失败了:
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')
}
}
build.gradle
文件出现在resolution
后,所以感到困惑。 - Peter Niederwieser