如何更新最新的 gradle 版本的 kotlin 库

How to update latest gradle version of kotlin library

我在最新的 intellij 中创建了一个多平台库。我的 intellj 添加了过时的 gradle 版本 settings.gradle.kts

pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.namespace == "com.android") {
                useModule("com.android.tools.build:gradle:4.1.2"). // How to update to latest version and what is the use of 4.1.2?
            }
        }
    }
}
rootProject.name = "xyz"

我在我的代码中评论了上面的内容。有人可以指导 如何将我的 gradle 版本更新到最新版本以及 4.1.2 那段代码有什么用?

我试图删除下面的 4.1.2 代码然后我遇到了问题

 resolutionStrategy {
     eachPlugin {
         if (requested.id.namespace == "com.android") {
             useModule("com.android.tools.build:gradle:4.1.2")
         }
     }
 }

错误

 Build file '/Users/vmodi/IdeaProjects/abc/build.gradle.kts' line: 1
 
 Plugin [id: 'com.android.application'] was not found in any of the following sources:

gradle-wrapper.properties

 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
 distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists

build.gradle.kts

plugins {
    kotlin("multiplatform") version "1.6.21"
    id("com.android.application")
}

group = "com.abc"
version = "0.0.1"

repositories {
    google()
    mavenCentral()
}

kotlin {
    android()
    iosX64()
    iosArm64()
    iosSimulatorArm64()

    sourceSets {
        val ktorVersion = "2.0.0"
        val commonMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-core:$ktorVersion")
                implementation("io.ktor:ktor-client-logging:$ktorVersion")
                implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
                implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
                implementation("io.ktor:ktor-client-auth:$ktorVersion")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2")
                implementation("io.insert-koin:koin-core:3.2.0-beta-1")
            }
        }
        val androidMain by getting {
            dependencies {
                implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
                implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
            }
            val iosX64Main by getting
            val iosArm64Main by getting
            val iosSimulatorArm64Main by getting
            val iosMain by creating {
                dependsOn(commonMain)
                iosX64Main.dependsOn(this)
                iosArm64Main.dependsOn(this)
                iosSimulatorArm64Main.dependsOn(this)
                dependencies {
                    implementation("io.ktor:ktor-client-darwin:$ktorVersion")
                }
            }
        }
    }
}

android {
    compileSdk = 21
    sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
    defaultConfig {
        applicationId = "com.abc.kotlinmultiplatform"
        minSdk = 21
        targetSdk = 31
    }
    @Suppress("UnstableApiUsage")
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

IntelliJ 版本

IntelliJ IDEA 2022.1.1 (Ultimate Edition)
Build #IU-221.5591.52, built on May 10, 2022
Licensed to Vivek Modi
For educational use only.
Runtime version: 11.0.14.1+1-b2043.45 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 11.6.5
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 16
Non-Bundled Plugins:
    com.intellij.nativeDebug (221.5591.54)
    org.jetbrains.kotlin-js-inspection-pack-plugin (0.0.9)

Kotlin: 221-1.6.21-release-337-IJ5591.52

根据@PylypDukhov 的建议,尝试更新 gradle 7.0.4 我收到这个奇怪的错误

错误

Failed to query the value of property 'namespace'.
Package Name not found in /Users/vmodi/IdeaProjects/abc/src/androidMain/AndroidManifest.xml, and namespace not specified. Please specify a namespace for the generated R and BuildConfig classes via android.namespace in the module's build.gradle file like so:

android {
    namespace 'com.example.namespace'
}

settings.gradle.kts

pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
    }
    resolutionStrategy {
        eachPlugin {
            if (requested.id.namespace == "com.android") {
                useModule("com.android.tools.build:gradle:7.0.4")
            }
        }
    }
}
rootProject.name = "LetsGetCheckedKotlinMultiplatform"

添加了androidMain

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.abc"/>

4.1.2 是版本号。要了解最新版本,请访问 Google's Maven Repository。在撰写本文时,最新的稳定版本是 7.2.0,因此只需将“4.1.2”替换为“7.2.0”并重新构建项目即可。

4.1.2是androidgradle插件版本,与gradle版本无关

Android gradle 插件附带 Android Studio,如果你想使用@Egor 提到的最新版本,你必须使用 Android Studio.

IntelliJ IDEA 延迟支持它们 - 目前最后支持的版本是 7.0.4,这对我来说很好用,您可能遇到的唯一问题是缺少对 Android 的 32 目标版本的支持 - 但它不需要上传应用程序并且可以在 32 上正常工作设备,你也可以用最新的插件在 A​​S 中构建发布版本,并在 IDEA 中开发,对我来说,它似乎在 KMM 项目中表现更好。