Creating Static Llibrary for Android app

by admin on January 25, 2016

Environment

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.

Requirements

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.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

native String helloStringFromJNI();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tvText = (TextView) this.findViewById(R.id.tvText);
Button btgetnative = (Button) this.findViewById(R.id.btcall_native);
btgetnative.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
tvText.setText(helloStringFromJNI());
}
});
}

static {
System.loadLibrary("hello-jni");
}
}

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="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
&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;TextView
android:id="@+id/tvText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
/&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;Button
android:id="@+id/btcall_native"
android:text="run native"
android:padding="3dp"
/&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&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;&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>

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

or

<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 Android.mk. 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

include $(BUILD_STATIC_LIBRARY)
[/sourcecode]
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"

jstring
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