Because the Libev library is written in C, the way to use this library in an Android project is to compile Libev into. So files and use JNI methods in Android to invoke Libev. so files.
As we all know, the NDK development of Android can compile c,c++ code, but it needs to write android.mk files by itself. However, for most open source projects, the amount of code is very large, and you need to use configure to configure the compilation options before compiling, in which case it is very unrealistic to write your own android.mk files.
Here, first refer to how to use the Android standalone tool chain to quickly migrate open source projects to implement the configuration of the Android cross-compilation tool chain. Because I need to run my application on the Android system of the x86 and ARM platforms, I have compiled two platforms of. so files, respectively, in Xxx/libev-4.19/android/x86/lib and xxx/libev-4.19/ Android/arm/lib below.
Next refer to the Android NDK for compiling local files and referencing third-party so files to create a new Prebuild folder under the JNI directory.
The Android NDK program provides a way to compile different platforms. So files, create a new application.mk file under the JNI directory, and add the following to the file.
#APP_ABI := all#APP_ABI := armeabi armeabi-v7a x86 APP_ABI := x86APP_PLATFORM := android-17
Where App_abi specifies the type of platform, App_platform specifies the version of Android. In the process of compiling native code, the Android NDK assigns different values to $ (target_arch) and executes the Android.mk file to generate the. so file for the corresponding platform. Like when
APP_ABI := armeabi armeabi-v7a x86
, the following folder is generated under the Lib directory:
When running on different platforms, the Android system will automatically load different. so files.
To make the Android system compile different platforms, load different libev.so files, I use the $ (target_arch) variable to differentiate. Create a new Android.mk file under the Prebuild file,
and copy the pre-compiled x86 and arm platform libev.so files to the Prebuild folder. It is important to note that The suffix name of the Libev library that was compiled on the Linux platform is libev.so.4.0.0, but Android can only recognize the. So suffix file, and here I change the libev.so.4.0.0 to libev.so, here is a hidden danger, which will be mentioned later.
The directory structure at this time is as follows:
The contents of android.mk in Prebuild are as follows:
include $(Clear_vars) Ifeq ($(Target_arch), x86)include $(Clear_vars)Local_module := Ev-x86Local_src_files :=$(Local_path)/prebuild/libev-x86.soinclude $(prebuilt_shared_library)Else include $(Clear_vars)Local_module := Ev-armLocal_src_files :=$(Local_path)/prebuild/libev-arm.soinclude $(prebuilt_shared_library) endif
This makes it possible to differentiate between different platforms.
The android.mk content below the JNI directory is as follows:
Local_path :=$(Call My-dir)include $(Clear_vars)Local_module := connLocal_src_files := Net_service.c Ifeq ($(Target_arch), x86)local_shared_libraries := Ev-x86Else local_shared_libraries := Ev-armendifLocal_ldlibs+= -L$(Sysroot)/usr/lib-llogLocal_c_includes :=$(Local_path)/includeLocal_cflags := -d_android_include $(build_shared_library)include $(Local_path)/prebuild/Android, m)
Note here to set Local_shared_libraries and include $ (local_path)/prebuild/android.mk so that my local code net_service.c can access the Libev library. In net_service.c, I called the function in Libev.
In this way, I build my libconn.so and run on the Android physical machine, and the following error occurs:
You can see that after loading libconn.so, the Libev.so.4 file is then loaded. But our file is libev-x86.so or libev-arm.so, why is it libev.so.4?
I've been looking for this problem for a long time, and I finally went to see the information about make libev-4.19 under Linux. The information is as follows:
From the information found in the Libev.so.4 this string, the callout part of the figure can guess the general meaning, is to compile the relevant files into a file named libev.so.4.0.0, but soname for the libev.so.4 of a so format file.
Here the key word is soname, the detailed information can refer to me to the dynamic link library Some understanding, basically is soname is an so file alias, Because we loaded the libev-x86.so file in the android.mk of the JNI main directory (following the example of the x86 platform), when the linker generated libconn.so, libev-x86.so in the Soname file was put libev.so.4 in Libconn In the. So dependency, when the system loads the libconn.so, the libev.so.4 file is loaded according to the dependent information. And our file name is libev-x86.so, so we can not find libev.so.4 error prompt.
Find the reason, we know the solution, the solution is to change the libev-86.so file soname to libev-x86.so, this simple.
The method is as follows: (take x86 as an example)
Perform configure configuration under the Libev-4.19 source folder
.--host=i686-linux-android--prefix=`pwd`/android/x86
Execute make
At this point, depending on the information, you can see the resulting file under. Libs, note that this is a hidden directory.
Enter the. Libs directory below, the directory structure is as follows:
The libev.so.4.0.0 here is through
Generated, so we manually enter this command in the. Libs directory to generate the so file soname for libev-x86.so, with the following command:
i686-linux -android -GCC -shared -fpic -dpic Libs/ev o Libs/event o -lm -o3 - Wl , -soname -wl , Libev-x86 So -o Libs/libev-x86 So
This generates a libev-x86.so file that soname to libev-x86.so.
The process of generating a libev-arm.so file is as above.
Copy the libev-x86.so libev-arm.so file to the Prebuild directory. The resulting APK file will work as expected.
Porting the Libev event library to Android