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

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.

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.

Related

Change StorageManager's default mount point from /mnt/obb to /mnt/data

I am following this obb sample to load a native library from an obb file. But I am seeing following error on System.load(mylib.getAbsolutePath()) call.
java.lang.UnsatisfiedLinkError: dlopen failed: library "/mnt/obb/fcc93fcbaf5acfc/mylibs.so" needed or dlopened by
"/system/lib/libnativeloader.so" is not accessible for the namespace "classloader-namespace"
[name="classloader-namespace", ld_library_paths="", default_library_paths="", permitted_paths="/data:/mnt/expand:/data/data/com.example"]
But as far as I understood, StorageManager#mountObb() by default mounting to /mnt/obb. Is there a way I can instruct StorageManager to mount the obb at /mnt/data ?

dalvik-cache preventing file creation

I'm trying to change the Dalvik VM in order to extract an extra file to the dalvik-cache directory when the dex file is being extracted by JarFile.cpp.
The problem is when I use open() function I'm getting permission denied!
I'm aware that the permissions of the dalvik-cache are "system system", but I see in it other permissions for u0_a## for dex file.
I can create the dex file in the dalvik-cache but not other types of files.
How can that be? What's preventing me on file types to create files?
Applications do not have permission to create files in /data/dalvik-cache. Neither does dexopt.
The installd command does have permission, so it creates the file entry and passes an open file descriptor as an argument to dexopt.
The only time dexopt directly creates files is on engineering builds, where the initial bootstrap classes are created when the zygote process starts. At that point it's running as root with full capabilities.
To create an additional file you'd need to modify installd.

Android NDK can't open any file

I'm trying to open files from C in my Android app/library. This did work at some point but stopped working after changing several things that seem completely irrelevant, and don't run until after this error occurs.
I am using the following function in C: *ppFile = fopen(fullUncFilePathAndName, "r+b"); and then again with permissions "w+b"
After each of these calls, errno = 13 (Permission Denied).
The file path is /data/data/{appFolder}/files/{filename} (got from Context.getFilesDir().getAbsolutePath(), and the working directory according to the C functions is "/". This is on internal storage, but I added the permission WRITE_EXTERNAL_STORAGE just because I had nothing else to do.
Can anyone think why reading or writing to existing and non-existent files in internal storage from an NDK library would fail with permission errors?
This problem solved itself in the best worst way possible. I deleted my app's data folder in the file browser, and then at the next build the app created its files without a hitch.
You could also clean the project and recompile from scratch.

Store a File in Android from a JAR File

I am writing some backend interface code for an Android App. I would like to package up the backend api in a jar file and include it as a lib in the main app. (I have the same problem even it it's a library project).
One of the features of this api is to download a file from a remote server. I have a method which I pass the "root path" into the api.
public void storeRemoteFile(String rootPath)
The method starts a thread, and in the background grabs the file and stores it to the roothPath. I have no problems running this code in the Console, and the file gets downloaded to my PC. So calling this method in a class that lives in the jar file gives me a file permissions error.
java.io.FileNotFoundException: /storage/adcard0/Android/data/com.myapp/files/temp.zip: open failed: EACCES (Permission denied)
Is there any way to give the api permissions to write the file? If not, are there any suggestions on how to allow the api jar to download the file, and pass it up the main app for storage?
Ensure you have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
In your Manifest.

how to grant android mediaserver file write permission?

I am developing several AudioEffect subclasses that are being compiled into Android 4.0.3 ICS... I am trying to dump raw PCM data to files, but because the AudioEffects run in the context of the mediaserver process it seems there is no file writing permissions available.
fopen("/data/local/tmp/pcm_in.pcm", "w");
is returning a NULL pointer and errno 13 (permission denied).
Any ideas how I can grant mediaserver this permission, or write to a folder I can access? I'm compiling the OS, so anything goes...
More specifically: How are permissions for these native/system services determined? I don't suppose they have a AndroidManifest.xml...
I suspect the problem is that you don't have permissions to create a file at /data/local/tmp. In fact i'm not even sure that directory exists on Android.
Instead you should either use the private storage for an app or save the files to the SD card. See this for more information: http://developer.android.com/guide/topics/data/data-storage.html
Maybe you can just create a folder with permission.
root#android:/mount -o remount,rw /
root#android:/mkdir test
root#android:/chown media:media test
root#android:/chmod 777 test
Then you can do whatever you want.
z.B.
fopen("/test/pcm_in.pcm", "w");

Categories

Resources