Compile Jni Code in Android Studio
Compile Jni Code in Android Studio
Compile Jni Code in Android Studio
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
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
:= helloc
LOCAL_SRC_FILES := HelloC.c
include $(BUILD_SHARED_LIBRARY)
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!
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
This can be done within activity or any class but recommended to create a separate utility class
for code cleanliness