Android-JNI simple instance Parsing
Recently, the project has iterated several versions, which are relatively easy at present. Although the project is idle, people cannot be idle. Otherwise, the project will be discarded. A journey of a thousand miles begins with a full force, quantitative change leads to qualitative changes, learning without thinking about it ....... okay, okay ..... let's go straight to the topic. Let's just say, let's talk about it. We welcome everyone to exchange, learn, and make progress together. You are welcome to move bricks. Even if you throw me, you will not answer the question.
1. Create an Android project.
2. Create a JNICall. java class:
Create a local method. This method serves as the interface for calling the C/C ++ program. In addition, we load the. so file in static mode.
3. Add local support
Right-click a project and choose ----> Android Tools ----> Add Native Support,
The Add Android Native Support dialog box is displayed. enter the name of the so file to be loaded:
In this case, a jni folder contains the my_jni.cpp and Android. mk files,
The my_jni.cpp file contains only one line of code, and the jni library is introduced:
Let's take a look at the Android. mk file:
The automatically generated MakeFile file contains multiple attributes:
LOCAL_PATH is the directory where C/C ++ code is located, that is, our jni directory.
Include $ (CLEAR_VARS) indicates clearing the previous dependent class library
LOCAL_MODULE is the name of the library to be compiled. It is the name entered in the Add Android Native Support dialog box. The compiler automatically adds lib to the front and. so to the back.
LOCAL_SRC_FILES is the C/C ++ file to be compiled.
Include $ (BUILD_SHARED_LIBRARY) indicates that the dynamic library is compiled. include $ (BUILD_STATIC_LIBRARY) indicates that the static library is compiled.
4. Compile JNI. Below we need to generate the. h header file:
Find the bin folder in the Android project and find the classes folder. All the bytecode files automatically generated by eclipse are under this folder. First, use the cd command in the command line to switch to this directory.
Then use the javah command to generate the. h header file we want:
The name generated by this header file can be modified as long as it is valid. Let's take a look at the generated. h file:
5. Compile the cpp file to generate the local database:
Here we need to introduce the header file. The key is to look at the format of the Method Name:
Java_com_example_jnidemo_JNICall_myJni
Java: a fixed writing method. This is required.
Com_example_jnidemo: indicates the package name that contains the native method.
JNICall: indicates the class name that contains the native method.
MyJni: native method name
These methods must be matched. Otherwise, an error occurs: java. lang. UnsatisfiedLinkError: Native method not found.
6. generate. so file (this step is not necessary, because eclipse can automatically compile and generate a file with the project at runtime. so file, of course, we can also compile it independently. Next we will introduce the process of separate compilation)
Run the cd command to enter the directory where the project is located, and then run the ndk-build command to generate the. so file:
The so file is generated, refresh the project, and let's take a look at the directory changes:
7. Call the native method:
8. Run the project and make results
So far, a simple JNI program has been completed