How to boot jni from SD card on Android 2.1?

I want to load third party jni library at runtime.
I tried to download directly from the SDK. This was expected. I tried to copy the library from sdcard to / data / data / app / and then System.load(/data/data/app/libjni.so)


It works on HTC HERO but doesn't work on HTC Legend with Android 2.1. it fails during native code execution and logs an uninformative stack trace Any other way to do this? Thats the best piece of code that works on 1.5 (HTC Hero) but donot works on 2.1 (HTC Legend).

FileInputStream fis = new FileInputStream("/sdcard/libjni.so");
File nf = new File("/data/data/app/libjni.so");
FileOutputStream fos = new FileOutputStream(nf);
byte[] buf = new byte[2048];
int n;
while ((n = fis.read(buf)) > 0)
    fos.write(buf, 0, n);
fis.close();
fos.close();
System.load("/data/data/app/libjni.so");

      

+2


a source to share


1 answer


Android dynamic loader cannot load executable code from sdcard filesystem because it is marked as non-executable and you cannot map executable memory pages from unused storage. (In theory, you can manually copy the content to the executable anonymous mapped pages, but that's ugly. The SD versions of Android support apps mount the executable filesystems contained in the file on the SD card, but a third party app can't write them)



But what you can do is write the library to a writeable location in internal storage, not to the lib directory for which you don't have write access, but another found from Context.getDir () or Context.getFilesDir () and then load it using full pathname and .so using System.load () instead of System.loadLibrary ().

+1


a source







All Articles