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

标签 android maven android-studio gradle

我尝试使用 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()” 可能的原因:

  • 项目“cardslib_1”可能使用不包含该方法的 Gradle 版本。 打开 Gradle 包装文件
  • 构建文件可能缺少 Gradle 插件。 应用 Gradle 插件
  • 我猜测是 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/

    相关文章:

    android - 将文本从 string.xml 设置为 TextView 不起作用

    android通用景观检查

    java - 使用 JPA、Maven 和 Tomcat,但出现 java.lang.NoClassDefFoundError : javax/persistence/Persistence

    Android Studio 找不到 OpenCV header

    android - Gradle同步问题:错误:原因:无法找到到请求目标的有效证书路径

    android - 清理 Android 截屏视频

    java - 是否可以获取手机当前选择的配置文件?

    unit-testing - 使用 Maven 项目将测试与 src 分开?

    java - 集成 Maven、Tycho 和 Eclipse 时处理非 OSGi 依赖项

    java - 字母顺序不适用于自定义 ListView