Kotlin-native: iOS framework sample

Created on 9 Nov 2017  Â·  13Comments  Â·  Source: JetBrains/kotlin-native

Hello,

I'm looking at Kotine/Native in terms of cross-platform development for iOS and Android.
The idea, that one can implement iOS app using Kotlin is nice, but there no more usefulness than guinea pig project. Experience of implementing Objective-C code on Kotlin using IDE without code completion for native frameworks is headache, basically.
The more interesting approach for practical usage, would be vice-versa - share some business logic, or computational algorithms implemented on Kotlin between iOS and Android applications.
In this case, Kotlin code must be compiled as either iOS static library (.a), or, preferably, as iOS framework.
Do you plan to provide such kind of sample either in this repository, either as separate one, as you did for kotlinconf-app and kotlinconf-spinner apps?

With regards.

Most helpful comment

CC @OlexandrStepanov

Hello,

By the popular request Kotlin/Native team has designed and implemented facility for extending existing Objective-C and Swift applications with frameworks written in Kotlin/Native. It allows one to create framework from portable part of the project, written in Kotlin (i.e. shared on iOS and Android, for example) and import it to an existing application written in Objective-C or Swift, as a regular framework. As an example of such functionality, we have developed basic calculator application, see https://github.com/JetBrains/kotlin-native/tree/master/samples/calculator. It consists of Swift UI and Kotlin “business logic” of the calculator, capable to parse arithmetic expression and evaluate them. Important Kotlin types, such as kotlin.String, kotlin.List, kotlin.Number and function types are transparently mapped to NSString, NSArray, NSNumber and block pointers in the Objective-C world. Kotlin classes and interfaces can be inherited by Objective-C classes.

This feature will be available as part of upcoming 0.5 release of Kotlin/Native. To try it now, clone the repo, build Kotlin/Native as described in README.md, and then follow the instruction from sample's README.md.
Please let us know of your experience, send us your curses and praises.

All 13 comments

Hello,

Do you plan to provide such kind of sample either in this repository, either as separate one, as you did for kotlinconf-app and kotlinconf-spinner apps?

Providing such a sample would be an easiest part of the required work.
Because actually we first need to design and develop the feature, i.e. exposing Kotlin/Native code as Objective-C (or Swift) framework.

Fortunately, this feature is being worked on, no ETA yet.

You can have code completion for native Objective-C frameworks in CLion for Kotlin with Kotlin/Native plugin.

There is no support for multiplatform as well as multitarget projects yet. We are going to have multitarget projects support soon, and multiplatform projects a bit later.

@Alefas

You can have code completion for native Objective-C frameworks in CLion for Kotlin with Kotlin/Native plugin.

Could you be more specific, how to achieve this? I have installed CLion 2017.3 EAP, installed both plugins. Then, when I open kotlinconf-app via importing them from folder, I don't have auto completion for native iOS classes for sure. What I'm doing wrong?

As far as I understood from http://justmaku.org/2017-06-07-kotlin-on-ios, it's possible to call Kotlin method from Swift through C. However, that's an overkill, and have a lot of limitations.
Exposing Kotlin/Native code as Swift framework is extremely wanted feature. It will allow to consider Kotlin/Native as cross-platform dev tool for projects with iOS and Android application requirement.
Keep me posted on this, please.

With regards.

CC @OlexandrStepanov

Hello,

By the popular request Kotlin/Native team has designed and implemented facility for extending existing Objective-C and Swift applications with frameworks written in Kotlin/Native. It allows one to create framework from portable part of the project, written in Kotlin (i.e. shared on iOS and Android, for example) and import it to an existing application written in Objective-C or Swift, as a regular framework. As an example of such functionality, we have developed basic calculator application, see https://github.com/JetBrains/kotlin-native/tree/master/samples/calculator. It consists of Swift UI and Kotlin “business logic” of the calculator, capable to parse arithmetic expression and evaluate them. Important Kotlin types, such as kotlin.String, kotlin.List, kotlin.Number and function types are transparently mapped to NSString, NSArray, NSNumber and block pointers in the Objective-C world. Kotlin classes and interfaces can be inherited by Objective-C classes.

This feature will be available as part of upcoming 0.5 release of Kotlin/Native. To try it now, clone the repo, build Kotlin/Native as described in README.md, and then follow the instruction from sample's README.md.
Please let us know of your experience, send us your curses and praises.

When trying to build the repo from commit 1c764b8after executing ./gradlew dependencies:updateand ./gradlew bundle build fails with the following error on macOS Sierra with JDK 8

> Task :runtime:android_arm32Launcher FAILED
/Users/<sanitized>/Downloads/kotlin-native/runtime/src/launcher/cpp/androidLauncher.cpp:74:25: error: use of undeclared identifier 'InitRuntime'
  RuntimeState* state = InitRuntime();
                        ^
/Users/<sanitized>/Downloads/kotlin-native/runtime/src/launcher/cpp/androidLauncher.cpp:86:3: error: use of undeclared identifier 'DeinitRuntime'
  DeinitRuntime(state);
  ^
2 errors generated.


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':runtime:android_arm32Launcher'.
> Process 'command '/Users/<sanitized>/.konan/dependencies/clang-llvm-5.0.0-darwin-macos/bin/clang++'' finished with non-zero exit value 1

Build using ./gradlew dist distPlatformLibs works fine but only builds host platform –macOS in my case–

How can I get around this and build the iOS platform like the default macOS?

@RobertoEstrada we have fixed the issue, please try to build the repo from the latest master (2cc39037 currently).

Builds fine now @SvyatoslavScherbina Thanks!!

Have you encountered the below error by any chance?

ld: warning: ignoring file /Desktop/Workspace/kotlin-native/samples/calculator/build/konan/bin/iphone/KotlinArithmeticParser.framework/KotlinArithmeticParser, file was built for arm64 which is not the architecture being linked (x86_64): /Desktop/Workspace/kotlin-native/samples/calculator/build/konan/bin/iphone/KotlinArithmeticParser.framework/KotlinArithmeticParser
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_KAPCalculator", referenced from:
objc-class-ref in ViewController.o
"_OBJC_CLASS_$_KAPPartialParser", referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

@PradeepPappuAto
As far as I see, you are trying to compile it for simulator.
This feature is not yet supported, but is being worked on now.

@SvyatoslavScherbina If I wanted to add to the arithmetic framework what would be the process for recompiling it? You would start by adding code to the parser.kt file but im not seeing any commands to compile this to the framework.

@argallo the framework gets recompiled during every Xcode build (if Kotlin sources have changed since the previous one).
The Kotlin build is configured with Gradle, see build.gradle. The exact compilation command can be found in Xcode project. Please see README.md for more details about the entire build configuration.

Issue seems to be resolved, closing.

Was this page helpful?
0 / 5 - 0 ratings