未找到 Gradle DSL 方法: ‘ edit()’

我有一个级别错误。

错误: (9,0)未找到 Gradle DSL 方法: ‘ edit ()’

我曾试图提到类似的问题,但没有工作。

Android Gradle build 错误: (9,0) Gradle DSL 方法未找到: 编译() & # 39;。

获取错误“未找到 Gradle DSL 方法: 编译() & # 39;”当同步构建

找到不支持的 Gradle DSL 方法: 编译() & # 39; !

我的 build.gradle密码在这里

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:6.5.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}


allprojects {
repositories {
jcenter()
}
}

build.gradle(Module.app)

apply plugin: 'com.android.application'


android {
compileSdkVersion 21
buildToolsVersion "21.1.1"


defaultConfig {
applicationId "com.example.simplemaker.pushtest"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}

我的代码怎么了?

93232 次浏览

正如项目 build.gradle文件中的注释所示:

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

删除该 gradle 文件中的2个编译语句:

compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:6.5.+'

然后将其他(模块)的 build.gradle依赖项设置为如下:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.+'
}

我正在使用基于 IntelliJ 创意的 Android 工作室,我已经更改了关于何时以及如何包装代码的设置。我还有“自动重新格式化”选项,可以随机格式化 Gradle 文件。所以它导致了这样的结果:

    compile 'de.greenrobot:eventbus:2.4.0' compile 'com.jakewharton:butterknife:7.0.1'

然后,Gradle 无法找到第二次编译的编译() ,因为每行只允许编写一个依赖项。

检查您的 建造,分级文件,有时当您添加一个模块到您的当前模块使用项目设置,当前模块的 建造,分级已损坏,并缩进被破坏,只要检查当前的 建造,分级和检查是否所有的编译语句发布在一个新的一行!

检查你的项目。 有两个建筑,等级。

将编译行移到另一个 build.gradle

这个问题真的很愚蠢,我找到了解决办法:

作为编译语句放在一行中

compile "com.squareup.okhttp3:okhttp:$okHttpVersion"    compile "com.squareup.okhttp3:okhttp-urlconnection:$okHttpVersion"    compile "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"    compile "com.google.code.gson:gson:$gsonVersion"

我只是换了下一行,解决了我的问题:

compile "com.squareup.okhttp3:okhttp:$okHttpVersion"
compile "com.squareup.okhttp3:okhttp-urlconnection:$okHttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"
compile "com.google.code.gson:gson:$gsonVersion"

希望对你有帮助,谢谢。

除了上文已经给出的适当答复外,还有一张图片可以更详细地解释:

enter image description here

项目有两个 build.gradle。你需要突出显示的那个: Gradle (模块: app)

应用程序依赖项必须包含在 App-> build.gradle文件中,而不能包含在 Project-> build.gradle 文件中。

一个简单的错误也会导致这个问题,不要包括 classpath依赖于 build.gradle(Module:app)

在我的例子中,我得到这个错误是因为我在 dependencies块之外有一个依赖项实现行。就像这样:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
...
}


implementation 'androidx.media:media:1.1.0'

注意,所有的实现调用都必须在依赖块中定义。 这是一个简单的修复。

我希望这能帮到别人。

编程快乐!