Android Compose 版本与 Kotlin 版本的兼容问题

问题描述

Android Studio 版本:2021.2.1 Patch 2

新建一个 compose 项目后,一运行,就报了类似如下错误:
“This version (1.1.1) of the Compose Compiler requires Kotlin version 1.6.10 but you appear to be using Kotlin version 1.5.31 …”

解决问题

1. 找到 compose 与 kotlin 的兼容性版本对照表

如下
在这里插入图片描述
我的目标是,想应用最新的版本

2. 添加 compose compiler 依赖

在module / build.gradle 中,增加

implementation "androidx.compose.compiler:compiler:1.3.0"

最新的 AS 是有版本自动提示的。 输入1.3 发现就有了 1.3.0, 而文档中 稳定版本的 1.3.0还没更新上去。

接着一运行,发现错误依旧 …

3. 修改 kotlin 版本配置

发现在 project 下的 build.gradle 中有:

plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
}

找到 kotlin 1.5.31 的配置,修改成 1.7.10。

4. 修改 compose 其它依赖项为最新的稳定版本

Compose 库-稳定版 页面可以查看到最新的发布日志。如:

在这里插入图片描述
发现其它 compose 库最新版本是 1.2.1,且在 AS 中使用自动提示时,也无法使用1.3.0,那就都改成1.2.1了。

[app module] / build.gradle:

android {
    compileSdk 33
	defaultConfig {
		...
		targetSdk 33
	}
	...
	composeOptions {
        kotlinCompilerExtensionVersion '1.3.0'
    }
}
dependencies {
//    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10"
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation "androidx.compose.compiler:compiler:1.3.0"
    implementation "androidx.compose.ui:ui:1.2.1"
    implementation "androidx.compose.animation:animation:1.2.1"
    implementation "androidx.compose.animation:animation-graphics:1.2.1"
    implementation "androidx.compose.foundation:foundation:1.2.1"
    implementation "androidx.compose.foundation:foundation-layout:1.2.1"
    implementation "androidx.compose.runtime:runtime:1.2.1"
    implementation 'androidx.compose.material3:material3:1.0.0-alpha16'
    implementation "androidx.compose.ui:ui-tooling-preview:1.2.1"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
    implementation 'androidx.activity:activity-compose:1.5.1'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:1.2.1"
    debugImplementation "androidx.compose.ui:ui-tooling:1.2.1"
    debugImplementation "androidx.compose.ui:ui-test-manifest:1.2.1"
}

最终 gradle sync 后,编译运行成功了 ^~^

猜你喜欢

转载自blog.csdn.net/jjwwmlp456/article/details/126443186