在build.gradle中添加依赖关系时发生Gradle错误 [英] Gradle errors while adding dependencies in build.gradle
问题描述
我尝试在我的项目中使用下面的代码在我的build.gradle文件中包含卡片库。
buildscript {
repositories {
jcenter()
mavenCentral()
}
依赖关系{
classpath'com.android.tools.build:gradle:1.1。 0'
//注意:不要在这里放置您的应用程序依赖关系;它们属于单个模块build.gradle文件中的
//
}
依赖项{
//核心卡库
编译'com.github.gabrielemariotti.cards: cardslib-core:2.0.1'
//可选内置卡
编译'com.github.gabrielemariotti.cards:cardslib-cards:2.0.1'
//可选RecyclerView
编译'com.github.gabrielemariotti.cards:cardslib-recyclerview:2.0.1'
//交错网格视图支持可选
编译'com.github.gabrielemariotti.cards:cardslib-extra-staggeredgrid:2.0.1'
//可用于拖放支持
编译'com.github.gabrielemariotti.cards :cardslib-extra-dragdrop:2.0.1'
// twowayview支持的可选项(即将推出)
//编译'com.github.gabrielemariotti.cards:cardslib-extra-twoway :2.0.1'
}
}
所有项目{
存储库{
jcenter()
mavenCentral )
}
}
但是编译时,android studio会抛出错误
错误:(23,0)未找到Gradle DSL方法:'compile()'
可能的原因:
打开Gradle包装文件
应用Gradle插件
我猜想是gradle版本的原因,在包含的库中较低。
如何知道我的依赖正在使用的gradle版本以及如何将它们调整到我的项目。
当我想添加库时,maven的库文件在aar文件中,我不认为它会让你知道gradle版本。
感谢您在这方面的任何帮助。
您在错误的地方添加依赖关系。它们应该位于buildscript部分之外,并且位于modules / application build.gradle中。
父级build.gradle。这应该在你的项目的根目录中
buildscript {
pre>
repositories {
jcenter()
mavenCentral()
}
依赖关系{
classpath'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
存储库{
jcenter()
mavenCentral()
}
}
模块build.gradle。这应该在您要添加依赖关系的模块文件夹中。
apply plugin:'com.android .application'
android {
// Android相关设置go here
}
依赖关系{
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'
}
这假定您的项目结构是像
项目
| ___ 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:
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
这篇关于在build.gradle中添加依赖关系时发生Gradle错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!