I created two Native C++ projects - one with Kotlin as a base language, another one with Java.
In both projects I made the same modification of the automatically-created C++ functions:
extern "C" JNIEXPORT jstring JNICALL
Java_com_stellarsolvers_test_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */,
jstring s) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
So I added the parameter: jstring s
.
The definition is also modified in the following way:
Kotlin:
external fun stringFromJNI(s: String): String
Java:
public native String stringFromJNI(String s);
In the calling code I provided a string value for this function:
Kotlin:
sample_text.text = stringFromJNI("Kotlin")
Java:
tv.setText(stringFromJNI("Java"));
No other changes were made to either of the projects.
Both projects are from "Native C++" template of
- Android Studio 3.3
- Build #AI-182.5107.16.33.5199772, built on December 25, 2018
- JRE: 1.8.0_152-release-1248-b01 x86_64
- JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
- macOS 10.14.2
The single change in the "Create New Project" wizard is:
- Minimum API level: API 21
I expected both projects to work properly.
With Java that was the case.
However, the Kotlin app behaves in a strange way: when the execution reaches the point where the C++ function should be invoked, the program crashes with a SIGSEGV in debugger on:
- Virtual Device: Nexus 5X API 28 x86 (Android 9, API 28)
Debug stacktrace is:
art_sigsegv_fault 0x00000000e7e571d0
art::FaultManager::HandleFault(int, siginfo*, void*) 0x00000000e7e57774
art::art_fault_handler(int, siginfo*, void*) (.llvm.650222801) 0x00000000e7e5749b
___lldb_unnamed_symbol22$$app_process32 0x00000000598aa6af
___lldb_unnamed_symbol2$$libc.so 0x00000000eace1c50
art::ManagedStack::ShadowFramesContain(art::StackReference<art::mirror::Object>*) const 0x00000000e8093086
art::Thread::DecodeJObject(_jobject*) const 0x00000000e81d8ac8
<unknown> 0x00000000e876d05d
dlsym 0x00000000598a9530
An app crashes on entering to the native method.
What is the reason of such a strange behavior of the Kotlin app?
I just tried to debug on real device: ASUS Zenfone Max Pro M1 (Android 8.1.0, API 27). And both Kotlin and Java projects are work properly. Debugger allows to enter the native method, no any crashes appear.
So the updated problem status is: An application crashes in 100% of cases on Virtual Device.