Saturday, February 9, 2013

Mixing Java and C/C++ Code On Android Development

Calling a C/C++ function from Java is actually quite easy but requires several steps:
The native method must be declared in your Java code.
The Java Native Interface (JNI) glue layer needs to be implemented.
Android makefiles have to be created.
The native method must be implemented in C/C++.
The native library must be compiled.
The native library must be loaded.
It really is easy in its own twisted way. We will go through each one of these steps, and by the end of this section, you will know the basics of mixing Java and C/C++. We will discuss the more intricate details of the Android makefiles, which allow you to optimize your code even more, in later sections. Since the Android NDK exists for Linux, MacOS X, and Windows (with Cygwin, or without when using NDK revision 7), the specific steps may vary slightly although the overall operations will remain the same. The following steps assume an Android project is already created and you now want to add native code to it.

No comments:

Post a Comment