Kotlin多平台默认项目运行iOs App时出错:无法解析com.android.tools.build:gradle:7.4.0-rc01

Sia*_*ash 12 kotlin kotlin-multiplatform

我创建了一个默认的 KMP 项目,没有进行任何更改。它在 Android 上运行良好,但是当我尝试在 iOS 上运行它时,它告诉我找不到 gradle 插件:

A problem occurred configuring root project 'KMPSandBox'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:7.4.0-rc01.
     Required by:
         project : > com.android.application:com.android.application.gradle.plugin:7.4.0-rc01
         project : > com.android.library:com.android.library.gradle.plugin:7.4.0-rc01
      > No matching variant of com.android.tools.build:gradle:7.4.0-rc01 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' but:
          - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.0-rc01 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
          - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.0-rc01 declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')
Run Code Online (Sandbox Code Playgroud)

Rad*_*u M 14

对我来说,您似乎安装了不兼容的 JDK,或者至少在 Gradle 构建脚本中定位了错误的 JDK。

请查看 Gradle 设置。

在此输入图像描述

以下错误行给了我这个提示:

Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
Run Code Online (Sandbox Code Playgroud)

从 Gradle 7.3.0 开始,最低 JDK 版本为 11。

https://developer.android.com/studio/releases/gradle-plugin#compatibility-7-3-0

您能检查一下您是否安装了 JDK 11 吗?

在您的app/build.gradle.kts(或 build.gradle - 如果您使用 Groovy)中,如果您有这些行并且它们可能在 Java 8 上?我个人总是喜欢在脚本文件中明确设置这些行,而不依赖于默认行为。

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
Run Code Online (Sandbox Code Playgroud)

否则默认:

默认情况下,用于编译项目的Java语言版本是基于项目的compileSdkVersion,因为不同版本的Android支持不同版本的Java。如有必要,您可以通过将以下compileOptions块添加到build.gradle文件来覆盖此默认Java版本。 https://developer.android.com/studio/intro/studio-config#jdk


小智 11

最好和简单的工作解决方案是转到 android studio 中的文件选项,然后单击设置,然后在构建、执行、部署选项下单击 gradle,然后在 gradle 属性下单击项目名称,然后在 gradle JDK 中的 gradle 子类别下选择嵌入式 JDK,然后单击应用并同步你的 gradle 文件,那么它肯定会起作用。

在此输入图像描述