Android源碼探究之BaseDexClassLoader的使用
前言
一共有4個參數(shù),分來來講。
1:dexFile(String類型)
2:optimizedDirectory(File類型)
3:librarySearchPath(String類型)
4:parent(ClassLoader類型)
一.dexPath(String)
官方的注釋:
* @param dexPath the list of jar/apk files containing classes and * resources, delimited by {@code File.pathSeparator}, which * defaults to {@code ":"} on Android.
包含類和類的jar/apk文件列表資源,由{@code File.pathSeparator}分隔,其中Android上的默認(rèn)值為{@code”:“}。
也就是說這里其實是可以傳入一個集合的。
比如如下的參數(shù)都是可以被接受的:
sdcard/test/aa.apk
sdcard/test/aa.apk:sdcard/test/bb.apk:sdcard/test/cc.apk
sdcard/test/aa.apk:sdcard/test/dd.jar
其中分隔符:,保險起見,可以使用File.pathSeparator替代。
示例代碼如下:
private void loadDex(List<File> apkList) { StringBuilder builder = new StringBuilder(); for (File f : apkList) { builder.append(f.getAbsolutePath()); builder.append(File.separatorChar); } DexClassLoader dexClassLoader = new DexClassLoader(builder.toString(), null, null, getClass().getClassLoader()); }
二.optimizedDirectory
官方的注釋:
this parameter is deprecated and has no effect since API level 26.
解壓的路徑,這里傳入路徑的最主要目的就是為了生成odex文件夾,方便后續(xù)存儲odex文件。
如注釋中所寫,這個參數(shù)26開始已經(jīng)失效了。所以這里就不擴(kuò)展去講了。
三.librarySearchPath
官方的注釋:
* @param librarySearchPath the list of directories containing native
包含native目錄的目錄列表,這里要注意的,傳入的一定是so的上一級目錄才可以。如果是更上一層的目錄是不行的。前言中的問題就出在這,傳入了一個更上一層的目錄地址。
排查流程:
最終使用librarySearchPath的地方是在DexPathList的splitPaths方法中。生成File加入List中:
@UnsupportedAppUsage private static List<File> splitPaths(String searchPath, boolean directoriesOnly) { List<File> result = new ArrayList<>(); if (searchPath != null) { for (String path : searchPath.split(File.pathSeparator)) { ... result.add(new File(path)); } } return result; }
而使用的時候,是使用了findLibrary的方法,for循環(huán)便利上面集合中的所有path,看是否存在。
public String findLibrary(String libraryName) { String fileName = System.mapLibraryName(libraryName); for (NativeLibraryElement element : nativeLibraryPathElements) { String path = element.findNativeLibrary(fileName); if (path != null) { return path; } } return null; }
而尋找的最終會調(diào)用到NativeLibraryElement的findNativeLibrary方法:
public String findNativeLibrary(String name) { maybeInit(); if (zipDir == null) { String entryPath = new File(path, name).getPath(); if (IoUtils.canOpenReadOnly(entryPath)) { return entryPath; } } else if (urlHandler != null) { // Having a urlHandler means the element has a zip file. // In this case Android supports loading the library iff // it is stored in the zip uncompressed. String entryName = zipDir + '/' + name; if (urlHandler.isEntryStored(entryName)) { return path.getPath() + zipSeparator + entryName; } } return null; }
這段代碼也就是問題的核心了,直接使用了new File(path,name)的方式,而不是循環(huán)查找,所以只有上一層才可以。
四.parent
官方的注釋:
@param parent the parent class loader
這個比較簡單,就是上一層的classLoader。
五.總結(jié)
無論是dexPath還是librarySearchPath,都是支持多路徑傳入的。路徑之間使用File.pathSeparator進(jìn)行分隔。dexPath中必須是APK或者Jar包的路徑,而librarySearchPath中必須是so文件的上一層級文件夾才可以。
到此這篇關(guān)于Android源碼探究之BaseDexClassLoader的使用的文章就介紹到這了,更多相關(guān)Android BaseDexClassLoader內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
圖文詳解Android Studio搭建Android集成開發(fā)環(huán)境的過程
這篇文章主要以圖文的方式詳細(xì)介紹了Android Studio搭建Android集成開發(fā)環(huán)境的過程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-12-12Android自定義View實現(xiàn)抽獎轉(zhuǎn)盤
這篇文章主要為大家詳細(xì)介紹了Android自定義View實現(xiàn)抽獎轉(zhuǎn)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12Android自定義View實現(xiàn)等級滑動條的實例
這篇文章主要介紹了 Android自定義View實現(xiàn)等級滑動條的實例的相關(guān)資料,需要的朋友可以參考下2017-04-04Android基礎(chǔ)知識之broadcast廣播詳解
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)知識之broadcast廣播的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06