且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在build.gradle中添加依赖关系时发生Gradle错误

更新时间:2022-12-13 14:14:31

您在错误的地方添加依赖关系。它们应该位于buildscript部分之外,并且位于modules / application build.gradle中。



父级build.gradle。这应该在你的项目的根目录中

  buildscript {
repositories {
jcenter()
mavenCentral()
}
依赖关系{
classpath'com.android.tools.build:gradle:1.1.0'
}
}

allprojects {
存储库{
jcenter()
mavenCentral()
}
}
pre>

模块build.gradle。这应该在您要添加依赖关系的模块文件夹中。

  apply plugin:'com.android .application'

android {
// Android相关设置go here
}

依赖关系{

compile'c​​om .github.gabrielemariotti.cards:cardslib-core:2.0.1'
compile'c​​om.github.gabrielemariotti.cards:cardslib-cards:2.0.1'
compile'c​​om.github.gabrielemariotti.cards :cardslib-recyclerview:2.0.1'
compile'c​​om.github.gabrielemariotti.cards:cardslib-extra-staggeredgrid:2.0.1'
compile'c​​om.github.gabrielemariotti.cards:cardslib-extra -dragdrop:2.0.1'
}

这假定您的项目结构是像

 项目
| ___ build.gradle
| ___模块
| ____ build.gradle


I've tried to included cards library in my project using the below code in my build.gradle file.

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.

Error:(23, 0) Gradle DSL method not found: 'compile()' Possible causes:

  • The project 'cardslib_1' may be using a version of Gradle that does not contain the method. Open Gradle wrapper file
  • The build file may be missing a Gradle plugin. Apply Gradle plugin
  • I'm guessing the reason to be gradle version, which is lower in libraries I'm including. How to know the gradle version my dependencies are using and how to adjust them to my project. 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. 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.

    Parent 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. 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