如何将依赖项添加到项目的顶级build.gradle文件中?

时间:2015-08-20 08:32:44

标签: android android-studio gradle dependencies google-cloud-messaging

我正在尝试实施 Google Cloud Messaging(GCM)的示例代码,该代码由Google自己提供,GCM Demo

我不知道我究竟能添加依赖项

  • 列表项
  • 将依赖项添加到项目的顶级build.gradle:
  • 当我进入项目结构>模块>单击添加然后选择模块依赖关系,然后会出现一个弹出屏幕,表示找不到依赖的模块。 如果有另一种方法我可以添加依赖项,那么将会感谢帮助。我附上了关于相同的图像。

enter image description here

1 个答案:

答案 0 :(得分:14)

link中所述。

将依赖项添加到项目的顶级build.gradle :(只需编辑文件)

classpath 'com.google.gms:google-services:1.3.0-beta1'

你应该像这样闷闷不乐:

//顶级构建文件,您可以在其中添加所有子项目/模块共有的配置选项。

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.google.gms:google-services:1.3.0-beta1'

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

然后将插件添加到您的应用级build.gradle(只需编辑文件):

apply plugin: 'com.google.gms.google-services'

你应该像这样闷闷不乐:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
  //..
}

最后在您的应用级build.gradle

中添加相关性
dependencies {
  compile "com.google.android.gms:play-services:7.5.0"
}