How to Use the compiled. So library in Android [go to http://billhoo.blog.51cto.com/2337751/1125559]

Source: Internet
Author: User
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:

  1. Jniexport jint jnicall
  2. 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:

  1. Package com. nativetools;
  2.  
  3. Public class nativedemo {
  4. Static {
  5. // Load the library to be used
  6. System. loadlibrary ("demomodule ");
  7. }
  8.  
  9. // Declare the local method in the library
  10. Public native int max (int A, int B );
  11. }

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:

  1. ...
  2. // Import the toolkit of the local method
  3. Import com. nativetools. nativedemo;
  4.  
  5. Public class mainactivity extends activity {
  6.  
  7. @ Override
  8. Protected void oncreate (bundle savedinstancestate ){
  9. Super. oncreate (savedinstancestate );
  10. Setcontentview (R. layout. activity_main );
  11. Nativedemo nativetool = new nativedemo ();
  12. // Call the local function max
  13. Integer I = nativetool. Max (1, 1024,512 );
  14. New alertdialog. Builder (this). setmessage (I. tostring (). Show ();
  15. }
  16. }
  17. ...

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.

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.