Accessing Java static and instance fields in the native code
We have demonstrated how to pass parameters of different types to native methods and return data back to Java. This is not the only way of sharing data between the native code and Java code. This recipe covers another method—accessing Java fields from the native code.
Getting ready
We're going to cover how to access Java fields of different types, including primitive types, strings, instance objects, and arrays. The following recipes should be read first before reading this recipe:
Passing parameters and receiving returns in primitive types
Manipulating strings in JNI
Manipulating classes in JNI
Manipulating objects in JNI
Manipulating arrays in JNI
Readers are also expected to be familiar with Java reflection API.
How to do it…
Follow these steps to create a sample Android project that demonstrates how to access Java static and instance fields from the native code:
Create a project named
AccessingFields
. Set the package name ascookbook...