Introduction to JNI Technology under Cygwin+ndk+eclipse

Source: Internet
Author: User

1, the preparation of JNI steps
1) Write the Java class with the native declaration.
2) Compiling Java files into class
3) Compile the. h file with Javah-jni Com.ikags.project.XXXX
4) Write code in C + +
5) write the makefile file and compile the. h and. C (. cpp) files into a. dll (. So) file
6) Provide the. dll (. So) file to the project, called by the System.loadlibrary method.

2. Writing makefile Files
When make executes, it automatically looks for the makefile (makefile) file and then performs the compilation work.
In a makefile, it usually contains the following content:
1) The target object, usually the target file or executable file, that needs to be created by the Make tool.
2) the file (Dependency_file) on which the target body is to be created.
3) command to run when creating each target body.

3, the android.mk wording
(1) The android.mk file first needs to specify the Local_path variable, which is used to locate the source file. As a general rule
The android.mk and the source files that need to be compiled are in the same directory, so they are defined as follows:
local_path:=$ (call My-dir)
The above statement means that the Local_path variable defines the directory path where the cost file is located.
(2) Multiple compilation modules can be defined in Android.mk, with each compiled module starting with the include $ (clear_vars)
End with include $ (build_xxx).
Include $ (clear_vars)
Clear_vars is provided by the build system, specifying that the GNU makefile for you to clear all local_xxx variables except Local_path,
such as local_module,local_src_files,local_shared_libraries,local_static_libraries and so on.
Include $ (build_static_library) to compile into a static library
The include $ (build_shared_library) represents the compilation into a dynamic library.
Include $ (build_executable) to compile into executable program

4, JNI invocation example
encoding premise, cygwin+ndk+eclipse build the development environment, the ability to properly compile and run
First JNI test program or print Hello,world
4.1 Write the Java,native method, and call the Lib name
package JNI;
public class Hello
{
    public native void SayHello ();
    static
    {
      System.out.println (System.getproperty ("Java.library.path"));
        system.loadlibrary ("Hello");
    }
    public static void Main (string[] args)
    {
      
        hello h = new Hello ();
        h.sayhello ();
    }
}
4.2, compile to. h file with Javah-jni All-inclusive name + class name
after compiling, the class file is in the bin directory of the project.
Enter the DOS state, CD to the project's Bin directory, and run the command as follows:
 javah-jni JNI. Hello
Under normal circumstances, Jni_hello.h will appear in the Bin directory

4.3. Create. So files
1) New hello.c file
Then we set up a C + + project, create a JNI directory under the SRC directory,
Copy the newly created jni_hello.h file to the JNI directory.
And in the JNI directory to create a hello.c file to implement the method declared in the header file, the code is as follows:
#include <jni.h>
#include "jni_hello.h"
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif


Jniexport void Jnicall Java_jni_hello_sayhello (jnienv *env, Jobject obj) {
printf ("Hello World!\n");
Return
}

2) New Android.mk file
Create a new Android.mk file under the JNI directory (that is, the HELLO-JNI.C sibling directory)
Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: = Hello
#LOCAL_MODULE, compile the target object to identify each module that you describe in the Android.mk file
Local_src_files: = hello.c
#LOCAL_SRC_FILES变量必须包含将要编译打包进模块中的C或C + + source code files
Include $ (build_shared_library)
#BUILD_SHARED_LIBRARY表示编译生成共享库

Run Cygwin, to the C + + project directory, as follows:
CD Cygdrive/c/workspace/hello/src/jni
Run the $ndk/ndk-build as follows:
$ ndk-build
Cygwin:generating Dependency File Converter script
Compile Thumb:hello <= hello.c
SharedLibrary:libhello.so
Install:libhello.so = libs/armeabi/libhello.so
If the prompt message is shown above, the. So shared library file was successfully generated

So files are copied to the path that the Java load Library Ld_library_path points to.

5, Run Java project

Source: Htpp://blog.sina.com.cn/staratsky

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.