如何检测Android应用程序加载了哪些本机共享库 [英] How to detect which native shared libraries are loaded by Android application
问题描述
我的应用程序使用本机共享库 (.so),我通过调用 System.loadLibrary("xxx")
加载它.它加载得很好,我可以调用本机方法.
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.
我想知道是否有可能检测到应用程序使用了哪些共享库.我试图通过 PackageManager 列出加载的库:
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;
但它返回一个空列表.仅当我的应用程序使用我在清单的应用程序标记中通过 uses-library
指定的一些 .jar 库(例如 com.google.android.maps)时,它才有效.
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.
如何列出 .so 库?
How can I list .so libraries?
推荐答案
解决方案很简单,非常感谢@praetorian-droid
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...
}
这篇关于如何检测Android应用程序加载了哪些本机共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!