1. Install ndk and ADT
Ndk is used to compile C ++ code, while ADT is used to compile Java code and generate an APK package.
Although ndk can be run through cywin in windows, the command line parameter is too long (when there are many files ). Therefore, we recommend that you install ndk on Linux.
ADT runs in windows and does not require ndk. Instead, you only need to copy the. So file compiled in Linux.
2. Compile libevent
Obtain the libevent source code and perform cross-compilation in Linux. The key is./configure, pass in the Cross-compilation tool provided by ndk through parameters, include and Lib paths
There will be some minor issues. For details, refer to this article cross compiling libevent for Android
You can do this.
Finally, make will show the compiled. So and. A files under. libs.
For more information about ndk cross-compilation, see this article building open source libraries with Android ndk.
3. compile the project code
Create a project directory under the samples directory of the ndk, such as myprj, and then create a sub-directory JNI, and merge the C ++ code into it.
Create the Android. mk file in the JNI directory. The content of the file is as follows:
Local_path: = $ (call my-DIR) include $ (clear_vars) # introduce the compiled file libevent. alocal_module: = eventlocal_src_files: = libevent/src /. libs/libevent. ainclude $ (prebuilt_static_library) include $ (clear_vars) # introduce the compiled file libevent_pthreads.alocal_module: = event_pthreadslocal_src_files: = libevent/src /. libs/libevent_pthreads.ainclude $ (prebuilt_static_library) include $ (clear_vars) # compile the CPP source code local_module: = libxyzlocal_c_des des: = libevent/src/includelocal_c_includes + = libxyz/# reference all CPP files in the multilevel directory my_files :=$ (wildcard libxyz /*. CPP) my_files + = $ (wildcard libxyz /*/*. CPP) local_src_files + = $ (my_files) # reference zlib library and Android log library local_ldlibs + =-LZ-llog # reference the previously defined static libevent library local_static_libraries: = eventlocal_static_libraries + = event_pthreads # compile and generate a dynamic library include $ (build_shared_library)
It should be noted that here we use the libevent static library instead of the dynamic library. This is because the Android platform does not support versioned soname, that is, file names such as libevent. so.5.0. Even as described in this article, after the soname is modified, the reloc failure still occurs during libevent_pthreads.so loading. Why, through objdump
-X: It seems that the libevent_pthreads.so table does not reference libevent. So. Some symbols cannot be found. The solution is to reference the static library.
Log output. You can use the _ android_log_print () function instead of printf to view the output log on the logcat console of ADT.
C ++ is bound to Java and JNI is used. Refer to the hello_jni example to create the libxyz_jni.cpp file and generate a binding function for each export function. For example
extern "C"{void Java_com_company_myprj_libxyz_foo(JNIEnv* env, jobject thiz){ libxyz_foo();}}
Note that the final function name Foo should not contain "_"; otherwise, binding with the Java end will fail.
Run ndk-build in the JNI directory to view the generated libxyz. So file in the libs/armeabi directory of the upper-level.
You can see all the export functions through objdump-T libxyz. So.
4. Java Engineering
Create a common android project and put libxyz. So compiled earlier into the libs/armeabi directory.
Create the libxyz. Java file to reference libxyz. So, as shown below.
package com.company.myprj;public class libxyz {public native void foo();static{System.loadLibrary("xyz");}}
Note that the names of package, class, and method exactly correspond to the exported function names of. So.
Finally, you can call the above Code at a proper position in the android project.
import com.company.myprj.libxyz;new libxyz().foo();
In addition, you must add Internet access permissions to the application in the manifest file.
<uses-permission android:name="android.permission.INTERNET"/>
If you are running in the simulator, you need
adb forward tcp:1234 tcp:8080
Convert the port 8080 in the simulator to the external port 1234 so that you can access it on the PC.