JVM crash on Android when using Kotlin & Kotson (gson library)


#1

I have some piece of kotlin code that crashes the JVM on Android.

I reported it with the kotson library originally, but realized that that’s the wrong placed, so I repost it here:

/* MainActivity.java */

@Override
protected void onResume() {
   super.onResume();
   TestKt.restore(this);
}
/* Test.kt */

interface TestInterface {}

class TestClass() : TestInterface {}

data class Test(
    val member: TestInterface = TestClass()
)

fun restore(activity: Activity) {
    val test = Gson().fromJson<Test>("{\"member\":{}}");
}

Gives me:
Fatal signal 11 (SIGSEGV), code 1, fault addr 0x6f0095 in tid 29300 (fab1ans.app)

Thank you


#2

I may not be correct about this, but I am pretty sure that in order for Gson or any equivalent to work, reflection is needed, in which case you would have to put kotlin-reflect in the project dependencies. Be aware that it adds another 6k methods and about 1.5 Mb to your APK.


#3

I’ve investigated based on the code you gave, and this is a Gson / Android issue.
It is not related to Kotlin (I’ve replicated the same crash with only java code).

It is because Gson tries to instanciate an interface, (in this case, TestInterface). While the JVM would throw an exception, Android juste crashes…
It appears the issue has been fixed, and will be part of Gson’s next release :slight_smile:


#4

Thanks!