Referencing Java objects from native code

We know from the previous section that a string is represented in JNI as jstring, which is in fact a Java object, which means that it is possible to exchange any Java object through JNI! However, because native code cannot understand or access Java directly, all Java objects have the same representation, jobject.

In this part, we will focus on how to save an object on the native side and how to send it back to Java. As an example, we will work with a custom object Color, although any other type of object would work too.

Note

The resulting project is provided with this book under the name Store_Part7.

Get Android NDK Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.