Pages

Label

Rabu, 01 Februari 2017

Generate .SO file by using NDK and use it in Android APP

Hello Guys !!! Hope You all are doing well.

Today I am going to Discuss how to generate .So file  by using NDK and how to use this file  as library in Android App by using Android Studio.

Obvious question  What is SO File??
Right I am giving brief detail...
In Android we are able to use C, C++ code known as a Native code for java. Android NDK  compiles this code into .so files. just like jar file in java to use it as library.  As per  Android NDK site below is  brief detail 

Application Binary Interface (ABI): The ABI defines exactly how your app's machine code is expected to interact with the system at runtime. The NDK builds .so files against these definitions. Different ABIs correspond to different architectures: The NDK includes ABI support for ARMEABI (default), MIPS, and x86. For more information, see ABI Management.

Now, I hope Guys I can proceed further.  Second Question How to create .SO file from Native Code?
  Here  I am describing it as a Step by Step procedure.

Step 1 Create a Folder (Let say hello-jni/jni)
I am using Ubuntu machine so let say under the u dir I have created one hello-jni folder and then one more jni folder. Then add three files inside this jni folder. Android.mk, Application.mk and hello-jni.c

Step 2 Modify content of MK file
Now open one by one 2 MK file. First open Android.mk file and copy paste following code in it.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
Open Application.mk file and copy paste follwoing code
APP_CFLAGS += -Wno-error=format-security
APP_ABI
:= all
# APP_ABI := armeabi armeabi-v7a x86

Step 3 Modify Native Code
Open hello-jni.c file and copy paste following code in it.
#include <string.h>
#include <jni.h>

JNIEXPORT jstring JNICALL
Java_softsolution_sks_com_myhellojni_MainActivity_getStringFromJNI(JNIEnv *env, jobject thisObj) {
return (*env)->NewStringUTF(env, "Hello from native code!");
}

jstring
Java_softsolution_sks_com_myhellojni_MainActivity_getJniString(JNIEnv* env, jobject thiz){
return (*env)->NewStringUTF(env, "Hello from JNI! Saurabh");
}

JNIEXPORT jint JNICALL
Java_softsolution_sks_com_myhellojni_MainActivity_getIntSqure(JNIEnv* env, jobject obj,jint value) {
return value * value;
}

JNIEXPORT jboolean JNICALL
Java_softsolution_sks_com_myhellojni_MainActivity_getBooleanMethod(JNIEnv* env,jobject obj, jboolean unsignedChar) {
return !unsignedChar;
}
Step 4 Generate .SO file by using NDK Build
This is the last step to generate .so file for your native code by using NDK Build command. After Generation this SO file, You can use it as lib in Your Android Studio Project.
 Open Your terminal(Ubuntu) and reached till your hello-jni folder.

As for Example u/hello-jni/jni . Then set Path variable for your NDK . Here I am assuming that You had downloaded NDK and kept it in u Dir.

export PATH=$PATH:/u/android-ndk-r14-beta1
 SYSROOT=u/android-ndk-r14-beta1/platforms/android-21/arch-arm/
After it run build command  
 ndk-build
That's it, It will generate .SO file for all ABI. Now Copy these ABI(.So file) and use it in your Android App. I am going to explain it below.

Using .So file in Android Studio 
With Latest Android Studio version(2.2.3), You are able to directly developed your app with NDK. 
But here i am discussing how you can use generated .so file in Android studio. 
  
Step 1 Create one new Project (or module in your existing Project) 
Let create one new Project/Module myhellojni in Android Studio. Then create a folder inside src main  as for example
 /src/main/jniLibs Then copy all your .so file with folder and paste it inside jniLibs like below.

Step 2 Add jniLibs path in build.gradle 

add below given path in hello-jni/build.gradle file under android{...} tag

sourceSets.main {
jniLibs.srcDir 'src/main/libs'}


Step 3  Use Native Method in MainActivity 
Now you can use native method inside your app activity. As for example

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

static {
Log.d(TAG,"Loads lib started..");
System.loadLibrary("hello-jni");
Log.d(TAG,"Load libs finished");
}

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView tv = (TextView)findViewById(R.id.hello_txt);
tv.setText(" hi "+getStringFromJNI());
}

public native String getStringFromJNI();
}


That's it.  Hope you guys Enjoy it.

Happy Coding!!!

Thanks
Saurabh

Note:- Please proved your comment and suggestion for my post. It will energize me.

0 komentar:

Posting Komentar

 
[tutup]