简体   繁体   English

在Gradle中添加依赖项

[英]Adding dependencies in Gradle

I am trying to move my Android project form IDEA to Android Studio with Gradle. 我正在尝试将我的Android项目从IDEA迁移到带有Gradle的Android Studio。 However, I am having difficulties with the dependencies. 但是,我在依赖方面遇到困难。 I removed my "lib" dir, so the jars would be retrieved from maven. 我删除了我的“ lib”目录,因此可以从maven中检索jar。 But how do I correctly add them? 但是如何正确添加它们?

Eg org.apache.commons.lang3 例如org.apache.commons.lang3

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

dependencies {
    compile 'com.google.android.gms:play-services:4.1.+'
    compile 'org.apache.commons:commons-lang3:2.6.+'
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    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')
    }
}

This results in: 结果是:

Failed to refresh Gradle project 'idoms-android'
         Could not find commons:commons-lang3:3.0.

It seems to be in the Maven Repository though. 它似乎在Maven仓库中。

You are missing this at the top level (ie, a peer of android and dependencies ): 您在最高级别上缺少此功能(即androiddependencies ):

repositories {
    mavenCentral()
}

The buildscript block has repositories and dependencies for the build process . buildscript块具有构建过程的 repositoriesdependencies You also need repositories and dependencies at the top level for the dependencies for your project itself. 您还需要在顶层为项目本身的依赖项提供repositoriesdependencies

dependencies {
    compile 'org.apache.commons:commons-lang3:3.4'
}

Add above line in your build.gradle file, this is the latest version. 在build.gradle文件中添加以上行,这是最新版本。

Also you can check it from here: 您也可以从这里检查它:

mvnrepository, Apache Commons Lang » 3.4 mvnrepository,Apache Commons Lang»3.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM