Android NDK For Beginners

Most of Android developers use Java to develop there own applications. and since I was one of them I start developing something that consume the CPU and lots of memory and other stuff, so I was tying to get more depth in how to utilize this and manage the memory issue so I get NDK as a search result !.

I know what is the NDK but I was thinking its much complicated to use it and its so hard to write code since its uses the CPP code or lets say pure C programming language.

BUT , I should do something to the performance of my APP so I decided to get more in depth to this NDK and how I should start with it ! and that was my major problem !.

The most important point is that the NDK does not replace the SDK you should have both so you use the NDK to put your native pieces of code into libraries that you can then consume from the Java code. Thus, you always start with a standard (Java) app and then add NDK pieces to it.

how and where to start ?

I start googling this day by day nd I have no real results. and finally I got this thing up !. the only problem is that I’m a JavaSE and JavaEE developer I never think that I need to add a C r C++ plugin to the Eclipse ! so I decide to write down a fast point to point tutorial to do everything and get you to the first point.

  1. your first step is to download the C/C++ support on Eclipse: just open your eclipse, go to the menu bar Help -> install new software.
    from the dialog in the work with field choose your eclipse version for me its Galileo, then wait to pending  urls.
    now you should find the in the pending  urls ” Programming Language” inside it you will find the C/C++ tools needed for code coloring and editors.
    now your eclipse is ready to write a C code.
  2. Now you should download the Cygwin. since the Android is based on Unix and to develop Native you need some Lib in the Unix environment so this will emulates Unix environment on Windows.
    go to http://www.cygwin.com/
    and download the setup.exe and please make sure to use a directory with no spaces when you install it.
    now you should see the icon on the desktop for this console double click and type : make -v to make sure that the needed Lib for the NDK are installed if error happened you should restart this step.
  3. Now you should download the Android NDK it self from: http://developer.android.com/sdk/ndk/index.html

Great news this is done and your are ready to start you first NDK Project. Now, lets create a new Android project and then:

  • Create a Folder and call it “jni” beside the res Folder this folder will contain all of your JNI’s and native code.
  • Create a file inside this folder and call it “Android.mk” with the following content:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# This will create our module name
LOCAL_MODULE    := ndktest
# module source file
LOCAL_SRC_FILES := ndktest.c
include $(BUILD_SHARED_LIBRARY)
  • this mean that we have one module and this module may be consists of many source code files.
  • now in the jni folder create new file which is the C code file you should named it “ndktest.c” as we gave this name the “Android.mk” file. the content of this file is :
#include <string.h>
#include <jni.h>
jstring getStringFromNativeFunction(JNIEnv* env,jobject javaThis)
{
  return (*env)->NewStringUTF(env, "Hello World!");
}
  • Now, you should create a “bin” library from this C source code, in windows environment you should use the Cygwin and Android NDK tools.
    So, Launch the Cygwin console and go to your project dir. you should notice that Windows drives are seems to be under “/cygdrive”.
    Then you should use the NDK to build the source code within this console in my case I just typed in the console : /cygdrive/c/android-ndk-r4/ndk-build since my NDK is installed on C:/ drive.
    You you will se a new Folder called “libs” and a file inside it has an extension “.so” if you back to your eclipse and refresh the workspace you should apple to see these file and folder.

another Great news :D the NDK is done ! and now you can use this into your project. but make sure that you need to do these steps each time you edit the source code. and to complete our example this the activity and how to use this Native method.

package daigoor.bdk.test.activities;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
 
public class NdkTestActivity extends Activity {
 
  // This will load the lib at the class creation phase
  // and the name of the loaded lib should match the
  // "Android.mk" file.
  static {
      // ndktest is the module name
    System.loadLibrary("ndktest");
  }
 
  // declare the native method code
  // it should match the name in the source code
  private native String getStringFromNativeFunction();
 
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        // call the method
       String str= getStringFromNativeFunction();
        // try to Toast the str
    }
}
HINT : you should think deeply to use the NDK is a bad idea to mix Java with C code this will reduce the code quality. and you should also read more about the JNI. for this purpose the best source is to read it from the oracle docs :
http://java.sun.com/docs/books/jni/

at the end : HaPpY CoDiNG ;)
02.01.12
80 notes
  1. daigoor posted this