java - JNI怎么链接已经写好的C++编写的动态库文件.so
ringa_lee
ringa_lee 2017-04-18 10:28:03
0
1
366

JNI怎么链接已经写好的C++编写的动态库文件.so

ringa_lee
ringa_lee

ringa_lee

reply all(1)
洪涛

Take CMake as an example:

Since the precompiled library is already built, you need to use the IMPORTED flag to tell CMake that you only need to import it into your project:

add_library( imported-lib
             SHARED
             IMPORTED )

Then you need to use the set_target_properties() command to specify the path to the library, like the code below.

set_target_properties( # Specifies the target library.
                       imported-lib
                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION
                       # Provides the path to the library you want to import.
                       imported-lib/src/${ANDROID_ABI}/libimported-lib.so )

In order for CMake to find your header file during compilation, you need to use the include_directories() command and pass your header file address in:

include_directories( imported-lib/include/ )

Use the target_link_libraries() command in the CMake build script to associate the prebuilt library with your local library:

target_link_libraries( native-lib imported-lib app-glue ${log-lib} )

When you build your APP, Gradle will automatically package the imported libraries into your APK. You can use APK Analyzer to check.

But if you can get the source code, I don’t recommend linking to .so because it may be incompatible with specific platforms. For example, the .so dynamic library compiled on the Linux platform cannot run on the arm platform (such as Android).

Reference:

  • Have fun using C/C++ in Android Studio 2.2

  • Relocations in generic ELF (EM: 62) Wrong solution

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template