Use eclipse + cygwin + CDT + ndk to create a simple Android JNI Project

Source: Internet
Author: User

Before reading this article, you must ensure that the preliminary work has been completed. The Android development environment has been set up, cygwin has been installed successfully, and the CDT plug-in of eclipse has been integrated with OK, the ndk has been decompressed normally (the DNK version is android-ndk-r8b and must ensure that the ndk version is no less than 5 ).

1. For files in cygwin: Change the cygwin_root \ home \ Administrator \. bash_profile file (cygwin_root is the installation root directory of cygwin software). Add the following content at the end of the. bash_profile file:

Android_ndk_root =/cygdrive/h/android-ndk-r8b
Export android_ndk_root

/Cygdrive/h/android-ndk-r8b is cygwin software protocol ndk decompression root path, cygdrive can be understood as cygwin software protocol, H/android-ndk-r8b is the path of ndk.

2. Create an android project. The activity name in this project is mainactivity, and then create a JNI folder. The JNI folder is used to store MK and C files.

3. Configure the android project:

3.1 create a new builders, as shown in:

When you click 2, select the "program" option to name the new builder, such as new_builder (1 ).

3.2 configure the main panel of Builder as follows:

1.select bash.exe under cygwinsoftware bin; 2. select the bin folder under the cygwin software; 3. -- login-c "CD/cygdrive/G/workspace/hellojni3 & $ android_ndk_root/ndk-build" g/workspace/hellojni3
The path of the current Android project. $ android_ndk_root is the variable defined in the initial step.

3.3 configure the refresh panel:

1. Select "specific resource" 2. Specify a specific resource and select the libs file under the android project; 3. Select "recursively include sub-Folders"

3.4 configure the build options panel:

1. Select when to build as needed. 2. Specify resources select the newly created JNI folder in this android project.

4. The mainactivity code is as follows:

Package com. example. hellojni3;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;

Public class mainactivity extends activity {

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Textview = (textview) findviewbyid (R. Id. textview );
Textview. settext (fromstring ());

}

Public native string fromstring ();

Static {
System. loadlibrary ("hello ");
}

5. Create an android. mk file under the JNI folder. The content is as follows:

Local_path: = $ (call my-DIR)

Include $ (clear_vars)

Local_module: = Hello
Local_src_files: = hello-jni2.c

Include $ (build_shared_library)

Local_module corresponds to system. loadlibrary in the above acitivity. Local_src_files is the name of the C file, creating a hello-jni2.c file in the JNI folder,

The content is as follows:

# Include <string. h>
# Include <JNI. h>

Jstring
Java_com_example_hellojni3_mainactivity_fromstring (jnienv * ENV,
Jobject thiz)
{
Return (* env)-> newstringutf (ENV, "from string! ");
}

Jstring corresponds to the string class in Java. java_com_example_hellojni3_mainactivity is the Java class that starts with Java and contains the native method (that is, Com. example. hellojni3.mainactivity), fromstring method corresponds to public native string fromstring (); Method in mainactivity.

6. recompile the c file as follows:

7. The running result is as follows:

I hope it will be helpful to everyone. learn from each other and improve.

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.