我的应用程序使用本机共享库(.so),我通过调用System.loadLibrary("xxx")
加载它。它加载很好,我可以调用本机方法。
我想知道是否有可能检测应用程序使用哪个共享库。我试图通过PackageManager列出加载的库:
PackageManager pm = getPackageManager();
String applicationPackage = this.getApplicationInfo().packageName;
ApplicationInfo ai = pm.getApplicationInfo(applicationPackage, PackageManager.GET_SHARED_LIBRARY_FILES);
String[] soFiles = ai.sharedLibraryFiles;
但它返回一个空列表。仅当我的应用程序使用com.google.android.maps之类的.jar库时,它才有效。我在清单的应用程序标记中由uses-library
指定。
如何列出.so库?
答案 0 :(得分:13)
解决方案很简单,非常感谢@ 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...
}
答案 1 :(得分:5)
使用此代码列出可从您的应用加载的共享本机库:
final File nativeLibraryDir = new File(getApplicationInfo().nativeLibraryDir);
final String[] primaryNativeLibraries = nativeLibraryDir.list();