Using JNI in Openframeworks Android (Part I)

Using JNI in Openframeworks Android (Part I)

This tutorial is a brief introduction to JNI ( Java Native Interface) where we’ll look over the benefits and potential uses in the Openframeworks Android (OF Android) context.Specifically, we’ll see how to call native elements from an OpenFrameworks’ application. This communication technique allows very wide range of developing possibilities and reduces the complexity of the projects. Thanks to JNI, we’ll be able to take advantage of the Android SDK‘s elements power. In order to follow this tutorial, you must download and install Eclipse and OpenFrameworks Android. You can find a detailed tutorial explaining how to do so here.

Introduction to JNI

Java Native Interface (JNI) is a programming framework which allows a program written in Java- executed in a virtual machine- to run and interact with other programs written in other languages such as C, C++ and assembly. The OpenFrameworks Android distribution already uses the JNI framework to compile and run OpenFrameworks programs (which are based in C++) in those devices running Android (based in Java).

Why JNI in OpenFrameworks?

One of the main features of the Java language is its portability. At first sight, we could think using JNI limits JAVA’s portability only to devices capable of running native code. Paradoxically, in the OpenFrameworks context, using JNI adds portability by allowing running OF programs in Android platforms. Using this procedure will allow us to increase OpenFrameworks cross-platform possibilities.

Do I want to use JNI in OpenFrameworks?

As mentioned before, Openframeworks already uses JNI to run in Android. Nonetheless, it is a good idea being able to combine the potential of Openframeworks together with those offered by the Android SDK. That way, we will be reducing some complexity in the programming (thanks to Android SDK) and we will be capable to develop powerful android applications with a very accurate finish touch.

Therefore, a good strategy we can follow is to keep developing our software in Openframeworks and using JNI to take advantage of those elements only available in an Android context. Like this, we will be completing our creative software in a simple and effective manner.

A good example of the JNI’s possibilities is invoking the device’s keyboard from an interface coded with Openframeworks (for example while using the ofxGui add-on). This way, the user could introduce data using the OS native keyboard.

Another use case when we’ll want to use this technique will be when controlling the different screens or Activities of our Android App. Using JNI we’ll be able to create applications combining the navigation and interface possibilities of the Android SDK together with the visual and calculus potentials offered by OF Android.

¿How do I improve my OF App with JNI?

Basically, we can communicate with JNI in two different ways, depending in our needs. On the first hand, we invoke Openframeworks code (C++) from a method coded in the Java Android side. On the second hand, we could go the opposite direction by invoking Java Android code inside the flow of our Openframeworks program (C++).

In the next two parts of this tutorial, we will discuss both cases and we will add some code to exemplify the technique.

- TUTORIAL PART II: Using JNI in Openframeworks Android (Part II: calling OF from Java).

- TUTORIAL PART III: Using JNI in Openframeworks Android (Part III: calling Java from OF).