Using C++ Code in Android Application

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-r8e
export 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 := TestJNI
LOCAL_SRC_FILES := TestJNI.c
include $(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 := TestJNI
LOCAL_SRC_FILES := TestJNI.cpp Dev.cpp
include $(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)

posted @ 2013-05-14 11:33  Yineng  阅读(813)  评论(0编辑  收藏  举报
Homepage: www.chenyineng.info
Copyright 2010 - 2014 Yineng Chen. All rights reserved.