简体   繁体   中英

Android NDK - Build shared library from source code

I'm working on an Android app that uses some 3rd party native libraries. We need to add some functionality to these libraries. So I wrote a set of C++ classes with the code that I need and also cross-compile openssl, as we are using openssl crypto functions.

I made a demo app to test my code and everything is working fine.

Now I need to send this code to the people that are developing the 3rd party libraries, and they will add our code and wrap all the native code in a JAR for us to use it in our app.

I used this example NDK hello libs as a base. As I want to send my code as a shared library (.so file), I did this in the 'CMakeList.txt':

cmake_minimum_required(VERSION 3.4.1)

set(distribution_DIR ${CMAKE_SOURCE_DIR}/../distribution)

add_library(lib_crypto STATIC IMPORTED)
set_target_properties(lib_crypto PROPERTIES IMPORTED_LOCATION
    ${distribution_DIR}/openssl/lib/${ANDROID_ABI}/libcrypto.a)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

### This code just calls my my-lib ###
add_library(native-lib SHARED
        src/main/cpp/native-lib.cpp)

### This is the lib I want to build ###
add_library(my-lib SHARED
         src/main/cpp/*****.cpp
         # more files...
         )

target_include_directories(my-lib PRIVATE ${distribution_DIR}/openssl/include)

target_link_libraries(
                native-lib
                my-lib
                )

target_link_libraries(
                neclic
                lib_crypto
                )

I built the APK and took 'my-lib.so' from it. Then I made a new project using this lib (and including the .h headers) and everything is working as expected.

My questions is: Is there a better way of doing this? May I expect any problem when I send this library to be integrated with the original code? For example, do they need to use the same NDK version? Or 'my-lib.so' can be used in any android app if the arch is supported?

Thanks!

Generally speaking, you don't need to send your libneclic.so to be packed in a JAR. You can use it in your APK as is , unless the 3rd party libraries that your collegues develop outside, need your library as dependency.

Actually, the supported way to disribute compiled Java libraries with native components are not JAR, but rather AAR files. But this is a technicality that is not important for this discussion.

If you use your libneclic.so directly in your APK, that is, you have Java class(es) that uses JNI to call functions in your library, you can keep the same CMakeList.txt in your project, or you can put the the built library in JniLibs folder, and Android Studio will pick it from there when it packages the APK.

On the other hand, if the library you built has exported functions that will be called from some 3rd party library, make sure that both use same shared STL (luckily, with the latest NDK release 18, there is only one shared STL implementation available, c++_shared .

It is highly recommended that the libraries are built with same NDK version, even though, most likely, moderate difference between these versions will not cause negative results. Each NDK release brings significant improvements, not only in performance, but also fixes bugs, including some security ones.

Still, it is safer to agree on a common older release of NDK, than to keep track of all possible inconsistencies.

Note that by default, CMake uses c++_static STL to build your libneclic.so , and you must override that, by setting ANDROID_STL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM