简体   繁体   English

如何在android studio中添加预构建的* .so库?

[英]How to add prebuilt *.so libraries in android studio?

I am trying to integrate the Hyperledger indy SDK . 我正在尝试集成Hyperledger indy SDK However, when running my code I get the error 但是,在运行我的代码时,我收到错误

E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.UnsatisfiedLinkError: dlopen failed: library "libgnustl_shared.so" not found
 at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
 at java.lang.System.loadLibrary(System.java:1657)

I am trying to follow the documentation provided in the project repo. 我正在尝试按照项目回购中提供的文档 I tried using the sample project on this blog . 我尝试在此博客上使用示例项目。

  • I was able to build the *.so libraries under a linux virtual machine, the copied the built files in my android studio project on windows. 我能够在linux虚拟机下构建* .so库,在windows上的android studio项目中复制了构建的文件。
  • I added the files inside my project's jniLibs forlder for each architecture. 我为每个架构添加了项目的jniLibs forlder中的文件。
  • Added the code to load the library inside my mainActivity 添加了在mainActivity中加载库的代码
static{
        System.loadLibrary("indy");
    }
  • Tried creating a CMake file 尝试创建一个CMake文件
cmake_minimum_required(VERSION 3.4.1)
add_library(indy SHARED IMPORTED)
include_directories(src/main/jniLibs/${ANDROID_ABI}/include)

My gradle file includes: 我的gradle文件包括:

android{
 defaultconfig{
 ...
    ndk{
            moduleName "indy"
            abiFilters 'armeabi-v7a'
        }
 }
...
 sourceSets {
        main {
            jniLibs.srcDir  'src/main/jniLibs'
        }
    }
    externalNativeBuild {
        cmake {
            path file('../CMakeLists.txt')
        }
    }
}

Still, keep on getting the same error when I launch the app. 但是,当我启动应用程序时,继续得到相同的错误。 I am aware that the bash script that builds the libraries on linux uses the android-ndk-r16b-linux-x86_64 tools so I tried downgrading my ndk in android studio to use the same version but had no luck. 我知道在linux上构建库的bash脚本使用android-ndk-r16b-linux-x86_64工具,所以我尝试在android studio中降级我的ndk以使用相同版本但没有运气。

The output of the build script is 构建脚本的输出是

include/
  indy_anoncreds.h
  indy_core.h
  ...
lib/
  libindy.a
  libindy.so
  libindy_shared.so

How can I use this libraries in my android studio project? 如何在我的android studio项目中使用这个库?

The issue is mainly related to the nature of libraries. 这个问题主要与图书馆的性质有关。 Libraries are dynamic in Android and needs to be linked at runtime. 库在Android中是动态的,需要在运行时链接。

libindy.so depends on stl, openssl, libsodium and libzmq. libindy.so依赖于stl,openssl,libsodium和libzmq。 You will find libgnustl_shared.so in NDK. 您将在NDK中找到libgnustl_shared.so All the other needed prebuilt libraries are also available here . 所有其他所需的预建库也可在此处获得

What you need to do is make sure these libraries are present in the jniLibs folder and load these in order libraries before libindy. 您需要做的是确保这些库存在于jniLibs文件夹中,并在libindy之前将它们加载到顺序库中。

System.loadLibrary("libgnustl_shared");
.
.
System.loadLibrary("indy");

Alternate approach: 替代方法:

There is a subproject in Indy where we are using libindy as dependency and we try to create a one fat dynamic library which has all the dependencies. Indy中有一个子项目,我们使用libindy作为依赖项,我们尝试创建一个具有所有依赖项的胖动态库。 Link 链接

If you follow the steps like vcx you dont have to have all the defendant l libraries in jniLibs as they will be already part of final .so file 如果您按照vcx这样的步骤操作,则不必在jniLibs中拥有所有被告l库,因为它们已经是最终.so文件的一部分

The command which make one fat dynamic library with all the symbols and dependencies is this (from the link pasted above) 使用一个包含所有符号和依赖关系的胖动态库的命令就是这个(来自上面粘贴的链接)

${LIBVCX}/target/${CROSS_COMPILE}/release/libvcx.a \
${TOOLCHAIN_DIR}/sysroot/usr/${NDK_LIB_DIR}/libz.so \
${TOOLCHAIN_DIR}/sysroot/usr/${NDK_LIB_DIR}/libm.a \
${TOOLCHAIN_DIR}/sysroot/usr/${NDK_LIB_DIR}/liblog.so \
${LIBINDY_DIR}/libindy.a \
${TOOLCHAIN_DIR}/${CROSS_COMPILE_DIR}/${NDK_LIB_DIR}/libgnustl_shared.so \
${OPENSSL_DIR}/lib/libssl.a \
${OPENSSL_DIR}/lib/libcrypto.a \
${SODIUM_LIB_DIR}/libsodium.a \
${LIBZMQ_LIB_DIR}/libzmq.a \
${TOOLCHAIN_DIR}/${CROSS_COMPILE_DIR}/${NDK_LIB_DIR}/libgnustl_shared.so -Wl,--no-whole-archive -z muldefs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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