[英]Where and when is the instance of PathClassLoader created in android source code?
當我研究android源代碼時,我注意到app中的通用類加載器是PathClassLoader
一個實例,並且這個類中有兩個構造函數。 一個是:
public PathClassLoader(String dexPath, ClassLoader parent) {
super(dexPath, null, null, parent);
}
另一個是:
public PathClassLoader(String dexPath, String libraryPath,
ClassLoader parent) {
super(dexPath, null, libraryPath, parent);
}
但是在app啟動過程中,我無法在源代碼中找到第二個構造函數的調用。 那么libraryPath參數的價值來自哪里呢? 眾所周知, libraryPath引用包含本機庫的目錄列表,並用於初始化nativeLibraryDirectories
的值,該值是DexPathList對象的字段。 因此,如果第二個構造函數沒有調用三個參數,那么如何初始化nativeLibraryDirectories
的值? 因此,應用程序如何找到其本機庫?
實際上,我想知道誰決定了nativeLibraryDirectories的價值?
希望有人可以指導我。 非常感謝。
那么libraryPath參數的價值來自哪里呢?
您可以使用Android Studio搜索來查找它。 執行“在路徑中查找” ,指定Android源目錄的“范圍”參數。 作為查找正則表達式后面的粘貼的文本:
new PathClassLoader\(\w+, \w+, \w+\)\;
這匹配構造函數的調用與三個參數。 另外,不要忘記選中“正則表達式”復選框:
然后在預覽選項卡中,您將能夠看到結果:
使用相同的技術,您可以找出誰正在調用PathClassLoaderFactory#createClassLoader()
函數:
在ZygoneInit.java
您將能夠找到以下代碼:
/**
* Creates a PathClassLoader for the system server. It also creates
* a shared namespace associated with the classloader to let it access
* platform-private native libraries.
*/
private static PathClassLoader createSystemServerClassLoader(String systemServerClasspath,
int targetSdkVersion) {
String librarySearchPath = System.getProperty("java.library.path");
return PathClassLoaderFactory.createClassLoader(systemServerClasspath,
librarySearchPath,
null /* libraryPermittedPath */,
ClassLoader.getSystemClassLoader(),
targetSdkVersion,
true /* isNamespaceShared */);
}
現在,回到你的問題。
所以,如果第二個構造函數沒有調用三個參數...
有, ZygoteInit#handleSystemServerProcess()
調用createSystemServerClassLoader()
,它最終將調用PathClassLoader
3個args構造PathClassLoader
。
實際上,我想知道誰決定了nativeLibraryDirectories的價值?
從上面的代碼中可以看出,它默認為系統屬性"java.library.path"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.