Skip to content
Advertisement

Kotlin JNA getting declared field names to be empty list

The code below is highly inspired by this answer and this answer, and I am trying to get it work with Kotlin language, rather than Java that was given in the answer there.

It is basically based on JNA (Java Native Access), which essentially pulls the SYSTEM_BATTERY_STATE from the Windows Native library (on C++) to get information about the battery on Laptops.

The code works as expected in Java version, but if I try to do the same with Kotlin, I get an exception saying that the declared field names is an empty list/array.

JavaScript
JavaScript

Why does declared field names comes out to be empty array/list? And how could I troubleshoot this?

Advertisement

Answer

JNA’s Structure class uses reflection to find the field names, which means they must be declared as public fields/attributes. The @JvmField annotation in Kotlin removes getters and exposes those attributes as needed. I suspect this should work for you (untested):

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement