首页 > 解决方案 > 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

我也尝试过classpathetc 的传统方法,但这会导致关于依赖关系解析的错误消息更长。

我不确定我做错了什么。

标签: androidandroid-studiogradlebuild.gradle

解决方案


google-services插件添加到plugins {}块中会导致错误。我发现的另一种方法是:

  1. 首先,在你的根构建文件(不是 app 文件夹中的那个)的buildscript {}块内,添加这个
buildscript {
 repositories {
   google()
   mavenCentral()
 }
 dependencies {
   classpath 'com.google.gms:google-services:4.3.10'
 }
}
  1. 在 app 文件夹的构建文件中,像这样应用插件,
apply plugin: 'com.google.gms.google-services'

在第 1 步中,使用mavenCentral()是必要的,因为google-services插件会从 maven Central 下载链接的依赖gson项:)


推荐阅读