出现错误“未找到 Gradle DSL 方法:'compile()'"同步 Build.Gra

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 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
    Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
    On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
    Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
    buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
    How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)