Ann R.
Ann R.

Reputation: 85

Android - Dynamically load system library without exact path?

A couple months ago I graduated from Android samples to work-related projects. As such, I am still painfully new to this build system and fully admit my inexperience with all things *.mk file and Android related.

I have a library that depends on OpenCL v1 or greater. Loading is delayed until the functionality is needed, and only if the minimum version of OpenCL is supported. Up until a couple days ago, I would receive a java.lang.UnsatisfiedLinkError whenever I tried to load it because libGLES_mali.so could not be located. Apparently the device my coworker tested on had this file while my device does not. I found other GLES libs that do exist on my test phone but my code to load them is... longer than I would think it should be.

Trying to use System.loadLibrary("GLES") didn't yield success, nor anything similar.

According to this site (http://www.2net.co.uk/tutorial/android-egl-cgf-is-dead) I need to try to load every GLES library I can think of by name, from both system/lib/ and system/lib/egl/. Consequently, currently my code for loading this library is as follows:

boolean bGles = false;


        if(!bGles) {
            try {
                System.load("system/lib/egl/libGLES.so");
                bGles = true;
            } catch (java.lang.UnsatisfiedLinkError e) {
            }
        }

        if(!bGles) {
            try {
                System.load("system/lib/libGLES.so");
                bGles = true;
            } catch (java.lang.UnsatisfiedLinkError e) {
            }
        }
        if(!bGles) {
            try {
                System.load("system/lib/egl/libGLES_android.so");
                bGles = true;
            } catch (java.lang.UnsatisfiedLinkError e) {
            }
        }
        if(!bGles) {
            try {
                System.load("system/lib/libGLESv1_CM.so");
                bGles = true;
            } catch (java.lang.UnsatisfiedLinkError e) {
            }
        }
        if(!bGles) {
            try {
                System.load("system/lib/egl/libGLESv1_CM.so");
                bGles = true;
            } catch (java.lang.UnsatisfiedLinkError e) {
            }
        }

It's so terribly messy! Is there no way of asking Android to load whatever the default 'GLES' is on the system? Default version number? I can't knock the tutorial completely, as all my libraries now load and function correctly now, but what will I do for other devices? With other names for their GLES libs?

I feel that I must be misunderstanding the article. Certainly, there must be a better way to load a shared system library than this?

Upvotes: 0

Views: 962

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93559

First off, there's a better way to do what you're doing

String libraries[] = {"name1","name2",...}
boolean success = false;
for(String library : libraries) {
  try{
    System.load(library);
    success = true;
    break;
  }
  catch(UnsatisfiedLinkError) {}
}
if(!success) {
   //Handle the failed all case
}

Secondly- why do you think you need to do this to begin with? Why aren't you using the build in OpenGL functionality and Java classes? If you aren't, your app is likely to break badly between devices. Edit: Ok, I now noticed OpenCL and did some digging. Android does not support OpenCL. Some devices do, but there's no general libraries for it. I'd reconsider going this route, if you do follow it you will only ever work on a subset of devices, and you're going to have to add hacks for each new generation.

Upvotes: 2

Related Questions