android - UnknownPluginException 使用 Google Play 服务和插件 DSL

标签 android android-studio gradle build.gradle

我正在 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 {} 的插件 block 导致错误。我发现的另一种方法是:

  • 首先,在你的根构建文件中(不是 app 文件夹中的那个),在 buildscript {} 里面 block ,添加这个
  • buildscript {
     repositories {
       google()
       mavenCentral()
     }
     dependencies {
       classpath 'com.google.gms:google-services:4.3.10'
     }
    }
    
  • 在 app 文件夹的 build 文件中,像这样应用插件,
  • apply plugin: 'com.google.gms.google-services'
    
    在步骤 1 中,使用 mavenCentral()是必需的,因为 google-services插件下载链接的依赖项,如 gson来自 Maven 中心 :)

    关于android - UnknownPluginException 使用 Google Play 服务和插件 DSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69701090/

    相关文章:

    java - 寻找范围的最大值

    java - 警告 : The following options were not recognized by any processor: '[room.schemaLocation]'

    android - Android:在从Android Studio访问之前,gradle无法正常工作

    android - 更改网络时接收 NETWORK_STATE_CHANGED_ACTION 已连接

    移动品牌中的 Android 外部存储

    android - 在 Android Studio 中不复制空行的能力/设置

    java - JBoss EAP 7 java.lang.IllegalArgumentException 通过 Gradle 脚本配置 JBoss EAP 7 数据源的问题

    java - Android:安装 APK 时出错

    android - 如何在Android上 "share"一个文件?

    java - "No SLF4J providers were found"和 "slf4j-api"已经导入了,如何解决 "slf4j-simple"?