发现不支持的Gradle DSL方法:'compile()'!

Ger*_*ard 10 gradle android-studio

我将在Android Studio中浏览Google的"将Google Play服务添加到您的项目"文档:https: //developer.android.com/google/play-services/setup.html

我正在使用该文档来修改新创建的Android项目的build.gradle文件.在第2步(将Google Play服务添加到您的项目中)中,它指出:

添加此行:

apply plugin: 'android'
Run Code Online (Sandbox Code Playgroud)

在依赖项下,添加以下内容:

compile 'com.google.android.gms:play-services:5.0.77'
Run Code Online (Sandbox Code Playgroud)

它还表示在更新Google Play服务后更新该版本,根据Android SDK Manager,该服务目前为18.

这是我在顶层的整个build.gradle文件(此文件的父文件是根文件夹).

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply plugin: 'android'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        compile 'com.google.android.gms:play-services:18'

        // 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 script error, unsupported Gradle DSL method found: 'compile()'!

Error:(10, 0) Possible causes could be:
              - you are using a Gradle version where the method is absent
              - you didn't apply Gradle plugin which provides the method
              - or there is a mistake in a build script
Run Code Online (Sandbox Code Playgroud)

我正在使用Android Studio 0.8.2.我没有安装Gradle,只使用Android Studio附带的插件.

有趣的是,我在制作这个新项目时生成的build.gradle文件说:

//NOTE: Do not place your application dependencies here
Run Code Online (Sandbox Code Playgroud)

但Google的文档说(与上述内容有冲突):

Note: Android Studio projects contain a top-level build.gradle file and a build.gradle
      file for each module. Be sure to edit the file for your application module.
Run Code Online (Sandbox Code Playgroud)

我的build.gradle文件(或环境)出了什么问题?

Sco*_*rta 17

您引用的Google文档是正确的,并且不会发生冲突.有多个build.gradle文件.不要像现在那样将依赖项放在顶层,而是将它们放在模块目录中的构建文件中.

另外,不要apply plugin: 'android'在该顶级构建文件中放置语句; 它会导致错误.

您还可以通过项目结构UI添加依赖项,这样做是正确的.


小智 8

不要通过编辑最"外部"的build.gradle(YourProject/build.gradle)来在项目中添加依赖.编辑"app"模块下的那个(YourProject/app/build.gradle).

顺便说一下,你会发现一个依赖的声明,例如:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
Run Code Online (Sandbox Code Playgroud)

这个块将在android {...}配置块下面.在我的情况下,我只是添加leeloo依赖项,所以它变成了:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'net.smartam.leeloo:oauth2-client:0.1'
    compile 'net.smartam.leeloo:oauth2-common:0.1'
}
Run Code Online (Sandbox Code Playgroud)

然后同步您的项目和依赖项将被下载.希望能帮助到你!


JBa*_*uch 1

编译时依赖项应位于dependencies下的块中allprojects,而不是位于 下buildscript

apply plugin: 'android'

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}

allprojects {
    repositories {
        jcenter()
    }
    dependencies {
        compile 'com.google.android.gms:play-services:18'
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该可以正常工作。