android - UnknownPluginException 使用 Google Play 服务和插件 DSL
问题描述
我正在 Android Studio Bumblebee 中创建一个新应用程序,默认使用新的 Groovy DSL 插件管理settings.gradle
。
我需要能够使用 Google Play 服务来启用 Firebase 功能,但是在com.google.gms.google-play-services
使用此处的文档应用插件时遇到构建错误:Google Play 服务自述文件
我已将以下内容添加到我的settings.gradle
文件中:
pluginManagement {
repositories {
gradlePluginPortal()
google()
}
plugins {
id 'com.android.application' version '7.1.0-alpha13'
id 'com.android.library' version '7.1.0-alpha13'
id 'org.jetbrains.kotlin.android' version '1.5.31'
id 'com.google.gms.google-services' version '4.3.10'
}
}
以及我的应用程序build.gradle
文件中的以下内容:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.google.gms.google-services'
}
但是,当我构建应用程序时,我得到以下信息UnknownPluginException
:
Plugin [id: 'com.google.gms.google-services', version: '4.3.10'] was not found in any of the following sources:
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'com.google.gms.google-services', version: '4.3.10'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.google.gms.google-services:com.google.gms.google-services.gradle.plugin:4.3.10')
Searched in the following repositories:
Gradle Central Plugin Repository
Google
我也尝试过classpath
etc 的传统方法,但这会导致关于依赖关系解析的错误消息更长。
我不确定我做错了什么。
解决方案
将google-services
插件添加到plugins {}
块中会导致错误。我发现的另一种方法是:
- 首先,在你的根构建文件(不是 app 文件夹中的那个)的
buildscript {}
块内,添加这个
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.10'
}
}
- 在 app 文件夹的构建文件中,像这样应用插件,
apply plugin: 'com.google.gms.google-services'
在第 1 步中,使用mavenCentral()
是必要的,因为google-services
插件会从 maven Central 下载链接的依赖gson
项:)