3

I want to use OpenSL` ES only when available i.e. if Android version >= 2.3

Currently I have in Android.mk

LOCAL_LDLIBS    += -lOpenSLES

But this won't work if Android version < 2.3

Can I somehow load Open SL library dynamically only if Android version >= 2.3, maybe using System.loadLibrary ?

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158

1 Answers1

1

Use Build.VERSION to check the API version before you load the library and if it's lower than API 9, don't load it.

Example:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
    System.loadLibrary("xyz");
}

[edit]

OR if you want to have one native library, loading OpenSL ES dynamically, you can use dlopen. You can find an example on stackoverflow: https://stackoverflow.com/a/1142169/1145705

Community
  • 1
  • 1
chrulri
  • 1,822
  • 14
  • 16
  • But which library for Open SL ES? – Alexander Kulyakhtin Aug 02 '12 at 12:54
  • 1
    Well if you link your library against OpenSLES it wont work without it. So you have to build two libraries, one for OpenSLES and one without OpenSLES support. But in that case, you may be better off in building two seperate APKs for publishing. Otherwise you would ship two libraries and one of them is never used. Edit: You can achieve this by using seperate Version Codes in your AndroidManifest.xml. Have a look: http://developer.android.com/guide/google/play/publishing/multiple-apks.html#VersionCodes – chrulri Aug 02 '12 at 12:57