JNI中l(wèi)oad與loadLibrary的區(qū)別

  • load 和loadLibrary都是System類的方法
  • 作用都是加載需要使用的庫文件
  • 類加載器都是通過Reflection.getCallerClass()獲取
  • 最終調(diào)用的是nativeLoad方法
  • load需要傳入加載類的絕對地址,loadLibrary只需傳入類文件名

load方法源碼

System中-->
  @CallerSensitive
    public static void load(String filename) {
        Runtime.getRuntime().load0(Reflection.getCallerClass(), filename);
    }

//最終調(diào)用Runtime中的load0方法担敌,重點(diǎn)看load0即可
Runtime-->

synchronized void load0(Class<?> fromClass, String filename) {
        if (!(new File(filename).isAbsolute())) {//此處判斷如果文件路徑名是否為絕對路徑峻黍,不是則拋出異常
            throw new UnsatisfiedLinkError(
                "Expecting an absolute path of the library: " + filename);
        }
        if (filename == null) {
            throw new NullPointerException("filename == null");
        }
        String error = nativeLoad(filename, fromClass.getClassLoader());
        if (error != null) {
            throw new UnsatisfiedLinkError(error);
        }
    }

load總結(jié):

  • load方法中會測試傳入的抽象路徑名是否為絕對路徑。
  • 絕對路徑名的定義取決于系統(tǒng)。
  • 在Android上揖铜,絕對路徑以字符“ /”開頭暑认。
loadLibrary源碼
System-->
 @CallerSensitive
    public static void loadLibrary(String libname) {
        Runtime.getRuntime().loadLibrary0(Reflection.getCallerClass(), libname);
    }

Runtime-->
//最終調(diào)用Runtime中l(wèi)oadLibrary0
 private synchronized void loadLibrary0(ClassLoader loader, Class<?> callerClass, String libname) {
        if (libname.indexOf((int)File.separatorChar) != -1) {//此方法判斷l(xiāng)ibname中是否含有分隔符,有的話就拋出異常
            throw new UnsatisfiedLinkError(
    "Directory separator should not appear in library name: " + libname);
        }
        String libraryName = libname;
        // Android-note: BootClassLoader doesn't implement findLibrary(). http://b/111850480
        // Android's class.getClassLoader() can return BootClassLoader where the RI would
        // have returned null; therefore we treat BootClassLoader the same as null here.
        if (loader != null && !(loader instanceof BootClassLoader)) {
            String filename = loader.findLibrary(libraryName);
            if (filename == null) {
                // It's not necessarily true that the ClassLoader used
                // System.mapLibraryName, but the default setup does, and it's
                // misleading to say we didn't find "libMyLibrary.so" when we
                // actually searched for "liblibMyLibrary.so.so".
                throw new UnsatisfiedLinkError(loader + " couldn't find \"" +
                                               System.mapLibraryName(libraryName) + "\"");
            }
            String error = nativeLoad(filename, loader);
            if (error != null) {
                throw new UnsatisfiedLinkError(error);
            }
            return;
        }

        // We know some apps use mLibPaths directly, potentially assuming it's not null.
        // Initialize it here to make sure apps see a non-null value.
        getLibPaths();
        String filename = System.mapLibraryName(libraryName);
        String error = nativeLoad(filename, loader, callerClass);
        if (error != null) {
            throw new UnsatisfiedLinkError(error);
        }
    }

解析:

*  libname.indexOf((int)File.separatorChar) != -1 

File-->
   public static final char separatorChar = fs.getSeparator();
  • 獲取文件中的文件名分隔符辜限,強(qiáng)轉(zhuǎn)成unicode值
  • 判斷l(xiāng)ibname中是否含有分隔符皇拣,不含有則返回-1.
  • 所以不等于-1則拋出異常

loadLibrary總結(jié)

  • 需傳入文件名的名稱部分
  • 通過BootClassLoader子類的findLibrary方法返回絕對地址
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市薄嫡,隨后出現(xiàn)的幾起案子氧急,更是在濱河造成了極大的恐慌,老刑警劉巖毫深,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吩坝,死亡現(xiàn)場離奇詭異,居然都是意外死亡哑蔫,警方通過查閱死者的電腦和手機(jī)钉寝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闸迷,“玉大人嵌纲,你說我怎么就攤上這事⌒裙粒” “怎么了逮走?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長今阳。 經(jīng)常有香客問我师溅,道長,這世上最難降的妖魔是什么盾舌? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任墓臭,我火速辦了婚禮,結(jié)果婚禮上妖谴,老公的妹妹穿的比我還像新娘窿锉。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布榆综。 她就那樣靜靜地躺著妙痹,像睡著了一般。 火紅的嫁衣襯著肌膚如雪鼻疮。 梳的紋絲不亂的頭發(fā)上怯伊,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天,我揣著相機(jī)與錄音判沟,去河邊找鬼耿芹。 笑死,一個胖子當(dāng)著我的面吹牛挪哄,可吹牛的內(nèi)容都是我干的吧秕。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼迹炼,長吁一口氣:“原來是場噩夢啊……” “哼砸彬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起斯入,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤砂碉,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后刻两,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體增蹭,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年磅摹,在試婚紗的時候發(fā)現(xiàn)自己被綠了滋迈。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡户誓,死狀恐怖饼灿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情帝美,我是刑警寧澤赔退,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站证舟,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏窗骑。R本人自食惡果不足惜女责,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望创译。 院中可真熱鬧抵知,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至掖疮,卻和暖如春初茶,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背浊闪。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工恼布, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人搁宾。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓折汞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親盖腿。 傳聞我的和親對象是個殘疾皇子爽待,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353