简体   繁体   English

找不到Gradle DSL方法:android()

[英]Gradle DSL method not found: android()

Any suggestions to fix this problem: I am getting this error in android studio after importing Project 任何解决此问题的建议:我在导入Project后在android studio中收到此错误

Error:(16, 0) Gradle DSL method not found: 'android()' Possible causes: 错误:(16,0)未找到Gradle DSL方法:'android()'可能的原因:

  • The project 'LoginActivity' may be using a version of Gradle that does not contain the method. 项目“LoginActivity”可能正在使用不包含该方法的Gradle版本。 Gradle settings Gradle设置
  • The build file may be missing a Gradle plugin. 构建文件可能缺少Gradle插件。 Apply Gradle plugin 应用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:1.1.0'
        }
    }
    allprojects {
        repositories {
            jcenter()
        }
    }
    
    android {
        compileSdkVersion 21
        buildToolsVersion '21.0.0'
        dexOptions {
            incremental true
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_6
            targetCompatibility JavaVersion.VERSION_1_6
        }
    }
    dependencies {
        compile 'com.android.support:support-v4:21.0.3'
        compile 'com.android.support:appcompat-v7:21.0.3'
    }
    apply plugin: 'eclipse'
    apply plugin: 'java'
    

    There is a bar notification which says: Gradle project sync failed. 有一个条形通知说:Gradle项目同步失败。 Basic fuctionality (eg: editing, debugging) will not work properly. 基本功能(例如:编辑,调试)将无法正常工作。

    Please help me to get rid of this problem. 请帮我摆脱这个问题。 Thanks! 谢谢!

    If you have an android block in your build script, you need one of the following two statements, depending on whether your module is an application module or a library module: 如果您的构建脚本中有一个android块,则需要以下两个语句之一,具体取决于您的模块是应用程序模块还是库模块:

    apply plugin: 'com.android.application'
    

    or: 要么:

    apply plugin: 'com.android.library'
    

    Having said that, there are other problems with your build script. 话虽如此,您的构建脚本还存在其他问题。 For one, this is a top-level build script (as you can see by the comment that says "Top-level build file where you can add configuration options..."), so it's likely that there isn't actually a top-level module for the android block to apply to, so adding the apply plugin statement may just lead to a different error. 首先,这是一个顶级构建脚本(您可以通过注释中看到“顶级构建文件,您可以在其中添加配置选项...”),因此很可能实际上没有顶级构建脚本-level模块用于android块的应用,因此添加apply plugin语句可能只会导致不同的错误。 If that happens, remove the android block. 如果发生这种情况,请删除android块。

    Another issue is that you already have a apply plugin: 'java' statement in this build script. 另一个问题是你已经有了一个apply plugin: 'java'这个构建脚本中的apply plugin: 'java'语句。 You can't use both the Java and the Android plugins in the same build script; 您不能在同一构建脚本中同时使用Java和Android插件; they collide with each other. 他们互相碰撞。

    Just delete this method from your root build.gradle 只需从root build.gradle中删除此方法即可

    android {
    compileSdkVersion 21
    buildToolsVersion '21.0.0'
    dexOptions {
        incremental true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    } }
    

    It should work after you compile again. 它再次编译后应该可以工作。

    如果得到错误然后检查您的gradle文件版本并删除其他gradle依赖,如classpath 'com.android.tools.build:gradle:1.5.0' gradle:1.5.0 classpath 'com.android.tools.build:gradle:1.5.0'它可能会解决问题

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM