在 gradle 7.2 中在哪里以及如何添加类路径依赖项?

s-h*_*ter 19 android gradle

对于使用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
}
Run Code Online (Sandbox Code Playgroud)

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'
Run Code Online (Sandbox Code Playgroud)

在哪里可以添加类路径classpath 'com.google.gms:google-services:4.3.10'?当我将其添加到 settings.gradle 时,它​​无法编译并出现错误:“在 org.gradle 类型的存储库容器上找不到参数 [com.google.gms:google-services:4.3.10] 的方法 classpath() .api.internal.artifacts.dsl.DefaultRepositoryHandler”

Sky*_*Sky 12

我认为使用 gradle 7.2 版本,您实际上不需要指定类路径,请检查doc1doc2。只需在项目级别 build.gradle 上插入插件 id:

plugins {
    ...
    id "com.google.gms.google-services" version "4.3.10" apply false
}
Run Code Online (Sandbox Code Playgroud)

并且不要忘记将其复制粘贴到应用程序级别 build.gradle 上:

plugins {
    ...
    id "com.google.gms.google-services"
}
Run Code Online (Sandbox Code Playgroud)

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


Jay*_*ara 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'
Run Code Online (Sandbox Code Playgroud)

builds.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
  }
}
Run Code Online (Sandbox Code Playgroud)