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

Windows10下KMM项目使用ComposeUIViewController遇依赖问题求助

解决KMM Windows环境下找不到UIKit.UIViewController的问题

问题根源

ComposeUIViewController和UIViewController都是iOS平台专属API,不能在共享模块/Android模块中直接引用。Windows环境下KMM的共享模块默认不会引入iOS平台依赖,代码放错位置或依赖配置缺失就会触发这个错误。

具体解决步骤

  • 确认代码位置
    把MainViewController的代码移到iOS模块(通常是项目里的iosApp模块或iosMain源集),只有iOS模块能直接访问UIKit相关API。

  • 检查iOS模块依赖配置
    打开iOS模块的build.gradle.kts,确保已添加Compose for iOS的依赖,示例配置:

    plugins {
        kotlin("multiplatform")
        id("org.jetbrains.compose")
    }
    
    kotlin {
        ios {
            binaries {
                framework {
                    baseName = "YourProjectFramework"
                }
            }
        }
    
        sourceSets {
            val iosMain by getting {
                dependencies {
                    implementation(compose.ui)
                    implementation(compose.foundation)
                    implementation(compose.material3)
                    implementation(compose.runtime)
                }
            }
        }
    }
    
  • 用expect/actual实现跨平台调用(如果需要从共享模块触发)
    如果要在共享模块中调用iOS的ViewController,用跨平台抽象:

    1. 共享模块(commonMain)定义expect函数:
      expect fun createMainViewController(): Any
      
    2. iOS模块(iosMain)实现actual函数:
      import androidx.compose.ui.window.ComposeUIViewController
      import platform.UIKit.UIViewController
      
      actual fun createMainViewController(): UIViewController {
          return ComposeUIViewController {
              App(
                  darkTheme = false,
                  dynamicColor = false
              )
          }
      }
      
  • 同步Gradle并清理缓存
    点击Android Studio顶部的「Sync Project with Gradle Files」按钮同步配置,若仍有问题,执行「File → Invalidate Caches... → Invalidate and Restart」清理缓存后重试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 23:27:14