KMM静态库集成TensorFlowLiteObjC报错:模块未找到
解决KMM静态库集成TensorFlowLiteObjC的cinterop错误
问题原因
构建KMM静态库时,cinterop工具无法定位TensorFlowLiteObjC模块。静态库模式下,CocoaPods依赖不会自动暴露给Kotlin/Native的interop工具,需手动配置模块路径与依赖链接规则。
解决方案步骤
1. 调整CocoaPods配置
更新cocoapods块,明确静态链接方式,并显式声明TensorFlowLiteC依赖(避免静态库模式下的路径缺失问题):
cocoapods { summary = "Core SDK Module" homepage = "Link to the Shared Module homepage" ios.deploymentTarget = "14.0" framework { baseName = "Core" isStatic = true } pod("TensorFlowLiteObjC") { version = "~> 2.10.0" static = true } pod("TensorFlowLiteC") { version = "~> 2.10.0" static = true } useLibraries() }
2. 创建cinterop定义文件
在共享模块的src/iosMain/cinterop目录(无则新建)下,创建TensorFlowLiteObjC.def文件,内容如下:
module TensorFlowLiteObjC headers TensorFlowLiteObjC/TensorFlowLiteObjC.h linkerOpts -framework TensorFlowLiteObjC -framework TensorFlowLiteC
3. 配置Gradle的cinterop路径
在共享模块的build.gradle.kts中添加cinterop配置,指定def文件路径与头文件目录:
kotlin { // 保留原有其他配置 ios { binaries { framework { baseName = "Core" isStatic = true } } cinterop("TensorFlowLiteObjC") { defFile = file("src/iosMain/cinterop/TensorFlowLiteObjC.def") includeDirs { val podsDir = project.file("../iosApp/Pods") include(podsDir.resolve("TensorFlowLiteObjC/TensorFlowLiteObjC")) include(podsDir.resolve("TensorFlowLiteC/include")) } } } }
4. 预构建CocoaPods依赖
先在iOS项目目录执行pod install,确保依赖下载并配置完成:
cd iosApp pod install cd ..
5. 重新构建项目
执行命令完成构建:
./gradlew clean build
额外说明
- 静态库模式下,Kotlin/Native无法自动识别CocoaPods的模块路径,必须手动指定头文件目录与链接参数。
- 需保证TensorFlowLiteObjC与TensorFlowLiteC的版本完全一致,避免版本冲突。
内容的提问来源于stack exchange,提问作者joselufo
相关产品推荐
相关产品推荐

