未找到Android Studio Gradle DSL方法:'android()' - 错误(17,0)

Sau*_*ron 80 dsl android android-gradle-plugin

我试图在Android Studio中运行我的项目,但错误如下所示:

在此输入图像描述

我已经跟踪了许多消息来源,只是为了让它运行并在这里结束,但不知道还能做什么.

如何配置此项目以运行?

的build.gradle:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.
    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.9.+'
        }
    }

    allprojects {
        repositories {
            mavenCentral()
        }
    }

    android {
        compileSdkVersion 19
        buildToolsVersion "19.1" 
    }
Run Code Online (Sandbox Code Playgroud)

settings.gradle:

include ':app'
Run Code Online (Sandbox Code Playgroud)

local.properties:

sdk.dir=C\:\\Users\\KJA\\AppData\\Local\\Android\\sdk
Run Code Online (Sandbox Code Playgroud)

gradle.propertes:

# IDE (e.g. Android Studio) users:
# Settings specified in this file will override any Gradle settings
# configured through the IDE.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
Run Code Online (Sandbox Code Playgroud)

Jar*_*ows 56

我继续从您提供的链接下载项目:http://javapapers.com/android/android-chat-bubble/

由于这是一个旧教程,您只需要升级软件,gradle,android构建工具和插件.

确保您拥有最新的Gradle和Android Studio:

的build.gradle:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序/的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 23
        versionCode 1
        versionName '1.0'
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:23.2.1'
}
Run Code Online (Sandbox Code Playgroud)

然后运行gradle:

gradle installDebug
Run Code Online (Sandbox Code Playgroud)


iTa*_*oid 46

在您的顶级,build.gradle您似乎有代码

android {
   compileSdkVersion 19
   buildToolsVersion "19.1" 
}
Run Code Online (Sandbox Code Playgroud)

你不能将这个代码放在顶层,build.gradle因为android build插件还没有加载.您可以在应用程序级别定义编译版本和构建工具版本build.gradle.

  • 精彩的Android Studio 2.2预览随机将其置于顶级build.gradle中 (3认同)

小智 27

由于某些未知原因,Android Studio错误地在顶级build.gradle文件中添加了android()方法.

只需删除该方法,它就适合我.

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'
}
Run Code Online (Sandbox Code Playgroud)

  • 2016年仍然是一个问题 - 并且通过评论该部分仍然可以解决. (3认同)

KOT*_*IOS 14

我试图通过以下步骤管理此问题:

删除android { ... }顶级根gradle文件中的块

调查

 compileSdkVersion 22
 buildToolsVersion "22.0.0"
Run Code Online (Sandbox Code Playgroud)

app/gradle文件中的代码行只有下面的下拉列表中的一个版本应该存在,否则它将提供下载相同的选项.

在此输入图像描述


Shu*_*app 10

当我尝试将Eclipse NDK项目导入Android Studio时,我遇到了同样的错误.事实证明,对于Android Studio中的NDK支持,您需要使用新的gradle和android插件(并且gradle版本为2.5+).这个插件需要更改模块的build.gradle文件.具体来说," android {...}"对象应该在" model {...}"对象里面,如下所示:

apply plugin: 'com.android.model.application'
model {
    android {
    ....
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,如果您已更新gradle配置以使用新的gradle插件和新的android插件,但未更改模块的build.gradle语法,则可能会出现"未找到Gradle DSL方法:'android()'"错误.

我在这里准备了一个补丁文件,在评论中有一些进一步的解释:https: //gist.github.com/shumoapp/91d815de6e01f5921d1f 这些是我将本机音频ndk项目导入Android Studio后我必须做的更改.