最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android源碼探究之BaseDexClassLoader的使用

 更新時間:2022年08月10日 11:41:45   作者:失落夏天  
今天解決一個插件化問題的時候,竟然發(fā)現(xiàn)SO沒有正常加載,很怪異,最終排查下來發(fā)現(xiàn)竟然是參數(shù)傳入錯誤導(dǎo)致的。這就扯到了本文的標(biāo)題上了,BaseDexClassLoader中的4個參數(shù)該如何傳入,傳入的又是什么呢

前言

一共有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í)是可以傳入一個集合的。

比如如下的參數(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)文章

  • SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)

    SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能(四)

    這篇文章主要介紹了SimpleCommand實(shí)現(xiàn)上傳文件或視頻功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android ListView之setEmptyView正確使用方法

    Android ListView之setEmptyView正確使用方法

    這篇文章主要介紹了Android ListView之setEmptyView正確使用方法的相關(guān)資料,希望通過本文能幫助到大家使用該方法,需要的朋友可以參考下
    2017-09-09
  • AsyncTask的三個屬性值和四個步驟

    AsyncTask的三個屬性值和四個步驟

    本文主要介紹了AsyncTask的三個屬性值和四個步驟,具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Android集成Unity的兩種方案

    Android集成Unity的兩種方案

    現(xiàn)在市面上的形形色色Android客戶端,為了更優(yōu)的用戶體驗,我們開發(fā)的上游產(chǎn)品和交互往往會在界面里設(shè)計很多動效,傳統(tǒng)的一頁頁的靜態(tài)展示頁面已經(jīng)不足以滿足用戶的審美需求了,本文將給大家分享Android集成Unity的兩種方案,感興趣的朋友可以參考下
    2024-05-05
  • 圖文詳解Flutter單例的實(shí)現(xiàn)

    圖文詳解Flutter單例的實(shí)現(xiàn)

    一個類只允許創(chuàng)建一個實(shí)例,那這個類就是一個單例類,這種設(shè)計模式就叫作單例設(shè)計模式,簡稱單例模式,下面這篇文章主要給大家介紹了關(guān)于Flutter單例的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2021-12-12
  • Android中Canvas的常用方法總結(jié)

    Android中Canvas的常用方法總結(jié)

    在Android自定義View的時候,我們經(jīng)常需要繪制一些自己想要的效果。這里就需要使用Canvas對象。下面這篇文章將Canvas對象常用方法做個筆記,方便自己和大家以后使用的時候查閱,下面來一起看看吧。
    2016-09-09
  • android 日志文件LogUtils實(shí)例

    android 日志文件LogUtils實(shí)例

    這篇文章主要介紹了android 日志文件LogUtils實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 使用Timer實(shí)現(xiàn)網(wǎng)頁勻速加載的進(jìn)度條樣式

    使用Timer實(shí)現(xiàn)網(wǎng)頁勻速加載的進(jìn)度條樣式

    這篇文章主要介紹了使用Timer實(shí)現(xiàn)網(wǎng)頁勻速加載的進(jìn)度條樣式,在使用WebView加載網(wǎng)頁時有時候網(wǎng)速等原因加載比較慢時,影響用戶的體驗度,今天小編給大家分享使用timer實(shí)現(xiàn)網(wǎng)頁勻速加載的進(jìn)度條樣式,需要的的朋友參考下吧
    2017-01-01
  • Android DragImageView實(shí)現(xiàn)下拉拖動圖片放大效果

    Android DragImageView實(shí)現(xiàn)下拉拖動圖片放大效果

    這篇文章主要為大家詳細(xì)介紹了Android DragImageView實(shí)現(xiàn)下拉拖動圖片放大效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • android 捕捉異常并上傳至服務(wù)器的簡單實(shí)現(xiàn)

    android 捕捉異常并上傳至服務(wù)器的簡單實(shí)現(xiàn)

    本篇文章主要介紹了android 捕捉異常并上傳至服務(wù)器的簡單實(shí)現(xiàn),具有一定的參考價值,有興趣的可以了解一下。
    2017-04-04

最新評論

黄陵县| 江永县| 奎屯市| 余江县| 双辽市| 监利县| 贞丰县| 嘉鱼县| 南安市| 突泉县| 林周县| 上杭县| 柳河县| 舒城县| 河源市| 龙井市| 通道| 辰溪县| 屏东县| 三门峡市| 石景山区| 安溪县| 济宁市| 田林县| 宝山区| 玉溪市| 明溪县| 乐业县| 灵武市| 康乐县| 忻州市| 图们市| 锡林浩特市| 西畴县| 万州区| 柞水县| 丁青县| 晋宁县| 屏山县| 星子县| 阿鲁科尔沁旗|