Android中外掛開發篇之----類載入器
前言
關於外掛,已經在各大平臺上出現過很多,eclipse外掛、chrome外掛、3dmax外掛,所有這些外掛大概都為了在一個主程式中實現比較通用的功能,把業務相關或者讓可以讓使用者自定義擴充套件的功能不附加在主程式中,主程式可在執行時安裝和解除安裝。在android如何實現外掛也已經被廣泛傳播,實現的原理都是實現一套外掛介面,把外掛實現編成apk或者dex,然後在執行時使用DexClassLoader動態載入進來,不過在這個開發過程中會遇到很多的問題,所以這一片就先不介紹如何開發外掛,而是先解決一下開發過程中會遇到的問題,這裡主要就是介紹DexClassLoader這個類使用的過程中出現的錯誤
導讀
一、預備知識
Android中的各種載入器介紹
外掛開發的過程中DexClassLoader和PathClassLoader這兩個類載入器了是很重要的,但是他們也是有區別的,而且我們也知道PathClassLoader是Android應用中的預設載入器。他們的區別是:
DexClassLoader可以載入任何路徑的apk/dex/jar
PathClassLoader只能載入/data/app中的apk,也就是已經安裝到手機中的apk。這個也是PathClassLoader作為預設的類載入器的原因,因為一般程式都是安裝了,在開啟,這時候PathClassLoader就去載入指定的apk(解壓成dex,然後在優化成odex)就可以了。
我們可以看一下他們的原始碼:
DexClassLoader.java
- /*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
-
* You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package dalvik.system;
- import java.io.File;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.zip.ZipFile;
- /**
- * Provides a simple {@link ClassLoader} implementation that operates on a
- * list of jar/apk files with classes.dex entries. The directory that
- * holds the optimized form of the files is specified explicitly. This
- * can be used to execute code not installed as part of an application.
- *
- * The best place to put the optimized DEX files is in app-specific
- * storage, so that removal of the app will automatically remove the
- * optimized DEX files. If other storage is used (e.g. /sdcard), the
- * app may not have an opportunity to remove them.
- */
- publicclass DexClassLoader extends ClassLoader {
- privatestaticfinalboolean VERBOSE_DEBUG = false;
- /* constructor args, held for init */
- privatefinal String mRawDexPath;
- privatefinal String mRawLibPath;
- privatefinal String mDexOutputPath;
- /*
- * Parallel arrays for jar/apk files.
- *
- * (could stuff these into an object and have a single array;
- * improves clarity but adds overhead)
- */
- privatefinal File[] mFiles; // source file Files, for rsrc URLs
- privatefinal ZipFile[] mZips; // source zip files, with resources
- privatefinal DexFile[] mDexs; // opened, prepped DEX files
- /**
- * Native library path.
- */
- privatefinal String[] mLibPaths;
- /**
- * Creates a {@code DexClassLoader} that finds interpreted and native
- * code. Interpreted classes are found in a set of DEX files contained
- * in Jar or APK files.
- *
- * The path lists are separated using the character specified by
- * the "path.separator" system property, which defaults to ":".
- *
- * @param dexPath
- * the list of jar/apk files containing classes and resources
- * @param dexOutputDir
- * directory where optimized DEX files should be written
- * @param libPath
- * the list of directories containing native libraries; may be null
- * @param parent
- * the parent class loader
- */
- public DexClassLoader(String dexPath, String dexOutputDir, String libPath,
- ClassLoader parent) {
- super(parent);
- ......
dexPath:是載入apk/dex/jar的路徑
dexOutDir:是dex的輸出路徑(因為載入apk/jar的時候會解壓除dex檔案,這個路徑就是儲存dex檔案的)
libPath:是載入的時候需要用到的lib庫,這個一般不用
parent:給DexClassLoader指定父載入器
我們在來看一下PathClassLoader的原始碼
PathClassLoader.java
- /*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package dalvik.system;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.RandomAccessFile;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.List;
- import java.util.NoSuchElementException;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipFile;
-
相關推薦
Android中外掛開發篇之----類載入器
前言 關於外掛,已經在各大平臺上出現過很多,eclipse外掛、chrome外掛、3dmax外掛,所有這些外掛大概都為了在一個主程式中實現比較通用的功能,把業務相關或者讓可以讓使用者自定義擴充套件的功能不附加在主程式中,主程式可在執行時安裝和解除安裝。在an
Android中外掛開發篇之----動態載入Activity 免安裝執行程式
一、前言又到週末了,時間過的很快,今天我們來看一下Android中外掛開發篇的最後一篇文章的內容:動態載入Activity(免安裝執行程式),在上一篇文章中說道了,如何動態載入資源(應用換膚原理解析),沒看過的同學,可以轉戰:當然,今天說道的內容還這這篇文章有關係。關於動態載入
Android中外掛開發篇之----應用換膚原理解析
一、前言今天又到週末了,感覺時間過的很快呀.又要寫blog了。那麼今天就來看看應用的換膚原理解析。在之前的一篇部落格中我說道了Android中的外掛開發篇的基礎:類載入器的相關知識。沒看過的同學可以轉戰:二、原理介紹現在市場上有很多應用都有換膚的功能,就是能夠提供給使用者一些
Android中外掛開發篇總結和概述
剛剛終於寫完了外掛開發的最後一篇文章,下面就來總結一下,關於Android中外掛篇從去年的11月份就開始規劃了,主要從三個方面去解讀Android中外掛開發原理。說白了,外掛開發的原理就是:動態載入技術。但是我們在開發外掛的過程中可能會遇到很多問題,所以這裡就分為三篇文章進行解
Java程式設計師從笨鳥到菜鳥之(九十九)深入java虛擬機器(八)開發自己的類載入器
歡迎閱讀本專題的其他部落格: 在大多數情況下,系統預設提供的類載入器實現已經可以滿足需求。但是在某些情況下,您還是需要為應用開發出自己的類載入器。比如您的應用通過網路來傳輸 Java 類的位元組程式碼,為了保證安全性,這些位元組程式碼經過了加密處理。這個時候您
Android開發學習之路--Android Studio外掛開發
前言 因為使用了mvp和dagger,所以每次新的一個功能頁面都需要重新寫一堆東西,比如Activity, Fragment, Presenter,dagger等,而這些程式碼基本上都是大同小異,完全可以寫一個模板,然後生成,略微修改便可以完成我們需要的功能。
Android Studio 外掛開發詳解二:工具類
轉載請標明出處:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自【趙彥軍的部落格】 在外掛開發過程中,我們按照開發一個正式的專案來操作,需要整理一些常用工具類。 Http 請求封裝 在外掛的專案中,我們看到依賴庫如下圖所示:
Android中的多渠道打包之友盟統計篇(二)
上篇簡單瞭解了Android中的多渠道打包,這篇主要說正事了---》 如何多渠道打包: 首先,多渠道打包最重要的是可以統計不同渠道下的應用的情況,比如下載量,日活量等等.. 關於統計的話國內有兩個比較出名的第三方統計工具吧:友盟統計和百度移動統計 這篇說的是用友盟,如果想要
Android中的快取處理及非同步載入圖片類的封裝
一、快取介紹: (一)、Android中快取的必要性: 智慧手機的快取管理應用非常的普遍和需要,是提高使用者體驗的有效手段之一。 1、沒有快取的弊端: 流量開銷:對於客戶端——伺服器端應用,從遠端獲取圖片算是經常要用的一個功能,而圖片資源往往會消耗比較大的流量。 載入速
Android開發——使用BitmapRegionDecoder類載入高清巨圖方案
package com.zhy.blogcodes.largeImage.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import a
Android開發筆記之RecycleView載入不同item佈局的實現
RecycleView是安卓5.0版本以後推出的新控制元件 優點 想要控制其顯示的方式,請通過佈局管理器LayoutManager 想要控制Item間的間隔(可繪製),請通過ItemDecoration 想要控制Item增刪的動畫,請通過ItemAnima
Android中級篇之Fresco-載入圖片基礎[詳細圖解Fresco的使用]
常見問題: 初次使用,我們就先簡單書寫我們的 activity_main.xml : <code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing
【朝花夕拾】Android自定義View篇之(六)Android事件分發機制(中)從原始碼分析事件分發邏輯及經常遇到的一些“詭異”現象
前言 轉載請註明,轉自【https://www.cnblogs.com/andy-songwei/p/11039252.html】謝謝! 在上一篇文章【【朝花夕拾】Android自定義View篇之(
【JVM之記憶體與垃圾回收篇】類載入子系統
# 類載入子系統 ## 概述 ![](https://img2020.cnblogs.com/blog/1542615/202007/1542615-20200713205844260-855669260.png) 完整圖如下: ![](https://img2020.cnblogs.com/blo
Android中Xposed框架篇-微信實現本地視頻發布到朋友圈功能
快速定位 adb 本地 ref jad jadx mps 頁面 視頻 微信非常龐大,還好有一些強大的工具,下面就來總結收獲的知識。 一、使用adb shell dumpsys activity top命令快速定位頁面 二、使用Jadx進行方法跟蹤時候如果發現沒有結
Android中APK簽名工具之jarsigner和apksigner詳解
內容 value signature align light 文件簽名 item als release 一.工具介紹 jarsigner是JDK提供的針對jar包簽名的通用工具, 位於JDK/bin/jarsigner.exe apksigner是Google官方提
Android初試--Android中的檔案操作之SDcard操作(2)
SDCARD:通俗的說,就像外接硬碟。 在模擬器中使用SDCard,你需要先建立一張SDCard卡(當然不是真的SDCard,只是映象檔案)。建立SDCard可以在Eclipse建立模擬器時隨同建立,也可以使用DOS命令進行建立,在DOS視窗中進入android SDK安裝路徑的tools
Android初試--Android中的檔案操作之ROM操作(1)
RAM(Random Access Memory)隨機儲存器,在手機中稱為執行記憶體。通俗的說,就像電腦的記憶體。在斷電以後儲存在上面的資料會自動消失。 ROM(Read-Only Memory)只讀儲存器,存放手機韌體程式碼的儲存器,比如手機的作業系統、一些應用程式如遊戲等。通俗的說,就
Java程式設計師從笨鳥到菜鳥之(九十三)深入java虛擬機器(二)——類載入器詳解(上)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Android入門第九篇之AlertDialog
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!