1

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

  • Technically speaking, the issue is simple. The interop processor generates Kotlin from the Objc headers. However, you also need to build the Objc and link the binary. `linkDebugTestIos` is trying to build a test kexe, but can't find `_OBJC_CLASS_$_Hello`. I don't know of a great way to do that off hand, or I'd make this an answer rather than a comment :) I'm sure somebody has a very easy and obvious way to do this. – Kevin Galligan Mar 01 '21 at 14:41
  • @KevinGalligan So that means I need to do something with the binary linking.., when the build happen..? – Davin Reinaldo Gozali Mar 03 '21 at 06:20

1 Answers1

1

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.

Artyom Degtyarev
  • 2,704
  • 8
  • 23
  • I see, yes I can do it easily with compile it to static library then include it in the def file. Well noted then if this way is not supported. Thank you so much! – Davin Reinaldo Gozali Mar 10 '21 at 10:54