You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Compose Multiplatform Gradle约定插件中配置Desktop模块?

在Compose Multiplatform约定插件中添加Desktop平台配置

以下是给你的Gradle约定插件添加Desktop(JVM)平台支持的具体步骤,适配你的多模块架构:

1. 给build-logic的约定模块引入Desktop依赖

在build-logic/convention/build.gradle.kts的依赖块中,添加Compose Desktop和Kotlin JVM相关依赖:

dependencies {
    // 保留已有的Android、iOS相关依赖
    implementation(compose.desktop.currentOs)
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:${libs.versions.kotlin.get()}")
}

2. 在约定插件中配置JVM(Desktop)目标

打开你的Compose多平台约定插件类(比如ComposeMultiplatformConventionPlugin.kt),在kotlin { ... }配置块中添加JVM目标,并关联源码集:

kotlin {
    // 保留已有的android()、iosX64()/iosArm64()/iosSimulatorArm64()配置

    // 添加Desktop JVM目标
    jvm("desktop") {
        compilations.all {
            kotlinOptions.jvmTarget = "17" // 根据项目需求调整JVM版本
        }
        withJava() // 启用Java互操作(可选,按需添加)
    }

    sourceSets {
        // 配置Desktop主源码集依赖
        val desktopMain by getting {
            dependencies {
                implementation(compose.desktop.currentOs)
                // 若有共享模块,添加依赖:implementation(project(":shared"))
            }
        }
        // 配置Desktop测试源码集
        val desktopTest by getting {
            dependencies {
                implementation(kotlin("test-jvm"))
            }
        }
    }
}

3. 配置Desktop运行任务(可选)

如果需要通过Gradle直接运行Desktop应用,在约定插件中添加任务配置:

// 给Desktop模块添加运行任务
tasks.register<JavaExec>("runDesktop") {
    group = "application"
    description = "Run the desktop application"
    classpath(sourceSets["desktopMain"].runtimeClasspath)
    mainClass.set("你的主类全路径,比如com.johan.docutagshell.MainKt")
}

4. 在Desktop模块中应用约定插件

确保你的Desktop应用模块(比如:app-desktop)的build.gradle.kts中应用约定插件:

plugins {
    id("com.johan.docutagshell.convention.compose-multiplatform") // 替换为你的约定插件ID
}

最后检查项目结构,确保Desktop模块的源码目录符合规范:src/desktopMain/kotlin/(存放主代码)和src/desktopTest/kotlin/(存放测试代码)。

内容的提问来源于stack exchange,提问作者Damon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:12:42