kotlin-multiplatformkotlin-nativekotlin-interop

Kotlin Multiplatform - Objective-C interoperability architecture issue Undefined symbols for architecture x86_64


I just tried using native interop feature since I need native code written in Objective-C to be used in my library. So first I'm trying to test using simple hello to interop an Objective-C code

gradle :

kotlin {
     ...
 
     val iosX64 = iosX64("ios") {
          compilations.getByName("main) {
              val myInterop by cinterops.creating {
                 defFile(project.file("src/nativeInterop/cinterop/objective-c-file.def"))
              }
          }
     }

     val iosArm32 = iosArm32 ("iosArm32 ") {
          compilations.getByName("main) {
              val myInterop by cinterops.creating {
                 defFile(project.file("src/nativeInterop/cinterop/objective-c-file.def"))
              }
          }
     }

     val iosArm64 = iosArm64("iosArm64") {
          compilations.getByName("main) {
              val myInterop by cinterops.creating {
                 defFile(project.file("src/nativeInterop/cinterop/objective-c-file.def"))
              }
          }
     }

     ...
}

my .def configuration :

language = Objective-C
headers = Hello.h Foundation/Foundation.h
package = org.native

compilerOpts = -Isrc/nativeInterop/include

linkerOpts = -framework Foundation "-F/Applications/Xcode 11.3.1.app/Contents/Developer/Platforms/"

this is the project structure look like
enter image description here

my Hello.h

#import <Foundation/Foundation.h>

@interface Hello: NSObject

+ (id) init;
- (void) helloObjectiveC;

@end

Hello.m

#import "Hello.h"

@implementation Hello

+ (id) init {}
- (void) helloObjectiveC {
    printf("hello Objective c interop")
}

@end

after run cinterop task from gradle, the klib generated from that hello class and of course now I can import it to my kotlin project. for example in my iOS module:

package bca.lib

import org.native.Hello

actual object Platform {

    actual fun getPlatform() = "iOSMain"
    
    fun helloNativeInterop() {
        val hello = Hello()
        hello.helloObjectiveC()
    }

}

Then I'm trying to call it in my unit test to check if I can get the result or I aslo try to build it to framework but I got an error :

> Task :cleanIosTest UP-TO-DATE
> Task :cinteropMyInteropIos UP-TO-DATE
> Task :generateBuildKonfig UP-TO-DATE
> Task :generateIosMainAppDatabaseInterface UP-TO-DATE
> Task :compileKotlinIos UP-TO-DATE
> Task :iosProcessResources UP-TO-DATE
> Task :iosMainKlibrary UP-TO-DATE
> Task :compileTestKotlinIos UP-TO-DATE
> Task :linkDebugTestIos FAILED
e: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors
Please try to disable compiler caches and rerun the build. To disable compiler caches, add the following line to the gradle.properties file in the project's root directory:
    
    kotlin.native.cacheKind=none
    
Also, consider filing an issue with full Gradle log here: https://kotl.in/issue
The /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld command returned non-zero exit code: 1.
output:
Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_Hello", referenced from:
      objc-class-ref in result.o
ld: symbol(s) not found for architecture x86_64
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':linkDebugTestIos'.
> Compilation finished with errors
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.6.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 8s


Anyone know how to resolve this issue or maybe is it impossible to interop in that way? I just needed so it can be used in my iOS app later after I successfully build it to .framework


Solution

  • This is a known limitation, described in the Kotlin issue tracker some time ago: KT-39562. Long story short, the cinterop tool does not provide an option to work with source files out-of-the-box. The easiest way here will be to create a framework from your Objective-C code. After that, you'll be able to work with it in a simple way, described in the documentation.