Pages

Label

Tampilkan postingan dengan label .so file. Tampilkan semua postingan
Tampilkan postingan dengan label .so file. Tampilkan semua postingan

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.

Sabtu, 21 Januari 2017

Add .so file in Android AOSP source code

Hello Guys !!! Hope You are doing well.
Today I am going to discuss How to add .so file in Android AOSP source code external folder.
You can add your prebuilt library in Android AOSP source code and it be a part of your AOSP System Image. I am describing step by step procedure for it.

Step 1 Create a folder ( let say myLibs) inside external folder of AOSP source code.

external folder of AOSP source code refers to external open source libraries.
That means libraries that the Android platform depend upon but that are not primarily developed and maintained by the Android open source project.

examples are webkit for the browser, FreeType for fonts, SqlLite for databases and so on. As more features are added to Android, more of these libraries are included in external.

Step 2 Create a Android.mk file

Create a Android.mk file inside your folder(let say myLibs) and copy your .so file in it.
You can use following content for your android.mk file
# Prebuilt Lib
LOCAL_PATH
:= $(call my-dir)
include $
(CLEAR_VARS)
LOCAL_MODULE
:= libMyabc # your lib name
LOCAL_SRC_FILES
:= libMyabc.so
# your lib .so file name
include $
(BUILD_SHARED_LIBRARY)
Step 3 Add your library in Framework
In final step you have to add your library in Android AOSP framework makefile so that it will recognise and build as a part of System image.
You find Framework Android.mk file on following location
/android_aosp_sourcecode_download_folder/frameworks/base/core/jni/
Open Android.mk file and add your library in following section
LOCAL_SHARED_LIBRARIES := \
You can put your library name in that section example libMyabc \
That's it... now make it (make -j4) and you find your added so file in following folder
/android_aosp_sourcecode_download_folder/out/target/product/generic/obj/lib
with file name like :- libMyabc.so and libMyabc.so.toc
and you also found it in system/lib folder
/android_aosp_sourcecode_download_folder/out/target/product/system/lib

Thanks
Saurabh
Happy Coding...
 
[tutup]