https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
h

hfhbd

07/05/2022, 12:11 PM
How can you use macros in Kotlin?
j

jw

07/05/2022, 12:14 PM
Define an inline function that calls the macro in C and then call that function from Kotlin
👍 2
h

hfhbd

07/05/2022, 12:16 PM
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

Landry Norris

07/05/2022, 3:09 PM
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

hfhbd

07/05/2022, 3:12 PM
Thanks! Maybe this is "fixed" by using
@CName("Java_Main_hello")
. At least using the release build works: https://github.com/hfhbd/kotlinJni
l

Landry Norris

07/05/2022, 3:13 PM
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

hfhbd

07/05/2022, 3:14 PM
That's sound promising 😄
Do you also know what JNICALL is?
l

Landry Norris

07/05/2022, 3:15 PM
Not sure.
Just out of curiosity, are you working on this for Android or desktop?
h

hfhbd

07/05/2022, 3:15 PM
Mainframe 😄
🤨 1
1
l

Landry Norris

07/05/2022, 3:17 PM
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

hfhbd

07/05/2022, 3:24 PM
I was surprised about lack of (good) documentation how to write a native android lib/app with Kotlin native.
l

Landry Norris

07/05/2022, 8:51 PM
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

natario1

07/07/2022, 8:36 AM
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.
145 Views