How to open native enum for Java through JNI?

I am importing headers from an existing project for a port in the Android NDK. In some cases, the enumerations defined in the native headers that I would like to use from the Java level are listed. How can this be done?

Ideally, I would like to simply set constants to the Java level somehow, but I see no way to do this.

The most obvious possibility is the dual definition of enums in Java and C ++. However, the existing headers a) are not explicitly numbered, b) have elements that are # ifdef'ed, and c) are shared with existing projects through external SVNs. Therefore, bidirectional enumeration seems to be much more fragile than even a typical case.

The next idea is to use some build-time generation code to create an enum in Java based on a pre-processed header - perhaps as integer constants, not Java enums?

The third and most vague idea I have is to define an enumeration in Java, pass these objects into JNI glue and compare it with some call to FindClass (), GetStaticFieldID () and GetStaticObjectField (); then drag the JNI glue to the original listing. However, all this seems ineffective.

Suggestions?

+4
source share
1 answer

I would make a completely independent set of Java enumerations and map them together at the JNI level, where you have both available. Be sure to run javah on the enum classes so that you get some #defines for your ordinals.

+1
source

Source: https://habr.com/ru/post/1447342/


All Articles