#. create a Android project “MyAndroidProject”

#. In file manager create  “jni” directory in your project directory

#. create a jni/Android.mk

Simple example of Android.mk file:
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS    := -llog

LOCAL_MODULE    := native
LOCAL_SRC_FILES := native.c

include $(BUILD_SHARED_LIBRARY)


and place your C/C++ sources file here. Also put here Android.mk file which is a makefile that tells Android build-system how to build your files.

#.  create a jni/native.c

 

#include
#include

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
 */
//jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz )
jstring Java_com_powenko_Tutorial_NDK_MyAndroidProject_Tutorial_NDK_MyAndroidProjectActivity_stringFromJNI( JNIEnv* env, jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello PowenKo from JNI ! My first NDK");
}

#.Now create a folder called jni, and change perspective to C/C++. Right click the project in the Project Explorer tab and choose New->Convert to a C/C++ project. ChooseC++ Project, check Specify Project Type, set it to Makefile project and –Other Toolchain–. Click Finish.

 

After doing this Eclipse will ask you if you want to switch to C/C++ perspective. ChooseYes because otherwise you wouldn’t be able to set C/C++ build preferences.

 

 

#. Click on your project with right button and select Properties or press Alt+Enter

Properties windows will appear. Here you have to configure use of ndk-build instead of make all command and set proper include paths.

 

#. Choose C/C++ Build and configure ndk-build as a build command

In Builder settings fill ndk-build into Build command entry. You have to uncheck Use default build command. You also need to have ndk-build script in your PATH.

 

 

Click Apply to save settings.

8) Choose C/C++ General->Paths and Symbols and configure include path

In Includes tab choose GNU C or GNU C++ and click Add… button. Add path to include directory which is located in platforms/android-4/arch/arm/usr/include subdirectory of place where you’ve unpacked Android ndk. Include path depends on target for which you are compiling (android-4 in my case — i.e. Android 1.6).

 

 

 
sample code:
Tutorial_NDK_MyAndroidProject

By admin-powenko

Dr. Powen Ko is a teacher and CEO on LoopTek LLC, and like to teaching. if you need to class, please let PowenKo know, he will love to service and sharing. LoopTek web site is www.looptek.com

Leave a Reply