Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Guide To Compile jni

code in Android Studio


01 JUNE 2016 / 4:00 PM / Videocon R&D

Create Android Project


Open Android Studio.
Select Start new Android Studio Project.
Set Application name. In this guide I will use jniCompile.
Set Company name. In this guide I will use ord.d2h.videocon.
hit Next
In the Select the form factors your app will run on I used Phone and Tablet and
selected API 9: Android 2.3 (Gingerbread) but you can try other setups.
hit Next.
Select Empty Activity and hit Next.
Leave Activity Name and all the other fields as is and hit Next.

Jni Folder
1. In the Project file viewer find path jniCompile / app / src
2. On src folder do Right Click -> New -> Directory Name it jni and hit OK.
3. If jni folder already available put it under src.

C Library File
All the c files should be placed under jni folder.
Example of how to define methods in c

The function name Java_eu_ratikal_helloc_MainActivity_getString is based


on this pattern Java_<package-name>_<activity-name>_<function-name>.
* <package-name> can be found in the fist line of projects MainActivity.java
file.
* <activity-name> can also be found in MainActivity.java file and is the class
name, in this case MainActivity.
* <function-name> you decide it! In this example id getString.

Android.mk

1.
2. Inside jni folder create a file named Android.mk (same procedure with HelloC.c).
3. Android.mk is a the file that instructs ndk-build how to compile and create the library

for java to call the C.


4. Add the following code in Android.mk
5. LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE

:= helloc

LOCAL_SRC_FILES := HelloC.c
include $(BUILD_SHARED_LIBRARY)

Skip if file already available

Application.mk
Inside jni folder create a file named Application.mk (same procedure with HelloC.c and
Android.mk).
Application.mk is a the file that instructs ndk-build how to compile and create the library for java to
call the C.
Add the following code in Application.mk
APP_ABI := armeabi armeabi-v7a mips x86
APP_PLATFORM := android-9

You can skip creating Application.mk if you now that armeabi is fine for your test
device. If you dont understand this, create the file and probably you are going to
be ok!

Build the library


Now build the library!

3
In the terminal of Android studio give the below command to generate the .so files
$ cd app/src/jni
app/src/jni $ *path-to-ndk-directory*/ndk-build

Move files to jniLibs folder


jniLibs is the folder where function System.loadLibrary(..) searches for native
libraries.
ndk-build created folder jniCompile/app/src/libs. Rename and move this folder to
jniCompile/app/src/main/jniLibs.

Load and Call the native C function from java


Use System.loadLibrary(name of .so file without extension) in a static block to
import in a separate utility class . This class should declare the methods of lib with native keyword

This can be done within activity or any class but recommended to create a separate utility class
for code cleanliness

You might also like