[Android Application Development]-(14) JNI-classic instance analysis

Source: Internet
Author: User

In the next few articles, I will learn about JNI development. I will mainly learn about JNI details and implement the examples through the Android platform. First, a classic instance will bring you-HelloWorld !! HelloWorld is classic. Once you think of learning programming, you think of HelloWorld that we wrote together in years. HelloWorld is used to explain the JNI development process. This process can be described as follows:


1) create a HelloWorld project;

2) create a class Helloworld. java with native method;


[Java]
Package org. winplus. helloworld;
Public class HelloWorld {
Public native void print ();
Private void stitic (){
System. loadLibrary ("helloworld ");
}
}

Package org. winplus. helloworld;
Public class HelloWorld {
Public native void print ();
Private void stitic (){
System. loadLibrary ("helloworld ");
}
}
The HelloWorld class first declares a publicnativevoid print () method. Static contains the local database. In java code, local methods declared as native only exist as declarations. Before calling a local method, you must first load the local library containing the method, as shown in HelloWorld, and put it in static. When JavaVM initializes a class, it will first execute this code, this ensures that the local library is loaded before the local method is called. For details about the reprinting mechanism, refer to the following article...

3) Use javac to compile Helloworld. java

If you are using Eclipse, you do not need to perform this operation. Enter the Class directory.

4) use javah-jni to compile Helloworld. class to generate the header file Helloworld. h; Javah-jni org. winplus. helloworld. HelloWorld will generate a header file named org_winplus_helloworld _ HelloWorld. h. The generated code is as follows:

[Cpp]
<SPAN style = "FONT-SIZE: 16px">/* do not edit this file-it is machine generated */
# Include <jni. h>
/* Header for class org_winplus_helloworld_HelloWorld */
 
# Ifndef _ Included_org_winplus_helloworld_HelloWorld
# Define _ Included_org_winplus_helloworld_HelloWorld
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: org_winplus_helloworld_HelloWorld
* Method: print
* Signature: () V
*/
JNIEXPORT void JNICALL Java_org_winplus_helloworld_HelloWorld_print
(JNIEnv *, jobject );
 
# Ifdef _ cplusplus
}
# Endif
# Endif </SPAN>

/* Do not edit this file-it is machine generated */
# Include <jni. h>
/* Header for class org_winplus_helloworld_HelloWorld */

# Ifndef _ Included_org_winplus_helloworld_HelloWorld
# Define _ Included_org_winplus_helloworld_HelloWorld
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: org_winplus_helloworld_HelloWorld
* Method: print
* Signature: () V
*/
JNIEXPORT void JNICALL Java_org_winplus_helloworld_HelloWorld_print
(JNIEnv *, jobject );

# Ifdef _ cplusplus
}
# Endif
# Endif
We noticed JNIEXPORTvoid JNICALL except (JNIEnv *, jobject); here we noticed two macros: JNIEXPORT and JNICALL. We can see that this function declaration receives two parameters and the corresponding Java code has no parameters. The first parameter is a pointer to the JNIEnv structure, and the second parameter is the HelloWorld object itself, that is, the this pointer.
5) use C to implement functions in the header file


[Cpp]
<SPAN style = "FONT-SIZE: 16px"> # include <jni. h>
# Include <stdio. h>
# Include "android/log. h"
# Include "HelloWorld. h"
Static const char * TAG = "HelloWorld ";
# Define LOGI (fmt, args...) _ android_log_print (ANDROID_LOG_INFO, TAG, fmt, # args)
JNIEXPORT void JNICALL Java_org_winplus_helloworld_HelloWorld_print
(JNIEnv * env, jobject obj ){
LOGI ("Hello World ");
Return;
}
</SPAN>

# Include <jni. h>
# Include <stdio. h>
# Include "android/log. h"
# Include "HelloWorld. h"
Static const char * TAG = "HelloWorld ";
# Define LOGI (fmt, args...) _ android_log_print (ANDROID_LOG_INFO, TAG, fmt, # args)
JNIEXPORT void JNICALL Java_org_winplus_helloworld_HelloWorld_print
(JNIEnv * env, jobject obj ){
LOGI ("Hello World ");
Return;
}

The log. h header file and ANDROID_LOG_INFO macro in jni are used for viewing printed information.

6) Compile the C file to generate a local library. This step is cumbersome. Please download the source code. Procedure

A) copy the header file and C file to the jni directory and write the Android. mk and Application. mk files.

B) generate the helloworld. so file www.2cto.com by using ndk or compiling In Ubuntu.

C) copy the generated file to the libs/armeabi directory and change the name to libhelloworld. so.

7) run the program and view the result.

 
Author: tangcheng_ OK
 

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.