0. Environment

Windows 8 x64

Android SDK (adt-bundle-windows-x86, http://developer.android.com/sdk/index.html)

Android NDK (android-ndk-r8e, http://developer.android.com/tools/sdk/ndk/index.html)

Cygwin

1. Steps

1.0 Setup Android develop environment

1.1 Unzip NDK zip file to a folder without space (such as "D:\GreenProgramFiles\android-ndk-r8e")

1.2 Install Cygwin

1.3 Add environment variables
In the user folder, edit ".bash_profile" file, add the following lines (modify the path to your ndk installation folder):

NDK_ROOT=/cygdrive/D/GreenProgramFiles/android-ndk-r8eexport NDK_ROOT

restart cygwin

1.4 Add android project in the eclipse

1.5 Add "jni" folder in the project root (do it in eclipse)

1.6 Add C/C++ code "TestJNI.c" in the "jni" folder

#include <string.h>#include <jni.h>jstring Java_ac_cas_is_hci_testjni_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz){    return (*env)->NewStringUTF(env, "Hello from JNI ! And I am happy !");}jint Java_ac_cas_is_hci_testjni_MainActivity_add(JNIEnv* env, jobject thiz, jint num1, jint num2){    return num1 + num2;}

If the file is "TestJNI.cpp"

#include <string.h>#include <jni.h>#include "Dev.h"extern "C"{    jstring Java_ac_cas_is_hci_testjni_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz);    jint Java_ac_cas_is_hci_testjni_MainActivity_add(JNIEnv* env, jobject thiz, jint num1, jint num2);}int Dev_call(int a, int b){    Dev *d = new Dev();    return d->dev_add(a, b);}jstring Java_ac_cas_is_hci_testjni_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz){    return env->NewStringUTF("Hello from JNI !");}jint Java_ac_cas_is_hci_testjni_MainActivity_add(JNIEnv* env, jobject thiz, jint num1, jint num2){    return Dev_call(num1, num2);}

1.7 Add "Android.mk" in the "jni" folder

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := TestJNILOCAL_SRC_FILES := TestJNI.cinclude $(BUILD_SHARED_LIBRARY)

Or the following if you have multi-cpp file, and notice that the .h file should not be added to the LOCAL_SRC_FILES

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := TestJNILOCAL_SRC_FILES := TestJNI.cpp Dev.cppinclude $(BUILD_SHARED_LIBRARY)

The document file "ANDROID-MK.HTML" in your ndk installation folder will tell you how to use "Android.mk"

1.8 Add "Application.mk" file to the "jni" folder if you want to use "#include <vector>" in your C++ code

APP_STL := stlport_static

For more information about "Application.mk", see "APPLICATION-MK.HTML" document in your ndk installation folder

1.9 Modify "MainActivity.java" file in package "package ac.cas.is.hci.testjni;"

    public native String stringFromJNI();    public native int add(int num1, int num2);    static    {        System.loadLibrary("TestJNI");    }

You can also put these code in a seprated file, such as "NdkCall.java", and the functions could also be static

1.10 Compile C/C++ code in cygwin

cd /cygdrive/D/workspaces/eclipse_android/TestJNI//cygdrive/D/GreenProgramFiles/android-ndk-r8e/ndk-build

My eclipse workspace is "D/workspaces/eclipse_android" and my project is in "D/workspaces/eclipse_android/TestJNI"

After this, a "libTestJNI.so" file will be generated in the "<project>/libs/armeabi/"

1.11 Run the project in eclipse

2. Some other things

2.0 Read "http://developer.android.com/tools/sdk/ndk/index.html#GetStarted" for more detail information

2.1 Java data type, JNI data type and C++ data type match table see:
http://blog.csdn.net/pfgmylove/article/details/7052839

2.2 How to print debug information from C++ code?

You can not use printf function in C++ code, because you can not find where the information is sent.

Use Android log module to do that thing, so you can find the debug information from LogCat.

In C++ code add:

#include <android/log.h>#define LOG_TAG "ndkmain"#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)LOGI("NDK is called!");

In "Android.mk" file, add:

LOCAL_LDLIBS += -llog

3. References

http://www.2cto.com/kf/201104/87831.html

http://developer.android.com/tools/sdk/ndk/index.html

http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/

http://blog.csdn.net/L____J/article/details/5787759

http://www.cnblogs.com/lsnproj/archive/2012/01/09/2317519.html

(This article is from http://www.cnblogs.com/chenyineng/archive/2013/05/13/3076151.html, and belongs to http://chenyineng.cnblogs.com and http://www.chenyineng.info)

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android(安卓)Webview 使用Html播放音频
  2. Java序列化反序列化、Serializable、Parc
  3. WhaleCTF平台Android部分题解
  4. Android(安卓)ApiDemos示例解析(45):App->
  5. Android(安卓)Studio断点调试三(深入研究)
  6. 【Android】入门案例(一)——简单登录
  7. 了解Android(安卓)Studio jniLibs目录
  8. 详解Android提交数据到服务器的两种方式
  9. 自定义MediaPlayer控制组件
  10. Android(安卓)同时setTag两次保存多种值