Most Android applications are written in a high-level programming language such as Java. Sometimes it is necessary and/or desirable to have parts of the application written in a low-level language such as C. The Java language includes methods for running code written in low-level languages by using the Java Native Interface (JNI). A JNI interface is composed of some Java code and some C code with bindings between them. The interface allows function calls and data to be exchanged both ways between the Java and native C code.
The following process may be used to incorporate native C code in a Java application on Android:
- Define a Java interface that the Java application will use to communicate with the native library.
- Use an automated tool (javac) to auto-generate a C header file from the Java interface.
- Write C code that uses the header file and implements all of the functions defined by it.
- Generate a shared object (.so) file from the C code.
- Load the shared object into the Java application and call the methods on the interface.