Android NDK on WindowsXP
Preparing :
jdk-6u32-windows-i586.exe
eclipse-java-indigo-SR2-win32.zip
installer_r18-windows.exe
ADT-18.0.0.zip
Before you start install Android NDk, you need make sure Android SDK set up correctly. (I installed cygwin on my computer, but the make process still works even I removed the cygwin. Maybe the current version of Android NDK already include some make tools)
android-ndk-r8b-windows.zip
Install NDK
unzip android-ndk-r8b-windows.zip to a directory, and add it’s ndk full path to PATH env variable.
Create an Android project in eclipse
Create an Android project in Eclipse, and supply the code as following:
package com.bruce.test; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class TestJniActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String strResult = unimplementStringFromJNI(); TextView tv = (TextView)this.findViewById(R.id.strView); tv.setText(strResult); } // The following are Jni calls public native String stringFromJNI(); public native String unimplementStringFromJNI(); static { System.loadLibrary("testJni"); } }
Create the Jni c/c++ header file according to the java .class files
Enter the command line, create new folder named “jni”under the android project directory, and input the command line as following:
$ javah –classpath ./bin/classes –d jni com.bruce.test.TestJniActivity
Note, please provide a correct value to option “classpath”, otherwise you will encounter error that could not find the specified .class file. And also make sure that this .class file already generated.
Implement the c/c++ function
After the above process, you will find a xxx.h generated located under $PROJECT_DIR\jni. What you need to do now is to write a xxx.c file to give a implementation for all interfaces. Here header file “com_bruce_test_TestJniActivity.h”generated as following:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_bruce_test_TestJniActivity */ #ifndef _Included_com_bruce_test_TestJniActivity #define _Included_com_bruce_test_TestJniActivity #ifdef __cplusplus extern "C" { #endif /* * Class: com_bruce_test_TestJniActivity * Method: stringFromJNI * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_bruce_test_TestJniActivity_stringFromJNI (JNIEnv *, jobject); /* * Class: com_bruce_test_TestJniActivity * Method: unimplementStringFromJNI * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_bruce_test_TestJniActivity_unimplementStringFromJNI (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
Here is the testJni.c:
#include "com_bruce_test_TestJniActivity.h" JNIEXPORT jstring JNICALL Java_com_bruce_test_TestJniActivity_stringFromJNI (JNIEnv *env, jobject obj) { return (*env)->NewStringUTF( env, (const char*)"String From JNI" ); } JNIEXPORT jstring JNICALL Java_com_bruce_test_TestJniActivity_unimplementStringFromJNI (JNIEnv *env, jobject obj) { return (*env)->NewStringUTF( env, (const char*)"unimplemented String From JNI" ); }
Compile them into a shared library
To compile those c/c++ file into a *.so shared library file, we need to provide a “Android.mk”file fist:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := testJni LOCAL_SRC_FILES := TestJni.c include $(BUILD_SHARED_LIBRARY)
Then compile it under the command line, switch to the project folder first,
$ ndk-build.cmd
Now, you could run the Android application now. Right click the project, and select Run –> Run As Android Application:
The full source code could be found here.