在 Android apk 中打包 linux 二进制文件 [英] Packaging linux binary in Android apk
问题描述
如果我想在我的 .apk 文件中包含一个 linux 二进制文件(如 ffmpeg)(仅适用于基于 ARM 的设备),这是否可以分发给非 root 手机?我知道可以使用 Runtime.exec() 调用包含的二进制文件,但这是推荐的做法吗?
If I want to include a linux binary (like ffmpeg) in my .apk file (only for ARM based devices), is this possible to distribute for non rooted phones? I know it's possible to call an included binary with Runtime.exec() but is it a recommended practice?
推荐答案
您可以将二进制文件存储在 res/raw
或 assets
文件夹中.这样,它将部署在设备上.你应该在安装后提取"它,你的 /data/data/your.package/files/
是一个有效的目的地;/sdcard/
不是.您的应用程序无法访问通常的 /data/local/
目标,即使它适用于 adb shell.chmod 500
也可以.
You can store the binary in res/raw
or in assets
folder. This way it will be deployed on the device. You should "extract" it after the installation, your /data/data/your.package/files/
is a valid destination; /sdcard/
is not. The usual /data/local/
destination is not accessible for your app, even if it is for an adb shell. chmod 500
is also fine.
也就是说,带有 JNI 包装器的 libffmpeg.so 在 Android 应用中更易于使用,并且占用的系统资源更少.
This said, libffmpeg.so with JNI wrappers is easier to use in an Android app, and takes less system resources.
更新,2014 年 4 月您可以欺骗系统,并将您的二进制文件存储在 libs/armeabi
文件夹中,让安装程序为您提取此二进制文件.它将最终在 /data/data/your.package/lib/
文件夹中,具有 +x
权限.这个 hack 还有一个很好的优势,它让系统选择正确的 ABI 来提取二进制文件:armeabi、armeabi-v7a、mips、或 x86.
Update, Aprl 2014 You can trick the system, and store your binary in libs/armeabi
folder, and let the installer extract this binary for you. It will end up in /data/data/your.package/lib/
folder, with +x
permissions. This hack has another nice advantage, it lets the system choose the correct ABI to extract the binary for: armeabi, armeabi-v7a, mips, or x86.
诀窍是遵循共享库命名约定.因此,当您将 ffmpeg 可执行文件放入 libs/armeabi
时,请将文件重命名为 lib_ffmpeg_.so.当然,您还将Runtime.exec()
使用完整路径和新添加的前缀和后缀的文件.
The trick is to follow the shared library naming convention. So, when you put ffmpeg executable into libs/armeabi
, rename the file to lib_ffmpeg_.so. Naturally, you will also Runtime.exec()
the file using the full path, and the newly added prefix and suffix.
这篇关于在 Android apk 中打包 linux 二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!