Eclipse ADT + NDK + CDT integrated development environment Configuration

Source: Internet
Author: User

NDK must be used to develop Android applications using Eclipse, but CYGWIN must be enabled to compile C/C ++ code. Although NDK and Cygwin can be integrated into Eclipse, however, opening the C/C ++ code under the Android project directory does not allow you to use "Open Declaration" or other functions, and cannot provide spelling, function does not exist, or other error prompts when editing the source code, the following configuration can avoid switching to Cygwin to compile C/C ++ code, and compile the source code in the Android project like in CDT, c/C ++ code compilation and Android code compilation are independent.


I. Preparations

Eclipse has been installed and the Cygwin, ADT, CDT, and Java environments have been installed.


2. Integrate CDT into the Android Environment

1. Create a new C Project named Test_C. You can select a Project type. Here, select Empty Project and Toolchains. Here, select Cygwin GCC and click "Finish"


2. right-click the newly created C Project and choose Properities-> C/C ++ Build. Make sure that the Builder type in Builder Settings is External builder and the Behaviour label is opened. The Build and Clean statuses are deselected.


3. Create an Android project Test and copy the. cproject file under the Test_C directory to the Android project directory.

At the same time, open the. project file under the Test_C directory, copy the <buildSpec> tag and the content under the <natures> tag to the corresponding tag of the. project file under the Android project directory.


4. Right-click the Android project Test, select Close project, and open it again. When the Test attribute is opened, the setting attribute of C/C ++ is added.


At this time, an error message will be displayed when we create a new C file.


This indicates that the CDT has been successfully integrated into the Android project. Next, add the corresponding header file.

Here, string. h is in the NDK directory, and jni. h is in the jdk directory. After adding the file, you can find that the function in the code can be highlighted and a prompt will be displayed when an error occurs. Next we will integrate NDK + CYGWIN


Iii. Integrate the NDK + Cygwin compiling environment into the Android Project

1. Open the Android Project Properties and select Builders-> New-> Program. Click OK.


2. Set the cygwin path and input parameters


Arguments is the input parameter. If it is too difficult to enter the project path every time, you can do this here:

Click Variables-> Edit Variables-> New. In the displayed dialog box, Set name To workspace_cyg and Value to the workspace directory e/android/workspace under the cygwin command line. The Arguments command can be changed to the following:

-- Login-c "cd/cygdrive/$ {workspace_cyg}/$ {project_name}/jni & $ NDK9/ndk-build"

($ NDK9 is the environment variable that defines the NDK directory in cygwin

$ {Project_name} is the variable that defines the project file name in eclipse.

)

3. Configure Refresh and Build Options as follows:

The configuration is complete. To compile the C/C ++ code, right-click the Android Project and choose "Build Project". The Android program will not run, the Android program compilation and C/C ++ code compilation can be separated here. Compile only C/C ++ code:

Attached hello-jni.c and Android. mk

#include <string.h>#include <jni.h>jstring Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv* env,jobject thiz) {return (*env)->NewStringUTF(env, "Hello from JNI !");}void Java_com_example_hellojni_HelloJni_printString(JNIEnv* env, jobject thiz,jstring str) {const char* ch = (*env)->GetStringUTFChars(env,str,NULL);(*env)->ReleaseStringUTFChars(env,str,ch);}

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := hello-jniLOCAL_SRC_FILES := hello-jni.cLOCAL_LDLIBS := -lloginclude $(BUILD_SHARED_LIBRARY)



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.