Ndk demo program

Source: Internet
Author: User
ArticleDirectory
    • Simple ndk demo program
Simple ndk demo Program

Now, the environment has been set up. Now we start to build a simple ndk program to experience the entire ndk Development Process:

Step 1: first, create a new project in eclipse. The project name is ndkpassnormaldata. Create a new Java class in the project: callnativedemo. Java

The content is as follows:

PackageCom. jiubang. Demo. ndk. passnormaldata;

PublicClassCallnativedemo {

 Static{

System.Loadlibrary("Ndkpassdatademo ");

// Pay attention to the library file name. For ndkpassdatademo, the system will load the // libndkpassdatademo. So library during initialization. Do not add the lib and. So extensions.

}

PublicNativeIntSum (IntA,IntB); // Add native to indicate that the method is implemented by. So.

PublicNativeString strcat (string str1, string str2 );

}

Compile the Java file first, and then use javah to generate the C header file we need in ndk:

Enter the bin directory of the project and enter:

Javah-JNI com. jiubang. Demo. ndk. passnormaldata. callnativedemo

The com_jiubang_demo_ndk_passnormaldata_callnativedemo.h file is generated in the current directory.

The content is roughly as follows:

/* Do not edit this file-it is machine generated */

# Include <JNI. h>

/* Header for class com_jiubang_demo_ndk_passnormaldata_callnativedemo */

# Ifndef _ included_com_jiubang_demo_ndk_passnormaldata_callnativedemo

# DEFINE _ included_com_jiubang_demo_ndk_passnormaldata_callnativedemo

# Ifdef _ cplusplus

Extern "C "{

# Endif

/*

* Class: com_jiubang_demo_ndk_passnormaldata_callnativedemo

* Method: Sum

* Signature: (ii) I

*/

Jniexport jint jnicall java_com_jiubang_demo_ndk_passnormaldata_callnativedemo_sum

(Jnienv *, jobject, jint, jint );

/*

* Class: com_jiubang_demo_ndk_passnormaldata_callnativedemo

* Method: strcat

* Signature: (ljava/lang/string;) ljava/lang/string;

*/

Jniexport jstring jnicall java_com_jiubang_demo_ndk_passnormaldata_callnativedemo_strcat

(Jnienv *, jobject, jstring, jstring );

# Ifdef _ cplusplus

}

# Endif

# Endif

Step 2: Add a folder named JNI to our new project. This folder is where the source code of the original ecology C/C ++ is stored. When we make, cygwin compiles this folder.

Step 3: Create an android. mk file in the JNI folder. Note that the suffix is. mk and the folder must be in lower case.

The content of Android. mk is:

Local_path: = $ (call my-DIR) <--------- returns the current path. It is the default and does not need to be changed.

Include $ (clear_vars) <-------- clear the previously defined environment variables. The default value does not need to be changed.

Local_module: = ndkpassdatademo <-------- name used for Java class reference

Local_cpp_extension: = CPP

Local_src_files: = fun. cpp <------ source file to be compiled

Local_ldlibs: =-L $ (sysroot)/usr/lib-llog <-- the library to be used for logging. It is not clear why the library name is like this for the time being...

Include $ (build_shared_library) <-- this refers to the dynamic library, which is the default and does not need to be changed.

Step 4: Create a fun. cpp file in the JNI folder. This file is a C/C ++ file.

Fun. cpp content:

# Include <stdio. h>

# Include <stdlib. h>

# Include <string. h>

# Include <Android/log. h>

# Include "com_jiubang_demo_ndk_passnormaldata_callnativedemo.h"

// CorrespondingPublicNativeIntSum (IntA,IntB) Implementation

Jint jnicall java_com_jiubang_demo_ndk_passnormaldata_callnativedemo_sum (jnienv * aenv, jobject athis, jint aparama, jint aparamb)

{

Int sum = 0;

_ Android_log_print (android_log_info, "ndk_passnormaldata", "callnativedemo_sum coming ");

Sum = aparama + aparamb;

File * file = fopen ("/sdcard/hello.txt", "W + ");

If (file! = NULL)

{

Fputs ("Hello world! /N ", file );

Fflush (File );

Fclose (File );

}

_ Android_log_print (android_log_info, "ndk_passnormaldata", "callnativedemo_sum leaving ");

Return sum;

}

// CorrespondingPublicNativeString strcat (string str1, string str2) Implementation

Jstring jnicall java_com_jiubang_demo_ndk_passnormaldata_callnativedemo_strcat (jnienv * aenv, jobject athis, jstring astr1, jstring astr2)

{

_ Android_log_print (android_log_info, "ndk_passnormaldata", "callnativedemo_strcat coming ");

Const char * str1 = aenv-> getstringutfchars (astr1, 0 );

Const char * str2 = aenv-> getstringutfchars (astr2, 0 );

// Char * str1 = (char *) (* aenv)-> getstringutfchars (aenv, astr1, 0 );

// Char * str2 = (char *) (* aenv)-> getstringutfchars (aenv, astr2, 0 );

Int strw.len = strlen (str1 );

Int str2_len = strlen (str2 );

// Char * pszret = malloc (strw.len + str2_len + 1 );

Char * pszret = new char [strw.len + str2_len + 1];

Strcpy (pszret, str1 );

Strcat (pszret, str2 );

// Jstring jstrret = (* aenv)-> newstringutf (aenv, pszret );

Jstring jstrret = aenv-> newstringutf (pszret );

// Free (pszret );

Delete pszret;

_ Android_log_print (android_log_info, "ndk_passnormaldata", "callnativedemo_strcat leaving ");

Return jstrret;

}

Then you can start compiling:

Open cygwin, enter the JNI directory of the project, and then execute ndk-build

You can see install.

Then we will find that there will be an additional lib and OBJ directory under the project directory.

Finally, add the local method of calling these ndk In the android ProjectCode:

Callnativedemo nativedemo = new callnativedemo ();

Int value = nativedemo. sum (10, 20 );

String STR = nativedemo. strcat ("who", "are you ");

Textview v = (textview) findviewbyid (R. Id. textview1 );

V. settext (STR );

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.