[英]How does dalvik findlibrary?
首先,在System.java中,它將Runtime調用到loadLibrary。
public static void loadLibrary(String libName) {
SecurityManager smngr = System.getSecurityManager();
if (smngr != null) {
smngr.checkLink(libName);
}
Runtime.getRuntime().loadLibrary(libName, VMStack.getCallingClassLoader());
}
第二,它將VMStack.getCallingClassLoader()調用到findLibrary。
void loadLibrary(String libraryName, ClassLoader loader) {
if (loader != null) {
String filename = loader.findLibrary(libraryName);
if (filename == null) {
throw new UnsatisfiedLinkError("Couldn't load " + libraryName + ": " +
"findLibrary returned null");
}
//....
}
}
所以,我認為VMStack.getCallingClassLoader()
是最有意義的。 但是在其jni文件dalvik_system_VMStack.cpp中 , Dalvik_dalvik_system_VMStack_getCallingClassLoader
函數很難學習。 最后,dalvik如何找到圖書館?
static void Dalvik_dalvik_system_VMStack_getCallingClassLoader(const u4* args,
JValue* pResult){
ClassObject* clazz =
dvmGetCaller2Class(dvmThreadSelf()->interpSave.curFrame);
UNUSED_PARAMETER(args);
if (clazz == NULL)
RETURN_PTR(NULL);
RETURN_PTR(clazz->classLoader);
}
VMStack.getCallingClassLoader()
返回聲明調用當前方法的方法的類的類加載器。 換句話說,如果我的函數foo()
調用Runtime.loadLibrary()
,它將返回foo
的類加載器。
所有這一切都是為了確保庫是在調用findLibrary
的人的上下文中加載的,而不是在java.lang.Runtime
的上下文中。
findLibrary()
很有可能由BaseDexClassLoader
實現,它調用DexPathList.findLibrary()
來完成實際的工作。 有趣的是,遍歷nativeLibraryDirectories
,它從libraryPath
參數初始化為BaseDexClassLoader
構造函數(從PathClassLoader
或DexClassLoader
)。
對於Android應用程序,請查看android.app.ApplicationLoaders
,它使用PathClassLoader
。 如果您追溯到足夠遠,您將看到從ApplicationInfo.nativeLibraryDir
中檢索的目錄。
編輯:更深入地發表評論......
/system/lib
來自java.library.path
屬性,核心庫從LD_LIBRARY_PATH
環境變量中提取該屬性。 特定於應用程序的庫目錄由框架配置。
PackageManagerService
構造函數在mAppLibInstallDir
設置lib路徑, setInternalAppNativeLibraryPath()
配置nativeLibraryDir
。
DexPathList.splitLibraryPath()
將java.library.path
路徑與特定於APK的路徑組合在一起。 有關訂購的說明,請參閱那里的評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.