I’m new to the Android NDK. I want to know what is the benefit of native code in Android. How does it improve performance, and where is it (native code) used in Android?
The NDK allows you to write code using C/C++ and then link it into your Java application. You can potentially increase the speed of your application. However, it may be worth reading about Replica Island, as they don’t use the NDK, however achieve very fast frame rates.
The downsides to the NDK are, it only compiles to specific CPUs (whereas staying in Java land means it will work on any targetted version of Android).
You can use native code, to optimize your application for speed. Especially when bit/byte operations are used in your code, like when you need to do compression / decompression of image files etc.
Native C code would use the underlying operating system’s (linux) APIs (system calls), and hence would be much faster than when java code would have to be interpreted through the JVM.
Besides, allocation of large memory (to the tune of 25-30MB even!) can be done using native C code, using malloc().
This would not be possible when coding through java, in the confines of the application.
Most games use the native C code libraries for 2D / 3d graphics, input, sound etc…
If you are just beginning with the NDK, check out the following link for an easy tutorial with screenshots: