旧版本Compose项目如何迁移到Koltin 2.0版本

发布于:2024-07-01 ⋅ 阅读:(16) ⋅ 点赞:(0)

升级Kotlin到2.0版本后,如果是使用了compose会提示:

Starting in Kotlin 2.0, the Compose Compiler Gradle plugin is required
when compose is enabled. See the following link for more information:
https://d.android.com/r/studio-ui/compose-compiler

除了删除composeOptions外,链接给出了使用libs.versions.toml的方法。并不是说官方给的教程不好,而是有很多旧版本的gradle文件没有做这样的配置。

所以就为老版本提供相关迁移方法,并做具体记录

1.非常老的,以buildscript为顶级节点的,修改如下

buildscript {
    ext {
        compose_version = '1.7.0-alpha04'
        kotlin_version = "2.0.0"
    }

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        //新增
        classpath "org.jetbrains.kotlin:compose-compiler-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

2.较新的,以plugin为根节点的

plugins {
    id 'com.android.application' version '8.5.0' apply false
    id 'com.android.library' version '8.5.0' apply false
    id 'org.jetbrains.kotlin.android' version '2.0.0' apply false
    //新增
    id 'org.jetbrains.kotlin.plugin.compose' version '2.0.0' apply false
}

对应模块的build.gradle增加

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    //新增
    id 'org.jetbrains.kotlin.plugin.compose'
}

网站公告

今日签到

点亮在社区的每一天
去签到