How to add C/C++ code to Android Studio using Android NDK

Akash Srivastava
3 min readAug 7, 2018

--

As android studio support to add C/C++ code to your project, so today we will talk about how to do that.

So C/C++ code are basically know for fast executing performance, an inorder to add the existing C/C++ library or some back end C/C++ code, Android has Android NDK so that you can execute C/C++ methods from your java program of Android project.

Android developed Android NDK https://developer.android.com/ndk

In this article we will go through the whole process of adding C/C++ code

Step 1:

Create your android project able to support NDK

In order to support NDK in your project you must download NDK.

To download NDK in your android studio, go to SDK manager and you will see NDK in SDK tools download it from there.

If you are working on some other IDE you can download it from here: https://developer.android.com/ndk/downloads/

Step 2:

Create a JNI folder in your android project

Create a JNI folder in your project’s root directory name it “jni”.

This JNI folder would contain your C/C++ libraries(in next step) as well as your wrapper class and files such as “Android.mk” and “Application.mk” which tell gardle about your C/C++ libraries.

Step 3:

Add your C/C++ code

Now create a new directory inside “jni” folder name it anything you want to repesents your library.

Add all your .h and .c file to it.

Step 4:

Create Android.mk file

Now we will create an android.mk file inside jni folder which will contain path to all libs inside your lib folder.

For example:

LOCAL_PATH :=$(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := *your_lib_name*

LOCAL_SRC_FILES :=\

./lib/hello.c \

./lib/bey.c

…… /* Add .c files here*/

…..

LOCAL_LDLIBS:= -llog

include $(BUILD_SHARED_LIBRARY)

Step 5:

Create Application.mk file

This file describe the app platform and ABI .

For example,

APP_PLATFORM := android-14 APP_ABI := all

Step 6:

Adding NDK build to your app gardle

You can simple add reference to above created file to NDK(else they would not work in java)

If you are working in android studio, just right click on your project root directory in android view of your project, and you will see an option of “add C/C++ code” option, after clicking it you would get a dialogue where you may choose NDK instead of cmake.

Add the path of Android.mk file to it and press ok

Hurray files would be added to cpp folder in your project after gardle has been synced.

You can also do it directly by going into app’s gardle file and add inside android{……

externalNativeBuild {

ndkBuild{

path ‘../jni/Android.mk’

}}}

Step 7:

Creating .so file

Now we have to create .so file so that it could be used by Android for future reference to access methods

Just open your terminal and change directory to your project’s root.

Now type the following command

Path_to_your_ndk/ndk-build

To know your NDK path go to the

file in android studio ->project structure ->SDK Location -> copy the NDK Location.

If no error occurs you will get the following output :-

SharedLibrary : lib.so

Install : lib.so => libs/x86/lib.so

So in this article we have successfully imported all the C/C++ libs to our android project.

In the next article I will tell you how to call the methods of C/C++ code in java using a wrapper class.

Thanks, stay tuned 😉

--

--