Using C++ Code in Android Application

來源:互聯網
上載者:User

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.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-

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 <.h><jni.h>* (*env)->NewStringUTF(env, * num1 +

If the file is "TestJNI.cpp"

#include <.h><jni.h> ** Dev_call( a, *d =  d->* env->NewStringUTF(*

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

LOCAL_PATH := $(call my-==

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

LOCAL_PATH := $(call my-== TestJNI. Dev.

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;"

        add( num1, "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.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> LOG_TAG "ndkmain" LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__));

In "Android.mk" file, add:

LOCAL_LDLIBS += -llog

 

http://www.bkjia.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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.