出现错误“未找到 Gradle DSL 方法:'compile()'

Getting Error quot;Gradle DSL method not found: #39;compile()#39;quot; when Syncing Build.Gradle(出现错误“未找到 Gradle DSL 方法:compile()同步 Build.Gradle 时)

本文介绍了出现错误“未找到 Gradle DSL 方法:'compile()'"同步 Build.Gradle 时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加 V4支持库到 android studio,我遵循了这个文档:https://developer.android.com/tools/support-library/setup.html#libs-without-res 但我得到一个错误.这就是我所做的

To add V4 support libraries to android studio, i followed this document:https://developer.android.com/tools/support-library/setup.html#libs-without-res but I get an error. Here is what i did

  1. SDK manager> 已安装 Android 支持库和 Android 存储库.
  2. 转到 Build.Gradle 并添加文档中给出的行.Build.Gradle 现在看起来像这样:

//顶级构建文件,您可以在其中添加所有子项目/模块通用的配置选项.

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()
    dependencies {

        compile "com.android.support:support-v4:18.0.+"
    }
}

}

然后,我收到一个提示我同步 gradle 的弹出窗口.当我同步 Gradle 时,我收到此错误:

Then, I get a popup that suggest that I sync gradle. When i sync Gradle, i get this error:

错误:(20, 0) Gradle DSL 方法未找到:'compile()'可能的原因:

  • 项目staymax"可能正在使用不包含该方法的 Gradle 版本.打开 Gradle 包装文件
  • 构建文件可能缺少 Gradle 插件.应用 Gradle 插件
  • Error:(20, 0) Gradle DSL method not found: 'compile()' Possible causes:

  • The project 'staymax' 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
  • 我是否缺少任何步骤?请建议.

    Am i missing any step? Please suggest.

    Build.Gradle(应用程序)

    Build.Gradle(app)

    apply plugin: 'com.android.application'
    
    android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"
    
    defaultConfig {
        applicationId "com.appt.shreyabisht.staymax"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
     }
    }
    
    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    }
    

    推荐答案

    在几乎所有情况下,您的依赖项都应该放在单个模块的 build.gradle 文件中,而不是放在最顶层的 build.gradle 文件中.在您的情况下,这意味着应该将依赖项添加到 app 模块的 build.gradle 文件中:

    In almost all cases, your dependencies should be put into the individual module's build.gradle files rather than at the top most level build.gradle file. In your case, that means the dependency should be added to the app module's build.gradle file:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile "com.android.support:support-v4:18.0.+"
    }
    

    您应该删除顶层 build.gradle 的整个 allprojects 部分.

    And you should remove the entire allprojects part of the top level build.gradle.

    这篇关于出现错误“未找到 Gradle DSL 方法:'compile()'"同步 Build.Gradle 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:出现错误“未找到 Gradle DSL 方法:'compile()'