Most people have started their android journey by learning Java or Kotlin. These are the languages that are typically used while making android apps. While exploring further about Android Development, you come across many new topics. One of the new and unique topics is NDK. Before hearing about NDK, you may be familiar with SDK(Software Development Kit) which supports Java. So now let’s explore what is NDK and why is it useful?
What is NDK?
NDK which stands for Native Development Kit is a set of tools or a toolset that will allow us to use C/C++ in our Android Application. Isn’t that cool? It is called Native because of the use of native-code languages in it such as C and C++.
Why is NDK Useful?
Now, we will answer this question with the help of an example. Let’s say you want to add a critical function or a specific processor-dependent code to your own app which will require high performance, using Java only. It would not succeed so in order to get the following requirements, you have to use C or C++ which will make it run faster. But even if we know C or C++, how would we implement it in our app? So this is where we require NDK to integrate the native-code languages to make it run on the app. Let’s see some more advantages of using NDK:
- It provides a way to embed the equivalent native libraries in the apk.
- It mostly runs without any recompilation.
- It can be used to access physical device components like sensors.
- It gives us the ability to reuse the code written in C/C++.
How Does it Work?
This brings us to our next question, How will the java code communicate with the native code? We would understand the question from the image given below:
First of all, we have to write a C/C++ code then after that, we have a Makefile in which we specifying the platform this application needs to target. We use ndk-build command to build the shared object libraries of the .so file. Then Java program uses these shared object libraries and calls the native functions through JNI. JNI is the Java Native Interface which defines a way for the bytecode that Android compiles the code written in Java or Kotlin to interact with the native codes.
So by reading this article we learned about how native languages like C or C++ are used in the android app. When we do not require some high-performance app, we can normally use Java or Kotlin but if we require a high-performance app, we need to use native languages to do so.