How to package native commandline application in apk?

See Android NDK build with ANT script.

You can add the binary to APK using aapt if Ant does not copy it automatically from libs/armeabi, see Documentation for aapt element in Ant script.

I believe that the file will be correctly extracted to /data/data/your.package.full.name/lib, with executable permissions.

Ant build involves a step called “ApkBuilder“, which only adds files from libs/armeabi that match pattern ^.+\.so$ or gdbserver – the latter for debug build only.

But it is not enough to rename the executable to “myexecutable.so”: this file will not be extracted by the APK installer on the device.

It is enough to rename the executable to libmyexecutable.so: this file will be extracted by the APK installer Package Manager on the device to /data/data/your.package.full.name/lib, with executable permissions.

I personally like to give this not-actually-a-library some special name, e.g. lib...ffmpeg...so.

UPDATE 6 years and 12 API levels later, this approach is still valid. But with Android App Bundle, the default behavior of the package manager is not to extract the native libraries from the APK, using them in-place. Here and in comments to this answer, you will find some workarounds for that. The one that is especially attractive, is to refer to the executable as "/path/to/MyApp.apk!lib...ffmpeg...so", but I have not tested this notation does not work with System.exec(). See IssueTracker for discussion.

Note that now there is a special exception for wrap.sh.

Leave a Comment