Android gradle build错误:(9,0)未找到Gradle DSL方法:'compile()'.

Big*_*337 58 android build gradle

我尝试同步项目时遇到以下构建错误:

Error:(9, 0) Gradle DSL method not found: 'compile()'
Possible causes:The project 'AlexTest' may be using a version of Gradle that does not contain the method.
The build file may be missing a Gradle plugin.
link: Apply Gradle plugin
Run Code Online (Sandbox Code Playgroud)

我已经尝试将他们链接到我的每一个gradle插件应用到底部的那个链接,但同样的问题,所以我得出结论,第一个错误是原因.

这是AlexTest的build.gradle文件(项目目录):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.13.2'
        compile 'com.google.android.gms:play-services:6.1.11'
        // 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)

我认为这是它遇到问题的gradle文件.但我不确定它指的是什么方法.

这里还有gradle-wrapper.properties,它也提到了:

#Mon Nov 10 01:06:12 PST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip
Run Code Online (Sandbox Code Playgroud)

或许distributionUrl中的gradle版本需要匹配依赖项中的gradle版本?

我也有应用程序目录本身的build.gradle文件 - 低1级,但我不认为这是它指的是,但在这里它是:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.snappiesticker.alextest"
        minSdkVersion 16
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:6.1.+'
}
Run Code Online (Sandbox Code Playgroud)

Com*_*are 78

我已经尝试将他们链接到我的每一个gradle插件应用到底部的那个链接,但同样的问题,所以我得出结论,第一个错误是原因.

正确.

这是AlexTest的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)

compile 'com.google.android.gms:play-services:6.1.11'从该文件中删除该行.将compile 'com.google.android.gms:play-services:6.1.+'您拥有的内容保留在其他build.gradle文件中.

dependencies封闭物buildscript仅适用于摇篮插件.dependencies模块build.gradle文件中的顶级闭包用于应用程序依赖性.

  • @elron:如果它让你感觉更好,IIRC,Gradle插件系统正在进行大修,所以我希望最终将修复双"build脚本"系统.向人们解释是非常困惑的,"是的,你必须修改`buildscript`,但不仅仅是*任何*`buildscript`,而是*right*`buildscript`. (2认同)

Ant*_*nde 13

Saw报告说问题是由于其他原因而发生的,但这对我来说已经解决了.

如果您在项目结构(Android Studio)中执行任何更改并按OK,Gradle将被同步,但这种类型的同步,如果依赖块具有类似的内容:

同步后,此块将保持这种方式:

解决此问题的一种方法是插入换行符以保持同步之前或在两个语句之间放置一个分号.

我希望它有所帮助.