How can you use macros in Kotlin?
# kotlin-native
h
How can you use macros in Kotlin?
j
Define an inline function that calls the macro in C and then call that function from Kotlin
👍 2
h
Yeah, I try it: https://kotlinlang.org/docs/native-c-interop.html#macros
Copy code
---
static inline int jniExport() {
    return JNIEXPORT;
}

static inline int jniCall() {
    return JNICALL;
}
I just want to create a JNI function
JNIEXPORT int JNICALL Java_Main_hello(JNIEnv *env, jobject obj, jstring from, jint repeat);
Wow, it works without the macros...
l
JNIExport just adds an attribute telling the compiler not to obfuscate or otherwise modify the symbol name. If you don’t need this, it’s optional, but could fail in hard to debug ways.
Copy code
__attribute__ ((visibility ("default")))
h
Thanks! Maybe this is "fixed" by using
@CName("Java_Main_hello")
. At least using the release build works: https://github.com/hfhbd/kotlinJni
l
I don’t think Kotlin supports C attributes on functions in any meaningful way. For what it’s worth, I moved an Android NDK C library to Kotlin/Native for work and we’ve been using it in production without issues for a month now.
Yeah. The CName annotation is a good idea. I’m not using
RegisterNatives
right now, so I have to use CName anyway to match the symbol name the JVM expects.
h
That's sound promising 😄
Do you also know what JNICALL is?
l
Not sure.
Just out of curiosity, are you working on this for Android or desktop?
h
Mainframe 😄
🤨 1
1
l
Cool. I’ve been working on writing a library for JNI bindings in Kotlin lately. I’ve been working with Android only so far. I need to do more with the server and desktop space using JNI.
h
I was surprised about lack of (good) documentation how to write a native android lib/app with Kotlin native.
l
I agree. It seems like the NDK doesn’t get the same level of support (the last maven central scan found 24 libraries for ndk). It’s disappointing, because I feel like Kotlin could be a very good replacement for C++ on the NDK.
I think the big limitation right now is the lack of unit testing support on the ndk. I usually have to set up a linuxX64 target, since it’s the closest, if I want unit tests to work and just run tests there and hope that it works on the ndk if it works on linuxX64.
n
It’s been a while now since last update, but I wrote a gradle plugin for
androidNative
testing https://github.com/deepmedia/multiplatform-testing . It downloads and configures an emulator that can run your architecture, builds a test executable and runs it.
218 Views