Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect which native shared libraries are loaded by Android application

My application uses native shared library (.so), I am loading it by calling System.loadLibrary("xxx"). It loads fine and I can call the native methods.

I wonder if there is any possibility to detect which shared libraries application uses. I tried to list loaded libraries by PackageManager:

PackageManager pm = getPackageManager();
String applicationPackage = this.getApplicationInfo().packageName;
ApplicationInfo ai = pm.getApplicationInfo(applicationPackage, PackageManager.GET_SHARED_LIBRARY_FILES);
String[] soFiles = ai.sharedLibraryFiles;

but it returns an empty list. It works only if my application uses some .jar libraries like com.google.android.maps which I specify by uses-library in the application tag of the manifest.

How can I list .so libraries?

like image 540
vitakot Avatar asked Jan 05 '12 10:01

vitakot


1 Answers

The solution is simple, many thanks to @ praetorian-droid

  try {
        Set<String> libs = new HashSet<String>();
        String mapsFile = "/proc/" + android.os.Process.myPid() + "/maps";
        BufferedReader reader = new BufferedReader(new FileReader(mapsFile));
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.endsWith(".so")) {
                int n = line.lastIndexOf(" ");
                libs.add(line.substring(n + 1));
            }
        }
        Log.d("Ldd", libs.size() + " libraries:");
        for (String lib : libs) {
            Log.d("Ldd", lib);
        }
    } catch (FileNotFoundException e) {
        // Do some error handling...
    } catch (IOException e) {
        // Do some error handling...
    }
like image 103
vitakot Avatar answered Oct 02 '22 20:10

vitakot