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

如何在IntelliJ中调试Kotlin多平台项目的Windows应用包?

Kotlin多平台Windows应用调试方案

1. 配置IntelliJ原生运行/调试配置

  • 打开顶部菜单栏Run -> Edit Configurations,点击左上角+号,选择Application
  • Main class:选Windows模块的主入口类(通常是你的包名.MainKt,Compose Desktop项目一般直接选MainKt)
  • Working directory:设置为Windows模块根目录或build/bin/windows/debugExecutable,确保应用能读取资源文件
  • Classpath:选择[你的项目名]-windows-main模块的类路径,IntelliJ通常会自动填充,未填充时手动选择即可
  • 保存配置后点击Debug按钮,就能像调试Android应用一样使用断点、变量查看等功能

2. 通过Gradle任务关联调试

  • 在右侧Gradle面板找到你的项目名 -> windows -> Tasks -> application -> run
  • 右键点击run任务,选择Debug,Gradle会自动启动Windows应用并关联IntelliJ调试器
  • 需要传递启动参数的话,右键run任务选Modify Run Configuration,在Arguments栏添加--args="你的参数"

3. 调试原生Windows组件(涉及JNI/原生库时)

  • 先通过Gradle或IntelliJ启动Windows应用
  • 打开Visual Studio,选择Debug -> Attach to Process,找到你的应用进程(比如你的应用名.exe)
  • 确保生成调试符号,在Windows模块的build.gradle.kts中添加配置:
    windows {
        compilations.main {
            kotlinOptions {
                freeCompilerArgs += "-Xdebug"
            }
        }
        binaries.executable {
            debuggable = true // 启用调试符号生成
        }
    }
    

4. 日志辅助调试

  • 在关键代码位置加println()或用Kotlin标准库日志工具输出信息,运行时查看IntelliJ控制台
  • 需要持久化日志的话,可写入本地文件:
    val logFile = File(System.getProperty("user.home"), "kmp-windows-app.log")
    fun log(message: String) {
        logFile.appendText("${LocalDateTime.now()}: $message\n")
        println(message)
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 18:30:41