在Gradle 7.2中如何添加classpath依赖项?应该在哪里添加?

17

针对使用Gradle 7.2创建的Android项目,项目gradle与之前版本有很大不同,它现在仅包含以下内容:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}

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

settings.gradle文件与以前非常不同,现在它包含以下更多的配置:

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
//        classpath 'com.google.gms:google-services:4.3.10'
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.name = "My App"
include ':app'

我应该在哪里添加 classpath 'com.google.gms:google-services:4.3.10' 的类路径?当我将其添加到 settings.gradle 文件中时,编译失败并出现以下错误:“在类型为 org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler 的仓库容器的 repository container 上未找到方法 classpath() 的参数 [com.google.gms:google-services:4.3.10]”。

4个回答

11

我认为使用Gradle 7.2版本,您不必特别指定类路径,请参考 doc1, doc2。只需在项目级别的build.gradle文件中插入插件ID:

plugins {
    ...
    id "com.google.gms.google-services" version "4.3.10" apply false
}

同时不要忘记将其复制粘贴到应用级别的 build.gradle 文件中:

plugins {
    ...
    id "com.google.gms.google-services"
}

要检查插件是否正确应用,可以从另一个答案中打印可用插件列表。


1

https://docs.gradle.org/7.2/userguide/plugins.html#sec:applying_plugins_buildscript 例子13. 使用buildscript块应用插件

该文档介绍了如何使用Gradle构建工具中的buildscript块来应用插件。通过在buildscript块中指定插件依赖项,您可以在项目中使用插件。这个例子演示了如何使用buildscript块来应用插件。
buildscript {
    repositories {
        gradlePluginPortal()
    }
    dependencies {
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
    }
}

apply plugin: 'com.jfrog.bintray'

1

将您的构建脚本类文件的代码粘贴到项目级别的gradle.build文件中,以下是一个示例:

/**
 * project-level - build.gradle file -- make sure buildscript is before 
 * plugins
**/
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.41'
    }
}

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.1.3' apply false
    id 'com.android.library' version '7.1.3' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.20' apply false
}


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

0

添加如下的setting.gradle文件

   dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven { url "https://jitpack.io" }
        maven { url "https://android-sdk.tapdaq.com" }
    }
    }
    rootProject.name = "App Name"
    include ':app'

buils.gradle(项目级别)

buildscript {
ext.kotlin_version = "1.6.10"
ext.kotlincoroutine_version = '1.5.1'
ext.coroutineadapter_version = '0.9.2'
ext.gradle_version = '3.5.0'
ext.retrofit_version = '2.9.0'
ext.gson_version = '2.8.7'
ext.okhttp_version = '4.9.0'
ext.glide_version = '4.12.0'

repositories {
    google()
    mavenCentral()
}
dependencies {
    classpath "com.android.tools.build:gradle:7.0.4"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接