Using C ++ Code in Android Application

Source: Internet
Author: User

0. Environment

Windows 8x64

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 shocould not be added to

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 cocould 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.