Maven 依赖与 Android Studio/Gradle [英] Maven Dependencies with Android Studio / Gradle

查看:33
本文介绍了Maven 依赖与 Android Studio/Gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用与 Android Studio 捆绑的 Gradle 构建系统.到目前为止,我能够使用存储在我的项目结构中的依赖项构建多项目设置.我现在想使用 Maven 依赖项,但无济于事.我写了一个非常简单的 build.gradle 文件,它总是失败:

I am using the Gradle build system bundled with Android Studio. So far, I am able to build multi-project setups using dependencies that are stored in my project structure. I would now like to use a maven dependency, to no avail. I have written a very simple build.gradle file that always fails :

    buildscript {
        repositories {
            maven { url 'http://repo1.maven.org/maven2' }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.4'
        }
    }
    apply plugin: 'android'

    dependencies {
        compile 'com.google.android:support-v4:r7'
    }

    android {
        compileSdkVersion 17
        buildToolsVersion "17.0.0"

        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 16
        }
    }

带有以下消息:

    * What went wrong:
        A problem occurred configuring project ':absabs'.
        > Failed to notify project evaluation listener.
           > Could not resolve all dependencies for configuration ':absabs:compile'.
              > Could not find com.google.android:support-v4:r7.
                Required by:
                    absabs:absabs:unspecified
....
Caused by: org.gradle.api.internal.artifacts.ivyservice.ModuleVersionNotFoundException: Could not find com.google.android:support-v4:r7.

到目前为止我尝试过的任何工件都会发生这种情况.知道出了什么问题吗?

It happens with any artifact I have tried so far. Any idea of what's wrong ?

谢谢

推荐答案

buildscript 部分中的repositories"块仅适用于构建环境.您还需要指定在解析构建项目的依赖项时要使用的存储库,因此您需要在 build.gradle 文件中放置如下内容:

The "repositories" block in the buildscript section only applies to the build environment. You also need to specify which repository to use when resolving dependencies for building your project, so you need to put something like the following in your build.gradle file:

repositories {
    mavenCentral()
}

您的完整文件如下所示:

Your complete file would look something like this:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.android:support-v4:r7'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

注意存储库"的两个实例.您可以在 gradle 文档中详细了解这实际上意味着什么.

Note the two instances of "repositories". You can read more about what this actually means in the gradle docs.

这篇关于Maven 依赖与 Android Studio/Gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆