我正在查看Android Studio中有关“将Google Play服务添加到您的项目”的Google文档:https://developer.android.com/google/play-services/setup.html
我正在使用该文档来修改一个新创建的Android项目的build.gradle文件。在步骤2(将Google Play服务添加到您的项目)中,它声明:
添加此行:
apply plugin: 'android'
在“依赖项”下,添加以下内容:
compile 'com.google.android.gms:play-services:5.0.77'
它还说,更新Google Play服务后,更新该版本,这是现在在18根据Android SDK管理器。
这是我的整个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()
}
}
保存时,它提示同步。I同步它,但得到:
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
我使用的是Android Studio 0.8.2。我没有安装Gradle,只是使用了Android Studio附带的插件。
有趣的是,我创建这个新项目时生成的build.gradle文件显示:
//NOTE: Do not place your application dependencies here
但谷歌的文档说(这与上述内容相冲突):
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.
我的build.gradle文件(或环境)有什么问题?
5条答案
按热度按时间00jrzges1#
您引用的Google文档是正确的,并且不存在冲突。有多个build.gradle文件。不要像您那样将依赖项放在顶层文件中,而是将它们放在模块目录中的构建文件中。
另外,不要将
apply plugin: 'android'
语句放在顶级构建文件中;则将导致错误。您还可以通过项目结构UI添加依赖关系,这是正确的操作。
t1qtbnec2#
不要通过编辑项目最“外部”的build.gradle(YourProject/build.gradle)在项目中添加依赖项。请改为编辑“app”模块下的依赖项(YourProject/app/build.gradle)。
顺便说一句,您将在其中找到一个依赖项的声明,例如:
这个块将位于**android {...}**配置块的下面。在我的例子中,我只是添加了leeloo依赖项,所以它变成了:
然后同步您的项目和依赖项将被下载。希望它有帮助!
fnx2tebb3#
编译时依赖项应该驻留在
allprojects
下的dependencies
块中,而不是buildscript
下:这样应该没问题。
igetnqfo4#
将“Gradle DSL方法”视为Java方法。因此在Gradle中,方法可以通过{}或.“"来区分。
与相同
其中“依赖”和“编译”都是方法。
因此,您在build.gradle文件中的某个地方包含了一个程序不支持的方法。例如,将依赖项设置为:
这和写作是一样的:
您将看到一条错误消息,显示“发现不支持的Gradle DSL方法:'nothing()'!”显然“nothing”不是一个真实的的方法。我只是编出来的。
因此,您的build.gradle中的一个“compile”方法是错误的。
3vpjnl9f5#
当我面对这个问题时,我使用Android开发者UI导入依赖项,如下所示:-
1进入查看---〉打开模块设置
1.选择“依赖关系”选项卡。单击+添加依赖关系并选择“库依赖关系”。在此处选择下载的库。