Current location - Health Preservation Learning Network - Slimming men and women - How does android ndk call the third-party so library file?
How does android ndk call the third-party so library file?
Question description: How does Android call the third-party SO library?

Known conditions: the SO library is an Android version connection library (*. So file) and provides a detailed interface description;

Understand the solution:

1. put the SO file directly under libs/armeabi, and then the system. Loadlibrary ("XXX") in the code; Then public native static int XXX _ XXX _ XXX (); Then you can directly call the xxx_xxx_xxx () method;

2. The second scheme is to create your own SO file, call the third-party SO in your own SO file, and then call your own SO in the program. This is complicated, you need to build a java class file and generate a. H file, write. The c source file generated before the h file is include, and the corresponding method is implemented. Finally, the ndk-build script in the android NDK development package is used to generate the corresponding. So * * share the library;

Solve:

1. Are the above two schemes feasible? What's the problem if it's not feasible?

2. What's the difference between these two schemes? Why do most people on the Internet use the second option?

3. There is only one *. So file, and provides a detailed interface description. Can it be used on Android?

First, it depends on whether this SO is a JNI standard SO, for example, whether it returns a type that JNI does not directly support. In other words, can this be directly called JNI? If the answer is no, you can only choose the second plan.

If the answer is yes, it depends on whether you want this SO library to be directly exposed to the JAVA layer. If the answer is no, you can only choose the second option, for example, you are also a library provider.

Generally, if you only have SO, it means that someone else provided it to you, and you can ask the other party to provide you with a matching JAVA call file.

1, it depends on whether this conforms to the specification of JNI call. It's up to you.

2. Because the second method is the most flexible, all kinds of situations can be realized.

3. yes.

The easiest way to see if you can call directly from JAVA is to see if the function name in SO is in Java_XXX_XXX_XXX format.

Yes, you can. You can write a matching JAVA file yourself, pay attention to the conversion rules between SO function names and JAVA function names, or ask the SO provider for it.

If not, choose the second scheme.

1, check whether the required documents are complete.

To use a third-party dynamic library, there must be at least two files, one is a dynamic library (. So), another file contains.

Header file (. H) declared by the dynamic library API

2. Encapsulate the original dynamic library

The original dynamic library file does not contain the information needed by jni interface, so we need to encapsulate it, so I

Our requirement is to encapsulate the API in libadd.so into a dynamic with jni interface.

3. Write the encapsulation function libaddjni.c of the library.

According to the com _ Android _ libjni _ libjavaheader.h file generated earlier, write libaddjni.c and use it.

Generate libaddjni.so

Integrate third-party software packages (. Can,. So) in Android.

Third-party software packages may be used in Android, including Java packages. Jars and local packages. So ... Jar packages can be integrated through the Eclipse development environment or compiled source code, depending on your working environment.

Assuming my own program is MyMaps, I need to use BaiduMaps library, including baidumapapi.jar and libbmapappingine _ v1_ 3 _1.so.

First, integrate third-party jar packages and. So the dynamic library in Eclipse.

Create directories libs and libs/armeabi under MyMaps project, put baidumapapi.jar under libs/ directory, and put libbmapappingine _ v1_ 3 _1.so under libs/armeabi/

To package the third-party jar package baidumapapi.jar into MyMaps in Eclipse:

1. Right-click the project and select Properties; ;

2.Java builds the path and selects the library; ;

3. Right-click "Add Library …" on the library page;

4. Select "User Library" and click "Next";

5. Click the User Library button;

6. In the pop-up interface, click "New …";

7. Enter "User Library Name" and click "OK" to confirm;

8. After returning, select the newly created user library and click "Add Jars" on the right;

9. Select baidumapapi.jar under MyMaps/libs/;

10. Return after confirmation.

In this way, the compiled jar package will be typed as MyMaps.apk, and libbmapappingine _ v1_ 3 _1.so will also be packaged as lib/armeabi/

During the program running, libbmapapigine _ v1_ 3 _1.so is placed in /data/data/

Second, integrate the third-party integration jar package and. So the dynamic library in the source code.

MyMaps in the Android source code is placed under packages/apps. Create directories libs and libs/armeabi under MyMaps, put baidumapapi.jar in libs/, and put libbmapapigine _ v1_ 3 _1.so in libs/armeabi.

2. 1 Modify the Android.mk file

Android.mk file is as follows:

[Normal] View a normal copy

LOCAL_PATH:= $ (calling my-dir)

Including USD (VARS settlement)

Local module label: = Optional

LOCAL _ STATIC _ JAVA _ LIBRARIES:= libbaidumapapi

LOCAL_SRC_FILES := $ (call all-subdir-java-files)

LOCAL_PACKAGE_NAME := My Map

Include $(BUILD_PACKAGE)

##################################################

Including USD (VARS settlement)

LOCAL _ pre build _ STATIC _ JAVA _ LIBRARIES:= libbaidumappi:libs/baidumappi . jar

LOCAL _ pre build _ LIBS:= libBMapApiEngine _ v 1 _ 3 _ 1:libs/armea bi/libBMapApiEngine _ v 1 _ 3 _ 1 . so

Local module label: = Optional

Include $(BUILD _ MULTI _ prebuild)

# Use the following include to make our test package.

Include $ (call-make files-under, $(LOCAL_PATH)).

1 integrated jar package

LOCAL_STATIC_JAVA_LIBRARIES takes the alias of jar library and can take any value;

Local _ prebuilt _ static _ Java _ libraries specifies the rules of prebuiltjar library, format: alias: jar file path. Note: Aliases must be the same as those in LOCAL_STATIC_JAVA_LIBRARIES and do not contain. Jar; ; The path of the jar file must be the real path to store the third-party jar package.

BUILD _ MULTI _ PREBUILT is used for compilation.

2 integration. So the dynamic library

LOCAL _ pre build _ LIBS specifies the rules of pre-built so, format: alias: so file path. Note: Alias can't usually be changed, especially when third-party jar packages are used. So the library does not contain. So; The path of the so file must be the real path to store the third-party so file.

BUILD _ MULTI _ PREBUILT is used for compilation and copying.

2.2 Add unaffected user modules

In the user_tags.mk file, add libbmapapigine _ v1_ 3 _1to GRANDFATHERED_USER_MODULES.

[Normal] View a normal copy

grand granted _ USER _ MODULES+= \

… \

libBMapApiEngine _ v 1 _ 3 _ 1

User_tags.mk can be under build/core or $(TARGET_DEVICE_DIR). It is recommended to modify $ (target device directory).

2.3 Compilation results

MyMaps.apk is compiled and generated in out/target/product/

Libbmapapigine _ v1_ 3 _1.soinout/target/product/