5

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.

IronHawk
  • 365
  • 3
  • 7
  • _"the program crashes with a SIGSEGV in debugger"_ And what does the stacktrace say (i.e. which line(s) of code does it point to)? – Michael Feb 04 '19 at 21:08
  • Welcome to StackOverflow! Thanks for clearly explaining your issue. Unfortunately, I tend to believe that the cause of your problem is some minor typo. Same scenario works smoothly for me. – Alex Cohn Feb 04 '19 at 22:24
  • @Michael - I edited my question (added stacktrace etc.) – IronHawk Feb 05 '19 at 02:59
  • @AlexCohn - maybe the problem is in Virtual Device, or in Minimum API level. I really wondering because it is the wizard-generated app stub and only the change is an extra parameter. Also when I add primitive parameter like `Int` all works both in Java and Kotlin. – IronHawk Feb 05 '19 at 03:03
  • The difference with emulator is that this is x86 vs arm64 on your real device. Could it be that the **abiFIlters** were not set correctly? – Alex Cohn Feb 05 '19 at 07:31
  • @AlexCohn: If the abiFilters are wrong, would the app even install? I know that some x86 Android devices had support for running ARMv7 code through libhoudini, but I didn't think the emulator supported that. – Michael Feb 05 '19 at 09:06
  • @Michael if you change abiFilters between builds, you may end with a binary that was updated for arm but not for x86 – Alex Cohn Feb 05 '19 at 09:28
  • 1
    @AlexCohn: I checked both projects and **abiFilters** flag is not configured, so according to the docs _Gradle builds and packages all available ABIs_ both for Kotlin and Java. – IronHawk Feb 05 '19 at 17:32
  • 2
    I must confess that my experiment worked on an API 25 emulator, not 28. – Alex Cohn Feb 05 '19 at 19:49
  • Local variable name 'sample_text' should not contain underscores – Mori Apr 16 '22 at 09:20

0 Answers0