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:
1.
The native method must be declared in your Java code.
2.
The Java Native Interface (JNI) glue layer needs to be implemented.
3.
Android makefiles have to be created.
4.
The native method must be implemented in C/C++.
5.
The native library must be compiled.
6.
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