java - JNI怎么链接已经写好的C++编写的动态库文件.so
ringa_lee
ringa_lee 2017-04-18 10:28:03
[Java讨论组]

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

ringa_lee
ringa_lee

ringa_lee

全部回复(1)
PHP中文网

以 CMake 为例:

由于预编译库是已经构建好的,你想就要使用 IMPORTED 标志去告诉 CMake ,你只需要将其导入到你的项目中即可:

add_library( imported-lib
             SHARED
             IMPORTED )

然后你需要使用 set_target_properties() 命令去指定库的路径,就像下面的代码那样。

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 )

为了让 CMake 在编译时期能找到你的头文件,你需要使用 include_directories() 命令,并且将你的头文件地址传进去:

include_directories( imported-lib/include/ )

在 CMake 构建脚本中使用 target_link_libraries() 命令,将预构建库与你本地库相关联:

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

当你构建你的 APP 的时候,Gradle 会自动将导入的库打包到你的 APK 中。你可以使用 APK Analyzer 来检查。

但是如果能得到源码的话,我不推荐你链接.so,因为可能会与特定平台不兼容。如在Linux平台上编译的.so动态库在 arm 平台(如 Android)上无法运行。

参考:

  • 在 Android Studio 2.2 中愉快地使用 C/C++

  • Relocations in generic ELF (EM: 62) 错误的解决方案

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号