Android development environment to build NDK (Series II)

Source: Internet
Author: User

First, about the NDK:
NDK Full Name: Native development Kit.
1. The NDK is a collection of tools.
The NDK provides a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK. These tools are great for developers.
The NDK integrates the cross compiler and provides the corresponding MK file isolation CPU, platform, ABI and other differences, developers simply need to modify the Mk file (stating "which files need to compile", "compilation characteristics Requirements", etc.), you can create a so.
The NDK can automatically package so with Java applications, greatly reducing the developer's packaging effort.
2. The NDK provides a stable, functionally limited API header file declaration.
Google expressly declares that the API is stable and supports the currently published API in all subsequent releases. As seen from this version of the NDK, these APIs support a very limited number of features, including: C standard library (LIBC), Standard math Library (LIBM), compression library (LIBZ), log Library (Liblog).

Second, the implementation of the NDK instance:
For the development of the NDK under the Windows environment, if the NDK used was a prior version of R7, it was necessary to install Cygwin in order to use the NDK, so the builder needed to be configured for Eclipse was actually executing cygwin and then passing ndk-build as the parameter. At the beginning of NDKr7, Google's Windows version of the NDK provides a ndk-build.cmd script so that it can be compiled directly from the script without the need to use Cygwin. Just add a builders to the Eclipse Android project to let Eclipse automatically compile the ndk.

This article is about implementation examples under NDK-R7.
Here is the process of using NDK-R7 to configure auto-compiled builders under Windows (actually, for Linux, you only need to modify Ndk-build.cmd to Ndk-build. )。
(1) Download the installation ndk-r7 first.
: http://developer.android.com/sdk/ndk/index.html
After downloading, unzip can be used.
(2) Open Eclipse, create a new Android project (my name is TESTNDK), create a new JNI folder under the project directory TESTNDK, which is used to save the file code that the NDK needs to compile, and so on.
(3) Create and configure a builder:
(a) Project->properties->builders->new, create a new builder.
(b) In the "Choose Configuration Type" dialog box that pops up, select "Program" and click "OK":
(c) in the "Edit Configuration" dialog box that pops up, configure the tab "Main".
Enter a name for the new builders in "name" (I'm named Ndk_builder).
Enter the path to the Nkd-build.cmd in location.
(My is D:\AndroidDev\android-ndk-r7\ndk-build.cmd, according to the respective NDK path settings, you can also click "Browser File System ..." to select this path).
Enter ${WORKSPACE_LOC:/TESTNDK} in "Working Diretcoty" (You can also click "Browse Workspace" to select the Testndk directory).

(d) In the Edit Configuration dialog box, configure the tab "Refresh".
Tick "Refresh Resources upon completion",
Tick "The entire workspace",
Tick "recuresively include sub-folders".

(e) In the Edit Configuration dialog box, configure the tab "Build options".
Tick "After a" and "clean",
Tick "During Manual builds",
Tick "During Auto Builds",
Tick "Specify working set of relevant resources".

Click on "Specify Resources ..."
Tick the "JNI" directory of the TESTNDK project and click "Finish".
Click "OK" to complete the configuration.
OK, here, Eclipse will be able to automatically invoke the NDK to compile the code for C + + in the Jin directory.
(4) Create a new jniclient.class in the TESTNDK project (in order to call C + + code), the contents are 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 navigate to the directory where the Jniclient.class is located, Enter "Javac Jniclient.java" after carriage return, generate Jniclinet.class file (if the project was built with Eclipse, in Testndk\bin \classes\com\ndk\ The Jniclinet.class file is already in the test directory).
(6) Copy Jniclinet.class to the Testndk\bin\classes\com\ndk\test directory, Position the cmd command to the TESTNDK \bin\classes directory, enter "Javah com.ndk.test.JniClient", and the C + + header file is generated in the Testndk\bin\classes directory Com_ndk_ Test_jniclient.h. The contents of the Com_ndk_test_jniclient.h file are as follows:
/* Do not EDIT This file-it are machine generated */
#include <JN I.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;) 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 a new android.mk file under the JNI directory with the following contents (detailed syntax is 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 the com_ndk_test_jniclient.h to the JNI directory of the TESTNDK project, then create a new com_ndk_test_jniclient.c file to complete the implementation of the function in the header file, the contents are as follows (originally JAVA_COM_ NDK_TEST_JNICLIENT_ADDSTR is to complete the function of the string addition, but the data conversion is a bit of a problem, want to write this document, followed by a further study of the JNI data type problem, so simply return a string. ):
#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;) 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

After editing the com_ndk_test_jniclient.c and saving it, you can see that the Libtestndk.so library will be automatically generated under the Obj/local/armeabi directory under the TESTNKD project.

(9) In Testndkactivity.java, complete the invocation of the function in Jniclient.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, you can see in the simulator interface output from the com_ndk_test_jniclient.c file "HelloWorld from JNI!" “。

OK,NDK instance to this completion. Follow-up can be in-depth learning ndk/jni, such as the data type conversion, ANDROID.MK file writing format and so on.

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.