0% found this document useful (0 votes)
196 views3 pages

Compile Jni Code in Android Studio

This document provides instructions for compiling C/C++ code to create a native library for use in an Android application project created in Android Studio. It outlines the steps to: 1. Create an Android project in Android Studio and select an empty activity. 2. Add a "jni" folder under "app/src" and place C/C++ source files (.c files) inside. 3. Create "Android.mk" and "Application.mk" files in the "jni" folder to configure the native library build. 4. Run the "ndk-build" command to build the native library files (.so) and move them to the "jniLibs"

Uploaded by

Ravi Bhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views3 pages

Compile Jni Code in Android Studio

This document provides instructions for compiling C/C++ code to create a native library for use in an Android application project created in Android Studio. It outlines the steps to: 1. Create an Android project in Android Studio and select an empty activity. 2. Add a "jni" folder under "app/src" and place C/C++ source files (.c files) inside. 3. Create "Android.mk" and "Application.mk" files in the "jni" folder to configure the native library build. 4. Run the "ndk-build" command to build the native library files (.so) and move them to the "jniLibs"

Uploaded by

Ravi Bhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy