更新到 Android Studio 4.2 后无法创建新的 Kotlin 项目

Can't create new Kotlin project after updating to Android Studio 4.2

我更新了 Android studio 4.2,但我无法创建新项目 kotlin

A problem occurred configuring root project 'My Application'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0-release-764.
 Searched in the following locations:
   - https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.5.0-release- 
764/kotlin-gradle-plugin-1.5.0-release-764.pom
   - https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.5.0-release-764/kotlin-gradle-plugin-1.5.0-release-764.pom
 Required by:
     project :

Possible solution:
- Declare repository providing the artifact, see the documentation at 
 https://docs.gradle.org/current/userguide/declaring_repositories.html

错误很明显Gradle无法找到您声明的库

可能的修复

地点

Project -> build.gradle

//update it
dependencies {
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
}

已编辑 kotlin 昨晚发布了 1.5.0 稳定版你可以使用这个版本保持最新

dependencies {
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0"
}

为我工作:

地点:build.gradle

改变

buildscript {
    ext.kotlin_version = "1.5.0-release-764"
    repositories {
        google()
        mavenCentral()
    }

buildscript {
    ext.kotlin_version = "1.5.0"
    repositories {
        google()
        mavenCentral()
    }

您应该知道 Android Studio 上的 Gradle 脚本中有“两个”build.gradle 文件,build.gradle(项目)和 build.gradle(模块)。我花了几个小时才意识到我一直在查看 build.gradle 的模块版本,同时试图解决这个问题,但无法找到要更改的正确 kotlin 版本变量。

build.gradle(项目)是 the proper build.gradle 您要对其进行更改。

从那里改变

buildscript {
ext.kotlin_version = "1.5.0-release-764"
repositories {
    google()
    mavenCentral()
}

buildscript {
ext.kotlin_version = "1.5.0"
repositories {
    google()
    mavenCentral()
}

并再次尝试重建您的项目。这应该有效。

在创建新项目时

buildscript {        ext.kotlin_version = "1.5.0-release-764"
        repositories {
            google()
            mavenCentral()
        }

这对我有用

buildscript {
    ext.kotlin_version = "1.4.30"
    repositories {
        google()
        mavenCentral()
    }

更改自

    buildscript {
    ext.kotlin_version = "1.5.0-release-764" //Remove release-764
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    }
}

改为

 buildscript {
    ext.kotlin_version = "1.5.0" //Here is the change have to add this Version 
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.0"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    }
}

将此依赖项添加到您的 build.gradle

 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0"

如果你想要一个快速的解决方案,只需 select 1.4.32 Kotlin 版本当 Android Studio 提示配置 Kotlin window.

此时 - 2021 年 5 月 13 日 - selecting 1.5.0 它给了我 gradle 问题

When ever I started creating a new project this error occurs with the gradle plugin.

更新 2021 年 5 月 14 日 ->

GOTO -> gradle -> build.gradle -> 在代码中 -> dependencies -> Change Classpath -> classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0"

一切顺利。

请将您的 Android Studio 更新至 4.2.1(2021 年 5 月 13 日发布),新的 稳定版。 此问题已解决:-

Kotlin Issue #187403740: Android Studio 4.2.0 generates projects with the wrong Kotlin version: "1.5.0-release-764"

更改日志

https://androidstudio.googleblog.com/2021/05/android-studio-421-available.html

新 Android Studio 4.2.1 已发布补丁。

如果升级不能解决您的问题,请尝试重新启动并重置缓存。

否则使用上面提供的解决方案替换build.gradle

来自

ext.kotlin_version = "1.5.0-release-764"

ext.kotlin_version = "1.5.0"

您问题的最简单解决方案:

变化:

ext.kotlin_version = "1.5.0-release-764"

至:

ext.kotlin_version = "1.5.0"

在您的 build.gradle 项目级别

因为现在每个人都知道解决方案,我们必须摆脱 -release-764 因为这个版本没有这样的工件。

下面同样可以验证

https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin

我只是想添加一些提示,每当你在未来任何 android 的库中遇到任何此类错误(Could not resolve all artifacts 对于任何库 Could not find xyz)。

我们使用的大部分库都是由

解析的
  • google()
  • mavenCentral()

如果它是 Google 的 Android 图书馆

如果它是另一个 android 库,并且除了 mavenCentral() 之外您没有为它们指定任何特定的存储库,那么您可以使用 https://mvnrepository.com/ or https://search.maven.org/ 来搜索或验证您的库的版本

如果您使用 maven { url 'https://jitpack.io' } 作为图书馆的存储库,那么您可以在此处搜索 https://jitpack.io/

希望这些指示对某些人有用,如果我遇到任何此类错误,这总是对我有帮助。

Change the dependency

ext.kotlin_version = "1.5.0-release-764"
   dependencies {
       classpath "com.android.tools.build:gradle:4.2.0"
       classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
   }

To this

ext.kotlin_version = "1.5.10"
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"     
    }

如果有人在 Java 项目中遇到类似问题,那么 这是一个示例 build.gradle 文件(项目级别)

注意:注意那里添加的mavenCentral()方法,这是必须要为[=22=添加] 4.2.1+

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.8'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.0'
        classpath 'com.android.tools.build:gradle:4.2.1'
    }
}


allprojects {
    repositories {


        mavenCentral()
        maven {
            url "https://maven.google.com/"
        }

        google()

        maven() {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }

        jcenter()

        maven { url 'https://jitpack.io' }


    }
}

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

在我的例子中,通过在 build.grade.

的构建脚本和所有项目存储库中用 mavenCentral() 替换 jcenter() 很容易解决这个问题