【问题标题】:UnknownPluginException using Google Play Services and Plugins DSLUnknownPluginException 使用 Google Play 服务和插件 DSL
【发布时间】:2021-12-10 12:39:27
【问题描述】:

我正在 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 等传统方法,但这会导致关于依赖关系解析的错误消息更长。

我不确定我做错了什么。

【问题讨论】:

    标签: android android-studio gradle build.gradle


    【解决方案1】:

    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 :)

    【讨论】:

    • 感谢@JustinCoding 的回答。我的印象是,为了让 Firebase 工作,它需要是一个插件。将其添加为 implementation 是否允许 Firebase 以相同的方式工作?
    • 嘿,我已经编辑了答案以更好地满足您的需求,请在您的项目中更新相同的答案。而且,此方法仅像插件一样工作:D
    • 看起来这行得通 :) - 上次它引发了大量关于依赖项未解决的问题,但它似乎已经解决了包含 mavenCentral() 的问题。谢谢你:)
    • 不客气,伙计!请将此答案标记为已接受,以便对其他人有所帮助:)
    【解决方案2】:

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

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

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

    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(Module:) 看起来像

    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(Project Settings) 看起来像

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      相关资源
      最近更新 更多