1

I need to specify the location of some native libraries in my Android application. I was accomplishing this with a hard-coded string:

public static final String DLL_DIR_STR = "/data/data/my.application.package/lib";

but wanted to get the path from Android instead. Following posts like this, I used getDir() to find the lib directory, changing

superCollider = new SCAudio(DLL_DIR_STR);

to

superCollider = new SCAudio(container.$context().getDir("lib", 0).getAbsolutePath());

Oddly, the initial libraries seem to load correctly

Trying to load lib /data/data/my.application.package/lib/libsndfile.so 0x42718d80
Added shared lib /data/data/my.application.package/lib/libsndfile.so 0x42718d80
No JNI_OnLoad found in /data/data/my.application.package/lib/libsndfile.so 0x42718d80, skipping init
Trying to load lib /data/data/my.application.package/lib/libscsynth.so 0x42718d80
Added shared lib /data/data/my.application.package/lib/libscsynth.so 0x42718d80

But when libscsynth tries to load some additional code, it's using the wrong path:

OK, listing opendir(/data/data/my.application.package/app_lib)

Any ideas where the "app_" comes from? I thought I must be using getDir() wrong, but the initial files load fine. Could it be something in the native code? Thanks for your help.

Community
  • 1
  • 1
Tbadams
  • 424
  • 1
  • 5
  • 20
  • 1
    You probably use **System.loadLibrary()** - this method knows where the app libraries are installed. On the contrary, **getDir()** _works hard_ to provide a directory that your app can safely use for file operations, so that it does not clash with special directories, of which **lib** is one example. – Alex Cohn Sep 28 '12 at 22:47

2 Answers2

2

I found the answer, quite by accident, in this post. ApplicationInfo.dataDir holds the location of the data directory, and "lib" is easily navigated to from there:

superCollider = new SCAudio(container.$context().getApplicationInfo().dataDir + "/lib");

Alternatively, nativeLibraryDir takes you directly to the lib directory, but requires API level 9.

Thanks for your help!

Community
  • 1
  • 1
Tbadams
  • 424
  • 1
  • 5
  • 20
  • 1
    It is no coincidence that **nativeLibraryDir** was introduced in Gingerbread. Before, the **lib** directory could be reliably found at **dataDir/../lib**, but since then apps can be installed on the external storage. Therefore, the recommended practice would be to check the API version and use **nativeLibraryDir** if available. – Alex Cohn Sep 29 '12 at 07:14
1

getDir will always prepend app_ to the directory name so it is very odd that it is working the first time. I would either just expect the app_ to be there or try using getFilesDir, at least you always know what it will return. Does SuperCollider have a restriction on what the directory name is?

I found another SuperCollider Project that seems to be doing the same thing you did initially with the comment "// TODO: not very extensible,".. I found that funny :)

JustinMorris
  • 7,259
  • 3
  • 30
  • 36
  • Thanks, I wondered if it was getDir(). "/lib" is the conventional location for native libraries; I think it's possible to change it; fortunately there's a good way to find "lib." – Tbadams Sep 29 '12 at 00:46