Android JNI Introduction First article--hellojni

Source: Internet
Author: User

Android support using NDK to develop C programs, about configuring the NDK environment issues should not be mentioned again, this online there are many, here through an example to tell the simple JNI development, you can refer to this article (Get Your eclipse-integrated NDK on! Build the development environment where Eclipse compiles the C language for so files.

The native method implements the following steps:

1. Declare the native () method in Java and compile (JAVAC);

2. Create an. h file with Javah;

3. Write C files that contain. h files

4. Compiling c files

5. Use a successful so file.

The first step:

1. Declaration Native method

[Java] view Plaincopyprint?
  1. Public class Printf_jni {
  2. Static {
  3. System.loadlibrary ("Com_nedu_jni_helloword_printf-jni");
  4. }
  5. public native void Printhello ();
  6. }

2. Javac Compilation

Go to the path where the Java file is located, call the Javac command,

Step two: Use the Javah command to generate the. h header file,

This to go back to the SRC directory, do not know what the reason, if the above Javac path will be an error,

The header files generated using the Javah command are as follows:

[CPP]View Plaincopyprint?
  1. /* Don't EDIT this file-it are machine generated */
  2. #include <jni.h>
  3. /* Header for class COM_NEDU_JNI_HELLOWORD_PRINTF_JNI */
  4. #ifndef _included_com_nedu_jni_helloword_printf_jni
  5. #define _included_com_nedu_jni_helloword_printf_jni
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /*
  10. * Class:com_nedu_jni_helloword_printf_jni
  11. * Method:printhello
  12. * Signature: () V
  13. */
  14. Jniexport void Jnicall Java_com_nedu_jni_helloword_printf_1jni_printhello
  15. (JNIENV *, jobject);
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #endif

Step three: Write the C file with the following code:

[CPP]  view plaincopyprint? 
  1. #include <stdio.h>    
  2. #include  <stdlib.h>    
  3. #include   "com_nedu_jni_helloword_printf_jni.h" &NBSP;&NBSP;&NBSP;&NBSP;
  4. jniexport void jnicall java_com_nedu_jni_helloword_printf_1jni_ printhello  
  5.    (JNIENV&NBSP;*E,&NBSP;JOBJECT&NBSP;J)      
  6. {    
  7.     printf ( "hello world!");    
  8. }    

Fourth step, write Android.mk file, compile C file
The Android.mk file is as follows:

[plain] view plaincopyprint?
  1. Local_path: = $ (call My-dir)
  2. Include $ (clear_vars)
  3. Local_module: = Com_nedu_jni_helloword_printf-jni
  4. Local_src_files: =PRINTF_JNI.C
  5. Include $ (build_shared_library)


Local_module: = Com_nedu_jni_helloword_printf-jnilocal_module: = Com_nedu_jni_helloword_printf-jnilocal_module means so Filename

Local_src_files files that need to be compiled

Following the introduction of this article (Get Your eclipse-integrated NDK on!), you can compile it in Eclipse.

Fifth step: Using the so file:

Load the so file with the following code

[HTML] view Plaincopyprint?
  1. System.loadlibrary ("Com_nedu_jni_helloword_printf-jni");


Load the so file with the following code to load the so file with the following code

The call is as follows:

[Java]View Plaincopyprint?
    1. Printf_jni print=New Printf_jni ();
[Java]View Plaincopyprint?
    1. Print.printhello ();

/**
* @author Zhang Xingye
* Email: xy-zhang#163.com
* Android Development Advanced Group: 278401545
*
*/

Android JNI Introduction First article--hellojni

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.