我尝试使用 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()
}
}
但是在编译时,android studio 会抛出如下错误。
错误:(23, 0) 未找到 Gradle DSL 方法:“compile()” 可能的原因:
我猜测是 gradle 版本的原因,它在我包含的库中较低。 如何知道我的依赖项正在使用的 gradle 版本以及如何将它们调整到我的项目。 当我想添加库时,maven 在 aar 文件中有存储库,我认为它不会让你知道 gradle 版本。 感谢您在这方面的任何帮助。
最佳答案
您在错误的位置添加了依赖项。它们应该位于 buildscript 部分之外并位于模块/应用程序 build.gradle 中。
父 build.gradle。这应该位于您项目的根目录中
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
模块构建.gradle。它应该位于您尝试添加依赖项的模块的文件夹中。
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'
}
这假设您的项目结构类似于
Project
|___build.gradle
|___Module
|____build.gradle
关于android - 在 build.gradle 中添加依赖项时出现 Gradle 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30011099/