Creating Static Llibrary for Android app

by admin on January 25, 2016


This project has been done on windows 7 using NetBeans as the IDE of choice.

Am using ant scripts to build the project.

Am NOT using cygwin or mini cygwin. I found this installations to be too bulky and i really avoided them.


Set the following Environment Variables.

  • name: ANDROID_HOME  value: <sdk path e.g C:\android\android-sdk>
  • name: ANDROID_SDK_HOME value: <sdk path e.g C:\android\android-sdk>
  • name: ANDROID_NDK_ROOT value: <ndk path e.g C:\android\android-ndk-r10e>
  • name: NDK_HOME  value: <ndk path e.g C:\android\android-ndk-r10e>
  • name: JAVA_HOME  value: <jdk path e.g D:\Program Files\Java\jdk1.7.0_17>
  • (optional) set this if you are building with ant.   name: ANT_HOME  value: <ant path e.g C:\Users\somename\AppData\Roaming\NetBeans\7.3.1\ant>
  • this will be used by the tool chain (increasing memory limits) .   name: _JAVA_OPTIONS value: -Xms256m -Xmx512m
  • name: PATH value: (append the following to your current path value using semi colon (;) as a separator) <sdk path>\platform-tools;<ndk path>;<jdk path>\bin

Create new android project in you IDE. (give it any name e.g hello_ndk)

sample code for MainActivity as follows:

 package com.hndk;&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/code&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;amp;lt;/code&amp;amp;amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;/code&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/code&amp;amp;amp;amp;gt;&amp;amp;lt;/code&amp;amp;gt;&lt;/code&gt;</code>

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

native String helloStringFromJNI();

public void onCreate(Bundle savedInstanceState) {
final TextView tvText = (TextView) this.findViewById(;
Button btgetnative = (Button) this.findViewById(;
btgetnative.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {

static {

Note: function helloStringFromJNI() has been declared as native, this is a keyword that says we want to call a function that has been implemented in a native code  library  (C language).

also note how we use the keyword static and the function System.loadLibrary("some library"), to load the library.

some library is just the name of the library without the extension. The Extension .so or .a is added automatically on compiling.

sample code for the UI main.xml

&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;?xml version="1.0" encoding="utf-8"?&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;LinearLayout xmlns:android=""
android:text="Hello World, MainActivity"
android:text="run native"

Now build you android code.

after successfully building,   you run the following commands in command prompt window.

Change directory to the location of your built classes e.g

cd <your project folder location>\bin\classes\
Call javah to build your header file as follows ( if you are using NativeApp class). javah -jni <fully qualified package name>.<you class name>

e.g javah -jni com.hndk.MainActivity

you can also use the full path to javah location as follows.

 <path to jdk>\bin\javah.exe -jni <fully qualified package name>.<you class name>


Call javah to build your header file as follows ( if you are using  normal Activity class e.g MainActivity ).


javah  -classpath <path to platform you are supporting> -jni <fully qualified package name>.<you class name>

e.g javah -classpath "<path to sdk>\platforms\android-8\android.jar"; -jni com.hndk.MainActivity


<path to jdk>\bin\javah.exe -classpath "<path to sdk>\platforms\android-8\android.jar"; -jni com.hndk.MainActivity

On success your output header file will be located in <your project folder location>\bin\classes\

Create a new folder called JNI in you project root folder. Copy the generated C header file to this new folder.

In the sane folder create a file called Add the following to the file

[sourcecode language=assembly]
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello_ndk /*any unique name without spaces*/
LOCAL_SRC_FILES := hello_ndk.cpp

also add a file called hello_ndk.cpp . This file will have the following code (it is our LOCAL_SRC_FILES above)

// hello_ndk.cpp&amp;amp;lt;/code&amp;amp;gt;&lt;/code&gt;</code>

#include "com_hndk_MainActivity.h"

Java_com_hndk_MainActivity_helloStringFromJNI( JNIEnv* env, jobject thiz )
// (*env)-&amp;amp;amp;amp;amp;amp;gt;ReleaseStringUTFChars(env, s, str);
// env-&amp;amp;amp;amp;amp;amp;gt;ReleaseStringUTFChars(s, str);
return env-&amp;amp;amp;amp;amp;amp;gt;NewStringUTF("Hello World from JNI! yepeee");


Now building the c++ library

dowload the following resorces :

Leave a Comment