Android Shared libraries and Static libraries

本文探讨了Android NDK环境下静态库与共享库的不同之处。共享库可在运行时按需加载,有助于减少内存占用,适用于多个库依赖的情况;而静态库则在编译时被嵌入到最终的库中,可以去除不必要的符号,适用于使用场景有限的模块。文章还讨论了选择静态库还是共享库的考量因素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

With Android NDK, it is possible to produce shared libraries (also called dynamic libraries,
like DLL on Windows) as well as static libraries:
Shared libraries are a piece of executable loaded on demand. These are stored on
disk and loaded to memory as a whole. Only shared libraries can be loaded directly
from Java code.
Static libraries are embedded in a shared library during compilation. Binary code
is copied into a final library, without regards to code duplication (if embedded by
several different modules).
In contrast with shared libraries, static libraries can be stripped, which means that
unnecessary symbols (like a function which is never called from the embedding library) are
removed from the final binary. They make shared libraries bigger but "all-inclusive", without

dependencies. This avoids the "DLL not found" syndrome well known on Window.



Shared vs. Static modules
Whether you should use a static or shared library depends on the context:
If a library is embedded in several other libraries
If almost all pieces of code are required to run
If a library needs to be selected dynamically at runtime
then consider turning it into a shared library because they avoid memory
duplication (which is a very sensible issue on mobile devices).
On the other hand:
If it is used in one or only a few places
If only part of its code is necessary to run
If loading it at the beginning of your application is not a concern
then consider turning it into a static library instead. It can be reduced in size at
compilation-time at the price of some possible duplication.
运行程序时出现 `error while loading shared libraries: libjvm.so: cannot open shared object file or directory` 错误,通常表明系统在运行时无法找到 `libjvm.so` 这个共享库文件。`libjvm.so` 是 Java 虚拟机(JVM)的一部分,通常位于 Java 安装目录下的 `lib` 子目录中。以下是详细的解决方案: ### 1. 检查 Java 安装 首先,确保系统中已正确安装 Java 运行环境(JRE)或 Java 开发工具包(JDK)。可以通过以下命令检查 Java 是否安装以及其版本信息: ```bash java -version ``` 如果未安装 Java,可以通过操作系统的包管理工具进行安装。例如,在基于 Debian 的系统上,使用以下命令安装 OpenJDK: ```bash sudo apt update sudo apt install default-jdk ``` ### 2. 确认 `libjvm.so` 文件的位置 安装完成后,需要确认 `libjvm.so` 文件是否存在于 Java 安装目录中。通常,该文件位于以下路径之一: - 对于 64 位系统:`/usr/lib/jvm/java-<version>-openjdk-amd64/jre/lib/amd64/server/` - 对于 32 位系统:`/usr/lib/jvm/java-<version>-openjdk-i386/jre/lib/i386/server/` 可以使用以下命令查找文件: ```bash find /usr/lib/jvm -name libjvm.so ``` ### 3. 配置动态链接器的搜索路径 如果 `libjvm.so` 文件存在但系统仍然无法找到它,可能需要将该文件所在的目录添加到动态链接器的搜索路径中。可以通过以下步骤完成此操作: #### 3.1 创建新的配置文件 在 `/etc/ld.so.conf.d/` 目录下创建一个新的配置文件,例如 `java.conf`,并将其内容设置为 `libjvm.so` 所在的目录路径: ```bash sudo nano /etc/ld.so.conf.d/java.conf ``` 在文件中添加以下内容(请根据实际情况修改路径): ``` /usr/lib/jvm/java-<version>-openjdk-amd64/jre/lib/amd64/server/ ``` #### 3.2 更新动态链接器缓存 保存并关闭文件后,运行以下命令更新动态链接器缓存: ```bash sudo ldconfig ``` ### 4. 设置 `LD_LIBRARY_PATH` 环境变量 如果无法修改系统范围的配置,可以临时设置 `LD_LIBRARY_PATH` 环境变量,将 `libjvm.so` 所在的目录添加到其中。例如,在 Bash shell 中,可以使用以下命令: ```bash export LD_LIBRARY_PATH=/usr/lib/jvm/java-<version>-openjdk-amd64/jre/lib/amd64/server/:$LD_LIBRARY_PATH ``` 为了使该设置永久生效,可以将上述命令添加到用户的 shell 配置文件中,例如 `~/.bashrc` 或 `~/.bash_profile`。 ### 5. 验证解决方案 完成上述步骤后,重新运行程序以验证是否解决了 `libjvm.so` 的加载问题。如果问题仍然存在,可以尝试重启系统以确保所有更改生效。 ### 示例代码 以下是一个简单的 Java 程序示例,用于验证 Java 环境是否正常工作: ```java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } } ``` 编译并运行该程序: ```bash javac HelloWorld.java java HelloWorld ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值