简体   繁体   English

在build.gradle中添加依赖项时出现Gradle错误

[英]Gradle errors while adding dependencies in build.gradle

I've tried to included cards library in my project using the below code in my build.gradle file. 我尝试使用build.gradle文件中的以下代码在我的项目中包含卡片库。

buildscript {
repositories {
    jcenter()
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.1.0'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
dependencies {
    //Core card library
    compile 'com.github.gabrielemariotti.cards:cardslib-core:2.0.1'

    //Optional for built-in cards
    compile 'com.github.gabrielemariotti.cards:cardslib-cards:2.0.1'

    //Optional for RecyclerView
    compile 'com.github.gabrielemariotti.cards:cardslib-recyclerview:2.0.1'

    //Optional for staggered grid view support
    compile 'com.github.gabrielemariotti.cards:cardslib-extra-staggeredgrid:2.0.1'

    //Optional for drag and drop support
    compile 'com.github.gabrielemariotti.cards:cardslib-extra-dragdrop:2.0.1'

    //Optional for twowayview support (coming soon)
    //compile 'com.github.gabrielemariotti.cards:cardslib-extra-twoway:2.0.1'

  }
 }

allprojects {
repositories {
    jcenter()
    mavenCentral()
}
}

But when compiling, android studio is throwing up errors as below. 但是在编译时,android studio会抛出如下错误。

Error:(23, 0) Gradle DSL method not found: 'compile()' Possible causes: 错误:(23,0)未找到Gradle DSL方法:'compile()'可能的原因:

  • The project 'cardslib_1' may be using a version of Gradle that does not contain the method. 项目'cardslib_1'可能正在使用不包含该方法的Gradle版本。 Open Gradle wrapper file 打开Gradle包装文件
  • The build file may be missing a Gradle plugin. 构建文件可能缺少Gradle插件。 Apply Gradle plugin 应用Gradle插件
  • I'm guessing the reason to be gradle version, which is lower in libraries I'm including. 我猜测是gradle版本的原因,这在我所包含的库中较低。 How to know the gradle version my dependencies are using and how to adjust them to my project. 如何知道我的依赖项正在使用的gradle版本以及如何将它们调整到我的项目中。 When I thought to add the libraries, maven has repositories in aar file which I don't think will let you know the gradle version. 当我想添加库时,maven在aar文件中有存储库,我认为这不会让你知道gradle版本。 Thanks for any help in this regards. 感谢您在这方面的任何帮助。

    You're adding the dependencies in the wrong place. 您将依赖项添加到错误的位置。 They should be outside of the buildscript section and in the modules/applications build.gradle. 它们应该位于buildscript部分之外和modules / applications build.gradle中。

    Parent build.gradle. 父build.gradle。 This should be in the root directory of your project 这应该在项目的根目录中

    buildscript {
        repositories {
            jcenter()
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.1.0'
        }
    }
    
    allprojects {
        repositories {
            jcenter()
            mavenCentral()
        }
    }
    

    Module build.gradle. 模块build.gradle。 This should be in the folder of the module you're trying to add the dependencies to. 这应该在您尝试添加依赖项的模块的文件夹中。

    apply plugin: 'com.android.application'
    
    android {
        // Android related settings go here
    }
    
    dependencies {
    
        compile 'com.github.gabrielemariotti.cards:cardslib-core:2.0.1'
        compile 'com.github.gabrielemariotti.cards:cardslib-cards:2.0.1'
        compile 'com.github.gabrielemariotti.cards:cardslib-recyclerview:2.0.1'
        compile 'com.github.gabrielemariotti.cards:cardslib-extra-staggeredgrid:2.0.1'
        compile 'com.github.gabrielemariotti.cards:cardslib-extra-dragdrop:2.0.1'
    }
    

    This assumes that the structure of your project is something like 这假设你的项目结构是这样的

    Project
    |___build.gradle
    |___Module
        |____build.gradle
    

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

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