Choosing shared libraries at runtime

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Choosing shared libraries at runtime

Russell Kay
All,

I have searched for this and cannot find an answer so apologies if this is in the archives and my google fu is not good enough to find it.

I have 2 shared libraries that I want to decide at runtime which one to use, they both implement the same interface so I thought it would be fairly simple to do by just doing the System.loadLibrary() call for the correct one before I do the loadLibrary for the main library that we use, but that is not working. It looks like it is not just the same LOCAL_MODULE name that is required but it has to be the same Filename as well or I get an UnsatisfiedLinkError.

That is the outline now for more specifics, the library is an OpenAL implementation and I have one version for (pre OpenSL that uses AudioTrack and one for OpenSL), yes we still want to keep Android 2.1 and 2.2 compatibility for reasons best left just now...

so I build each library and they have both LOCAL_MODULE set to openal and LOCAL_MODULE_FILENAME set to libopenal and one set to libopenal-at (they are built in separate invocations of ndk-build) but it appears at runtime that the full filename for the .so is baked in so I cannot just load libopenal-at at runtime or I get the error.

Can anyone help with any suggestions???

Russell

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Choosing shared libraries at runtime

David Turner
Hello Russel,

On Mon, Jan 7, 2013 at 5:23 PM, Russell Kay <[hidden email]> wrote:
All,

I have searched for this and cannot find an answer so apologies if this is in the archives and my google fu is not good enough to find it.

I have 2 shared libraries that I want to decide at runtime which one to use, they both implement the same interface so I thought it would be fairly simple to do by just doing the System.loadLibrary() call for the correct one before I do the loadLibrary for the main library that we use, but that is not working. It looks like it is not just the same LOCAL_MODULE name that is required but it has to be the same Filename as well or I get an UnsatisfiedLinkError.

That is the outline now for more specifics, the library is an OpenAL implementation and I have one version for (pre OpenSL that uses AudioTrack and one for OpenSL), yes we still want to keep Android 2.1 and 2.2 compatibility for reasons best left just now...

so I build each library and they have both LOCAL_MODULE set to openal and LOCAL_MODULE_FILENAME set to libopenal and one set to libopenal-at (they are built in separate invocations of ndk-build) but it appears at runtime that the full filename for the .so is baked in so I cannot just load libopenal-at at runtime or I get the error.

Can anyone help with any suggestions???


I'm not sure I understand what you're doing when packaging your application, but ELF mandates that the full library name is hard-coded in the .dynamic section, so you can't simply rename a library and have it load under a different name.

One possible solution would be to build both libraries as distinct module names, then do something like this to load then:

  if (systemHasOpenSLES)
    System.loadLibrary("openal");
  else
    System.loadLibrary("openal-at");
 
Where 'systemHasOpenSLES' is a boolean that would be computed with something like:

  systemHasOpenSLES = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD;

Hope this helps

Russell

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
Reply | Threaded
Open this post in threaded view
|

Re: Choosing shared libraries at runtime

Russell Kay
David,

This is the exact solution I am looking for but I get the unsatisfied link error when the other library is loaded I suspect my ndk-build files are setup incorrectly.

Basically I have 2 shared libs as described previously one is built externally but the other is built with the main game. I suspect that I just have this setup incorrectly.

Any pointers on how I should go about setting this up.

Russell 

Sent from my iPhone

On 7 Jan 2013, at 08:57, David Turner <[hidden email]> wrote:

Hello Russel,

On Mon, Jan 7, 2013 at 5:23 PM, Russell Kay <[hidden email]> wrote:
All,

I have searched for this and cannot find an answer so apologies if this is in the archives and my google fu is not good enough to find it.

I have 2 shared libraries that I want to decide at runtime which one to use, they both implement the same interface so I thought it would be fairly simple to do by just doing the System.loadLibrary() call for the correct one before I do the loadLibrary for the main library that we use, but that is not working. It looks like it is not just the same LOCAL_MODULE name that is required but it has to be the same Filename as well or I get an UnsatisfiedLinkError.

That is the outline now for more specifics, the library is an OpenAL implementation and I have one version for (pre OpenSL that uses AudioTrack and one for OpenSL), yes we still want to keep Android 2.1 and 2.2 compatibility for reasons best left just now...

so I build each library and they have both LOCAL_MODULE set to openal and LOCAL_MODULE_FILENAME set to libopenal and one set to libopenal-at (they are built in separate invocations of ndk-build) but it appears at runtime that the full filename for the .so is baked in so I cannot just load libopenal-at at runtime or I get the error.

Can anyone help with any suggestions???


I'm not sure I understand what you're doing when packaging your application, but ELF mandates that the full library name is hard-coded in the .dynamic section, so you can't simply rename a library and have it load under a different name.

One possible solution would be to build both libraries as distinct module names, then do something like this to load then:

  if (systemHasOpenSLES)
    System.loadLibrary("openal");
  else
    System.loadLibrary("openal-at");
 
Where 'systemHasOpenSLES' is a boolean that would be computed with something like:

  systemHasOpenSLES = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD;

Hope this helps

Russell

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.