找不到Gradle DSL方法:“ compile()”


78

我有此gradle错误。

错误:(9,0)未找到Gradle DSL方法:“ compile()”

我曾尝试参考类似的问题,但没有成功。

Android gradle build错误:(9,0)未找到Gradle DSL方法:“ compile()”。

同步Build.Gradle时出现错误“找不到Gradle DSL方法:'compile()'”

找到了不受支持的Gradle DSL方法:“ compile()”!

我的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'
}

我的代码有什么问题?

对不起我的英语不好。

非常感谢!


Answers:


111

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

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

删除该gradle文件中的2条compile语句:

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.+'
}

如果我的android项目没有模块怎么办?
arun8

我使用com.android.support:support-compat:23.0.0 bt,它是com.android.support:support-v4:23.0.0
10101010

36

我正在使用基于IntelliJ Idea的Android Studio,并且更改了何时以及如何包装代码的设置。我也有“自动重新格式化”选项,这导致随机格式化Gradle文件。因此,它导致如下所示:

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

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


这是为什么会发生此错误的另一种可能性@Drew
Bato-Bair Tsyrenov

好的,有点像是一个问题。不想得到它删除
德鲁

1
谢谢,我才发现IDEA确实重新格式化了Gradle文件的格式,并合二为一。您的回答对我有用。
speedcell4 2016年

一些额外的细节将在这里很方便
Chris Nevill

@ChrisNevill是什么意思?
巴托·拜尔

31

这确实很愚蠢,我得到了解决方案:

编译语句合而为一

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"

希望对您有帮助。谢谢。


正是因为某些原因,某些事情使我的编译语句
陷入了困境


我已经成功地将“更改为”
克里斯·内维尔

我在使用字符串的地方做了一些成功:compile myString +''(这是两个空逗号)
Chris Nevill

面临同样的问题,这就是解决方案!
dmSherazi

8

检查您的build.gradle文件,有时当您使用项目设置将模块添加到当前模块时,当前模块的build.gradle损坏,并且缩进被破坏,只需检查当前build.gradle并检查是否所有编译语句换行!


这正是我在“模块设置”中更新“签名”配置时发生的情况。
瓦希卜·乌尔·哈克


2

检查您的项目。有2个build.gradle。

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


0

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


0

一个简单的错误也可能导致此问题,不要包括classpathbuild.gradle(Module:app)


0

就我而言,出现此错误是因为我在dependencies块外有一个依赖关系实现行。像这样:

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

implementation 'androidx.media:media:1.1.0'

注意,所有实现调用必须在依赖关系块中定义。那是简单的解决方法。

我希望这可以帮助某个人。

编码愉快!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.