Search code examples
androidandroid-permissionsapk-expansion-filesandroid-native-library

Loading an .so native library from android's obb folder


I have an .so file that I packed into an obb expansion. It's about 20 megabytes.

I'm trying to use

System.load("/storage/emulated/0/Android/obb/com.mypackage.myapp/libtest.so");

to load the library. But I get the error:

dlopen("/storage/emulated/0/Android/obb/com.mypackage.myapp/libtest.so", RTLD_LAZY) failed: dlopen failed: couldn't map "/storage/emulated/0/Android/obb/com.mypackage.myapp/libtest.so" segment 2: Permission denied

type=1400 audit(0.0:8): avc: denied { execute } for path="/storage/emulated/0/Android/obb/com.mypackage.myapp/libtest.so" dev="fuse" ino=367663176224 scontext=u:r:untrusted_app:s0 tcontext=u:object_r:fuse:s0 tclass=file permissive=0

So I realized that we're not permitted to have an executable there.

So my question is,

1- Where should extract the file to be able to do this?

2- What function call should I make?

Thank you.


Solution

  • Copy the file to any location of your choice under your app's private internal storage folder, make sure it is marked read only and executable (not writable, especially by anyone else!). This is the only tree of locations typically writable by an application where executables are permitted.

    You can determine the private folder location by calling getFilesDir() on an initialized Activity or Service Context.

    There are a number of existing questions here which demonstrate mechanisms for file copying in Java.