Eclipse ndk Configuration

Source: Internet
Author: User

1. About ndk:
Ndk stands for Native Development Kit.
1. ndk is a collection of tools.
Ndk provides a series of tools to help developers quickly develop C (or C ++) dynamic libraries, and can automatically package so and Java applications into APK together. These tools are of great help to developers.
Ndk integrates with the cross compiler and provides MK files to isolate differences such as CPU, platform, and Abi, developers can create so simply by modifying the MK file (indicating "which files need to be compiled" and "Compilation feature requirements.
Ndk can automatically package so and Java applications, greatly reducing developers' packaging work.
2. ndk provides a stable and functional API header file statement.
Google explicitly states that this API is stable and supports the currently released API in all subsequent versions. From the ndk version, we can see that these Apis support very limited functions, including: C standard library (libc), standard Math Library (libm), and compression library (libz), log Library (liblog ).

II. Implementation of ndk instances:
For the development of ndk in windows, if the ndk is a version earlier than R7, cygwin must be installed before ndk can be used. Therefore, the builder to be configured for eclipse is actually executing cygwin, then pass ndk-build as the parameter. Starting with ndkr7, Google's windows ndk provides a ndk-build.cmd script that can be compiled directly using this script without the need to use cygwin. You only need to add a builders for the eclipse Android project to enable eclipse to automatically compile the ndk.

This article is an example of the implementation of NDK-r7.
The following is the process of configuring automatically compiled builders in windows with a NDK-r7 (in fact for Linux, you only need to modify the ndk-build.cmd to ndk-build .).
(1) first download and install the NDK-r7.
: Http://developer.android.com/sdk/ndk/index.html
Decompress the package and you can use it.
(2) Open eclipse, create an android Project (my name is testndk), and create a JNI folder under the project directory testndk. This folder is used to save the file code that ndk needs to compile.
(3) create and configure a builder:
(A) Project-> properties-> builders-> New, create a builder.
(B) In the pop-up choose configuration type dialog box, select program and click OK ]:
(C) In the pop-up edit configuration dialog box, configure the tab main ].
Enter the name of the new builders in "name" (my name is ndk_builder ).
Enter the path of the nkd-build.cmd in location.
(I am D: \ androiddev \ android-ndk-r7 \ ndk-build.cmd, according to their ndk path settings, you can also click "Browser file system ..." To select the path ).
In "working diretcoty", enter $ {workspace_loc:/testndk} (you can also click "Browse workspace" to select the testndk directory ).
 
(D) refresh on the configuration tab in the edit configuration dialog box ].
Check "Refresh resources upon completion ",
Select the "the entire workspace ",
Select recuresively include sub-folders ".
 
(E) In the edit configuration dialog box, click the configuration tab build options ].
Select "after a" clean "",
Check "during manual builds ",
Check "During Auto builds ",
Select specify working set of relevant resources ".
 
Click "specify resources ..."
Select the "JNI" directory of the testndk project and click "finish".
Click "OK" to complete the configuration.
OK. Now eclipse can automatically call ndk to compile the C/C ++ code under the Jin directory.
(4) Create a New jniclient. Class (to call the C/C ++ Code) in the testndk project. The content is as follows:
Package com. ndk. test;

Public class jniclient {

Static public native string addstr (string stra, string strb );
Static public native int addint (int A, int B );
}
(5) use the CMD command to locate jniclient. class directory, enter "javac jniclient. java "and press enter to generate jniclinet. class file (if it is a project created using eclipse, jniclinet already exists in the testndk \ bin \ Classes \ com \ ndk \ test directory. class file ).
(6) set jniclinet. copy the class to the testndk \ bin \ Classes \ com \ ndk \ test directory, locate the CMD command to the testndk \ bin \ Classes directory, and enter "javah COM. ndk. test. after jniclient ", press enter to generate the C ++ header file com_ndk_test_jniclient.h in the directory testndk \ bin \ Classes. The content of the com_ndk_test_jniclient.h file is as follows:
/* Do not edit this file-it is machine generated */
# Include <JNI. h>
/* Header for class com_ndk_test_jniclient */

# Ifndef _ included_com_ndk_test_jniclient
# DEFINE _ included_com_ndk_test_jniclient
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: com_ndk_test_jniclient
* Method: addstr
* Signature: (ljava/lang/string;) ljava/lang/string;
*/
Jniexport jstring jnicall java_com_ndk_test_jniclient_addstr
(Jnienv *, jclass, jstring, jstring );

/*
* Class: com_ndk_test_jniclient
* Method: addint
* Signature: (ii) I
*/
Jniexport jint jnicall java_com_ndk_test_jniclient_addint
(Jnienv *, jclass, jint, jint );

# Ifdef _ cplusplus
}
# Endif
# Endif

(7) Create an android. mk file in the JNI directory. The content of the file is as follows (detailed syntax will be explained later ):
Local_path: = $ (call my-DIR)
Include $ (clear_vars)
Local_module: = testndk
Local_src_files: = com_ndk_test_jniclient.c
Include $ (build_shared_library)

(8) Copy com_ndk_test_jniclient.h to the jni directory of the testndk project, and create a new com_ndk_test_jniclient.c file to implement the functions in the header file, the content is as follows (originally java_com_ndk_test_jniclient_addstr was intended to complete the string addition function, but the data conversion is a bit problematic. I want to write this document first and then study the JNI data type, therefore, only one string is returned .) :
# Include "com_ndk_test_jniclient.h"
# Include <stdlib. h>
# Include <stdio. h>

# Ifdef _ cplusplus
Extern "C"
{
# Endif
/*
* Class: com_ndk_test_jniclient
* Method: addstr
* Signature: (ljava/lang/string;) ljava/lang/string;
*/
Jniexport jstring jnicall java_com_ndk_test_jniclient_addstr
(Jnienv * ENV, jclass Arg, jstring instringa, jstring instringb)
{
Jstring STR = (* env)-> newstringutf (ENV, "helloworld from JNI! ");
Return STR;
}

/*
* Class: com_ndk_test_jniclient
* Method: addint
* Signature: (ii) I
*/
Jniexport jint jnicall java_com_ndk_test_jniclient_addint
(Jnienv * ENV, jclass Arg, jint A, jint B)
{
Return A + B;
}

# Ifdef _ cplusplus
}
# Endif

Edit com_ndk_test_jniclient.c and save it. You can see that the libtestndk. So library is automatically generated under the OBJ/local/armeabi directory of the testnkd project.

(9) Call the function in jniclient. Java in testndkactivity. Java:
Package com. ndk. test;

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

Public class testndkactivity extends activity {
Static {
System. loadlibrary ("testndk ");
}

/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Setcontentview (R. layout. Main );

String STR = jniclient. addstr ("prefix", "suffix ");


Int isum = jniclient. addint (5, 2 );
String strsum = "5 + 7 =" + isum;

Textview TV1 = new textview (this );
Tv1.settext (STR );
Setcontentview (TV1 );
}
}
(10) run the testndk project. On the simulator, you can see that "helloworld from JNI!" In the com_ndk_test_jniclient.c file is output! ".

OK. This completes the ndk instance. In the future, you can learn about ndk/JNI in depth, such as data type conversion between C/C ++ and Java, and compiling format of Android. mk files.

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.