Search code examples
kotlingradlecocoapodskotlin-multiplatform

Kotlin Multiplatform: "Fatal error: module 'TensorFlowLiteObjC' not found"


I’m trying to integrate TensorFlowLiteObjC into a KMM static library, but it’s not working I have the suspicion that I need to link the TensorFlowLiteC library with the TensorFlowLiteObjC library through cocoapods but I’m not fully sure.

Cocoapods configuration

  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"
    }
    useLibraries()
  }

Error when I'm trying to build the project (./gradlew clean build):

> Task :core:cinteropTensorFlowLiteObjCIosArm64 FAILED
Exception in thread "main" java.lang.Error: /var/folders/q9/kc2cwb813l7_d9x4n5y78b1h0000gn/T/6134897582236622275.m:1:9: fatal error: module 'TensorFlowLiteObjC' not found
        at org.jetbrains.kotlin.native.interop.indexer.UtilsKt.ensureNoCompileErrors(Utils.kt:274)
        at org.jetbrains.kotlin.native.interop.indexer.ModuleSupportKt.getModulesASTFiles(ModuleSupport.kt:75)
        at org.jetbrains.kotlin.native.interop.indexer.ModuleSupportKt.getModulesInfo(ModuleSupport.kt:14)
        at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.buildNativeLibrary(main.kt:516)
        at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLib(main.kt:285)
        at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.processCLibSafe(main.kt:214)
        at org.jetbrains.kotlin.native.interop.gen.jvm.MainKt.interop(main.kt:80)
        at org.jetbrains.kotlin.cli.utilities.InteropCompilerKt.invokeInterop(InteropCompiler.kt:45)
        at org.jetbrains.kotlin.cli.utilities.MainKt.mainImpl(main.kt:40)
        at org.jetbrains.kotlin.cli.utilities.MainKt.main(main.kt:62)

FAILURE: Build failed with an exception.

Any help would be appreciated


Solution

  • If you still looking for an answer
    You need to specify the module name for the TensorFlow pod (usually you can find it inside the library's module.modulemap file).

    pod(name = "TensorFlowLiteObjC", moduleName = "TFLTensorFlowLite", version = "2.11.0")
    

    then you can import and use it like this

    import cocoapods.TFLTensorFlowLite.TFLInterpreter
    import cocoapods.TFLTensorFlowLite.TFLInterpreterOptions
    ...
    val errorPointer = alloc<ObjCObjectVar<NSError?>>().ptr
    val options = TFLInterpreterOptions()
    val interpreter = TFLInterpreter(modelPath, options, errorPtr)
    ...