Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a native shared object, in a jar, from an android application?

I've got a shared object, which I'll call 'libFoo.so', compiled against the NDK. I have a java project (I'll call it 'jarlib') that makes use of that shared object, by calling

System.loadLibrary("Foo");

That java project, I export as a jar, and use in an android application. If I comment out all the bits about the shared object, the jar works fine, and I'm able to interact with it perfectly.

I can use jarlib, along with the shared object from a Unity android application with the shared object in ./lib/armeabi/libFoo.so, and it loads it up all peachy.

I'm trying to use the jarlib and libFoo.so in my non-unity android app, and loadLibrary absolutely refuses to find the libFoo.so. I've tried placing it so it will be included in my apk in ./lib, as well as ./lib/armeabi, and nothing seems to make the load call see it.

I've tried including the libFoo.so in ./lib or ./lib/armeabi of the jarlib, but once I try to build/run my android app, I'm told:

The library 'jarlib.jar' contains native libraries that will not run on the device.
The following libraries were found:
  - libFoo.so

with this, if it's in the ./lib (not ./lib/armeabi)

Additionally some of those libraries will interfer with the installation of the application because of their location in lib/
lib/ is reserved for NDK libraries.

I'm not sure what combination I'm missing, but I'm sure it can be done, I just can't seem to figure out how to do it!

like image 804
nitz Avatar asked Dec 08 '11 16:12

nitz


1 Answers

Well! It turns out it was something so tiny, that it caused me eight hours of headaches.

I found that my libFoo.so wasn't showing up in my app.apk/lib/armeabi/libFoo.so like it did when unity built the apks. I assumed it had something to do with that, so I started looking for settings to make sure that my lib folder was copied to the apk.

As it turns out, the lib folder is not, however...

Remember how I said I tried it in (on the android application side):

./lib/libFoo.so
-and-
./lib/armeabi/libFoo.so

Turns out, what I needed to do was put it in a similar path. Oh so similar.

./libs/armeabi/libFoo.so

Yeah. libs. With a s.

In fact, when I renamed my folder 'lib' to 'libs', it changed the icon of the folder in eclipse to have the little android symbol on it, like the bin or res folders have. And sure enough, when you build the apk, the lib/armeabi folder is there (notice it's named 'lib' in the apk, and not 'libs'. This still confuses me.)

One of my co-workers (that was working on this project before me) told me that somewhere along the line, the folder used to be 'lib', but was then changed to 'libs' in a later version of the android SDK. Either way, having just picked up this project, I didn't realize that the folder should have been named 'lib', as it seemed fine to me. I was about ready to pull my hair out. I'm leaving this here in the hopes that SOMEONE has the same problem I did, and manages to solve it a bit faster.

like image 80
nitz Avatar answered Nov 06 '22 19:11

nitz