Source: http://billhoo.blog.51cto.com/2337751/1125559
In the previous article [compiling and debugging Android native code under win7] of Bill, a dynamic library "libdemomodule" has been compiled and generated. so (no time to read the previous article, Bill uploaded "libdemomodule. as described in this article ). Since it is a library, it is used for use, and compared with the compilation, it is much easier to use. The following Bill briefly introduces how to use the JNI interface "java_com_nativetools_nativedemo_max" in another android application ".
First, we should create a step-by-step Android sample project "usesharedlibdemo", version android-api9
Run it again to make sure that our demo works properly.
Double-click the project tree, create the "armeabi" folder under the "/Libs" directory, and copy the prepared library "libdemomodule. so "copy to this directory for use.
For the use of this library, we do not need to know how the "Max" function is implemented. We only need to know its JNI interface. For the library "libdemomodule. the current interface of the "Max" function in so "is as follows:
- Jniexport jint jnicall
- Java_com_nativetools_nativedemo_max (jnienv * ENV, jobject jthis, jint A, jint B );
According to the description of the above interface, to use this interface in this project, we need to create a new package "com. nativetools" and create the class "nativedemo" under this package, as shown below:
Then, load the library "libdemomodule. So" in the class "nativedemo", and declare the local method "Max" as follows:
- Package com. nativetools;
-
- Public class nativedemo {
- Static {
- // Load the library to be used
- System. loadlibrary ("demomodule ");
- }
-
- // Declare the local method in the library
- Public native int max (int A, int B );
- }
Note that "system. in loadlibrary ("demomodule"), "demomodule" is replaced by the prefix "lib" and suffix ". so, and is case sensitive. Otherwise, errors such as the implementation of the function cannot be found and the initialization failure of the database to be loaded will occur. For example, we can change the upper case "D" to the lower case "D ", the following error is reported, which also provides a reference for correcting the error:
Then, the task is over. All we have to do is import our toolkit where we need to use the max function and call Max for related computing:
- ...
- // Import the toolkit of the local method
- Import com. nativetools. nativedemo;
-
- Public class mainactivity extends activity {
-
- @ Override
- Protected void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. activity_main );
- Nativedemo nativetool = new nativedemo ();
- // Call the local function max
- Integer I = nativetool. Max (1, 1024,512 );
- New alertdialog. Builder (this). setmessage (I. tostring (). Show ();
- }
- }
- ...
We can see that the "Max" function in the library has been successfully called. Note that the code for declaring native code on the android end must be organized strictly according to the JNI interface. In this example, the JNI interface is
This is why we need to add the "com. nativetools. nativedemo" class to the project.
Summary
Bill himself is new to using third-party libraries. He thinks this article is still a lot worse, but he cannot make it clear, so he just wants to use it. the so method is briefly described, and it is hoped that in the future study and work, we will be able to gain in-depth and specific knowledge and skills in this area.