UnknownPluginException 使用 Google Play 服务和插件 DSL

UnknownPluginException using Google Play Services and Plugins DSL

我正在 Android Studio Bumblebee 中创建一个新应用程序,这默认使用 settings.gradle 中新的 Groovy DSL 插件管理。

我需要能够使用 Google Play 服务来启用 Firebase 功能,但是在使用此处的文档应用 com.google.gms.google-play-services 插件时,我 运行 遇到了构建错误: Google Play Services Readme

我已将以下内容添加到我的 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 等遗留方法,但这会导致有关依赖项解析的错误消息更长。

我不确定我做错了什么。

google-services 插件添加到 plugins {} 块导致错误。我发现的替代方法是:

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

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

通过使用 Android Studio Bumblebee 新 Groovy DSL 插件管理 在 build.gradle(Project:) plugins{} 部分添加下行并同步项目将解决您的问题。

id 'com.google.gms.google-services' version '4.3.10' apply false

你的build.gradle(项目:)看起来像

plugins {
    id 'com.android.application' version '7.1.1' apply false
    id 'com.android.library' version '7.1.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
    id 'com.google.dagger.hilt.android' version '2.41' apply false
    id 'com.google.gms.google-services' version '4.3.10' apply false
    id 'com.google.firebase.crashlytics' version '2.8.1' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

你的build.gradle(模块:)看起来像

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.kapt'
    id 'com.google.dagger.hilt.android'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
}

android {
    compileSdk 32

    signingConfigs {
        config {
            enableV3Signing true
            enableV4Signing true
        }
    }
}

你的settings.gradle(项目设置)看起来像

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "MyApp"
include ':app'