Search code examples
androidandroid-ndkffmpegandroid-build

Packaging linux binary in Android apk


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?


Solution

  • 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.

    This said, libffmpeg.so with JNI wrappers is easier to use in an Android app, and takes less system resources.


    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.

    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.