JNI is built serving as a bridge between Java and NDK code.
We may follow the JDK1.1 JNI guide in Java native Interface Programming, which includes
On the Java side, you declare a native method with the
nativekeyword and an empty method body. On the native language side, you provide an implementation for the native method. You must take care when writing native methods to "match" the native function implementation with the method signature in Java.
javah, explained in Step 3: Create the .h file, is a helpful tool to generate native function prototypes that match the Java native method declaration.
The JNI defines a mapping of Java types and native (C/C++) types. This section introduces the native types corresponding to both primitive Java types (e.g.,
double) and Java objects (including strings and arrays).
Strings are a particularly useful kind of Java objects. The JNI provides a set of string manipulation functions to ease the task of handling Java strings in native code. The programmer can translate between Java strings and native strings in Unicode and UTF-8 formats.
Arrays are another kind of frequently-used Java object. You can use JNI array manipulation functions to create arrays and access array elements.
The JNI supports a complete set of "callback" operations that allow you to invoke a Java method from the native code. You locate the method using its name and signature. You can invoke both static and instance (non-static) methods.
javapis a helpful tool to generate JNI-style method signatures from class files.
The JNI allows you to locate the field using the field's name and type signature. You can get hold of both static and instance (non-static) fields.
javapis a helpful tool to generate JNI-style field signatures from class files.
This section teaches you how to deal with exceptions from within a native method implementation. Your native method can catch, throw, and clear exceptions.
Native code can refer to Java objects using either local or global references. Local references are only valid within a native method invocation. They are freed automatically after the native method returns. You must explicitly allocate and free global references.
This section describes the implications of running native methods in the multi-threaded Java environment. The JNI offers basic synchronization constructs for native methods.
This section shows you how to load the Java Virtual Machine from a native library into a native application. This lesson includes how to initialize the Java Virtual Machine and invoke Java methods. The Invocation API also allows native threads to attach to a running Java Virtual Machine and bootstrap themselves into Java threads. Currently, the JDK only supports attaching native threads on Win32. The support for Solaris native threads will be available in a future release.
In C++, the JNI presents a slightly cleaner interface and performs additional static type checking.The following tutorials are also very good for Android app development with NDK: