找不到Gradle DSL方法:'compile()'

T.A*_*shi 76 android

我有这个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()
    }
}
Run Code Online (Sandbox Code Playgroud)

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'
}
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?

对不起我的英语不好.

非常感谢!

Sim*_*mas 111

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

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Run Code Online (Sandbox Code Playgroud)

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

compile 'com.android.support:appcompat-v7:20.+'
compile 'com.google.android.gms:play-services:6.5.+'
Run Code Online (Sandbox Code Playgroud)

然后让你的其他(模块)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.+'
}
Run Code Online (Sandbox Code Playgroud)

  • @ T.Akashi请将此答案标记为正确答案! (2认同)

Bat*_*nov 36

我正在使用基于IntelliJ Idea的Android工作室,我已经更改了设置何时以及如何包装代码.此外,我还"自动重新格式化"选项,导致随机格式化Gradle文件.所以它导致这样的事情:

    compile 'de.greenrobot:eventbus:2.4.0' compile 'com.jakewharton:butterknife:7.0.1'
Run Code Online (Sandbox Code Playgroud)

Gradle然后找不到第二次编译的compile().因为您只允许每行写一个依赖项.


Pra*_*ani 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"
Run Code Online (Sandbox Code Playgroud)

我刚接下一行改变并解决了我的问题:

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"
Run Code Online (Sandbox Code Playgroud)

希望它能帮到你.谢谢.


Moh*_*eza 8

检查您的build.gradle文件,有时当你使用项目设置模块添加到您当前的模块,该的build.gradle当前模块的损坏,并缩进被打破,只是检查当前的build.gradle和检查所有的编译语句是以新的一行发行的!


Ale*_*riy 6

除了上面已经给出的正确回复之外 - 更详细解释的图片:

在此输入图像描述

项目有2个build.gradle.您需要突出显示的一个:build.gradle(模块:app)