目前來說,Android中的插件技術以及熱修復技術都跟ClassLoader息息相關,瞭解此技術有助於加深對Android系統的 瞭解。尤爲是對於插件技術來說,對Class的加載基本固定都是一個套路。(熱修復要更復雜一些,涉及到C++層面的方法數等知識)java
最好先閱讀JVM中的ClassLoader,有了這篇基礎而後再看這邊理解會更加深入。此外還能夠谷歌關鍵字搜索一下相關知識,有個基礎概念再看本篇文章更佳。本文不會過多重複其餘BlOG中提到的概念。android
相比其餘多數東拼西湊的文章,這篇文章能從頭到腳幫你梳理一下Android中ClassLoader的知識點,以及代碼層面幫你實戰 加深理解。api
還記得在我以前的JVM那篇文章的結尾,我自定義了一個ClassLoader,經過讀取了一個本地class文件,而後傳遞了 一個byte數組 到defineClass這個方法裏從而成功的加載了一個本地class,可是在android中,這種方法是不被容許的。 咱們來看看源碼:數組
android的classLoader能夠看出來是有defineClass這個方法的,可是這些方法都統一返回了異常,也就是說 android官方不容許咱們用JVM那套方法來加載class,爲了更清晰,我複製一段源碼出來。bash
protected final Class<?> defineClass(String name, byte[] b, int off, int len,
ProtectionDomain protectionDomain)
throws ClassFormatError
{
throw new UnsupportedOperationException("can't load this type of class file");
}
複製代碼
很直觀,能看出來這裏是什麼意思,再接着往下看app
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
if (c == null) {
// If still not found, then invoke findClass in order
// to find the class.
c = findClass(name);
}
}
return c;
}
複製代碼
這裏看一下,同樣很直觀,能夠看出來這裏的loadClass和JVM中的loadClass的流程如出一轍。也是符合雙親委託機制。框架
有必定基礎的人應該知道,android中的類加載器有兩種,DexClassLoader和PathClassLoader。咱們下面就來看看這2個 ClassLoader是如何加載class文件的吧。ide
package dalvik.system;
import dalvik.system.BaseDexClassLoader;
import java.io.File;
public class DexClassLoader extends BaseDexClassLoader {
public DexClassLoader(String dexPath, String optimizedDirectory, String librarySearchPath, ClassLoader parent) {
super((String)null, (File)null, (String)null, (ClassLoader)null);
throw new RuntimeException("Stub!");
}
}
複製代碼
public class PathClassLoader extends BaseDexClassLoader {
public PathClassLoader(String dexPath, ClassLoader parent) {
super((String)null, (File)null, (String)null, (ClassLoader)null);
throw new RuntimeException("Stub!");
}
public PathClassLoader(String dexPath, String librarySearchPath, ClassLoader parent) {
super((String)null, (File)null, (String)null, (ClassLoader)null);
throw new RuntimeException("Stub!");
}
}
複製代碼
嗯 能看出來 這2個都是派生自BaseDexClassLoader類,函數
public class BaseDexClassLoader extends ClassLoader {
public BaseDexClassLoader(String dexPath, File optimizedDirectory, String librarySearchPath, ClassLoader parent) {
throw new RuntimeException("Stub!");
}
protected Class<?> findClass(String name) throws ClassNotFoundException {
throw new RuntimeException("Stub!");
}
protected URL findResource(String name) {
throw new RuntimeException("Stub!");
}
protected Enumeration<URL> findResources(String name) {
throw new RuntimeException("Stub!");
}
public String findLibrary(String name) {
throw new RuntimeException("Stub!");
}
protected synchronized Package getPackage(String name) {
throw new RuntimeException("Stub!");
}
public String toString() {
throw new RuntimeException("Stub!");
}
}
複製代碼
看到這可能不少人就懵逼了,這是啥意思,實際上這裏表明的就是這個類執行的時候,會讓ROM裏的類來實際執行,這裏android studio定位到的源碼 只是告訴咱們,嗯 這裏有一個這樣的類。可是實際的實現是放在Rom中作的。 不少人到這裏可能仍是沒法理解是什麼意思,其實這個地方在不少開源項目中都有實際用法,好比你要反射調用api裏沒有暴露出來給你的類怎麼辦呢?這個類在rom裏是有的 可是api並無暴露出來給你。就能夠用這種寫法了。我舉個滴滴開源框架VirtualApk的例子:oop
看到沒有滴滴也是這麼作的,將這些類放在本身的框架內暴露出來(注意包名要和ROM中的同樣)這樣就能夠在其餘地方反射調用了。 咱們隨便找個源碼進去看一看:
package android.app;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
/**
* @author johnsonlee
*/
public final class ActivityThread {
public static ActivityThread currentActivityThread() {
throw new RuntimeException("Stub!");
}
public static boolean isSystem() {
throw new RuntimeException("Stub!");
}
public static String currentOpPackageName() {
throw new RuntimeException("Stub!");
}
public static String currentPackageName() {
throw new RuntimeException("Stub!");
}
public static String currentProcessName() {
throw new RuntimeException("Stub!");
}
public static Application currentApplication() {
throw new RuntimeException("Stub!");
}
public ApplicationThread getApplicationThread() {
throw new RuntimeException("Stub!");
}
public Instrumentation getInstrumentation() {
throw new RuntimeException("Stub!");
}
public Looper getLooper() {
throw new RuntimeException("Stub!");
}
public Application getApplication() {
throw new RuntimeException("Stub!");
}
public String getProcessName() {
throw new RuntimeException("Stub!");
}
public final ActivityInfo resolveActivityInfo(final Intent intent) {
throw new RuntimeException("Stub!");
}
public final Activity getActivity(final IBinder token) {
throw new RuntimeException("Stub!");
}
final Handler getHandler() {
throw new RuntimeException("Stub!");
}
private class ApplicationThread extends ApplicationThreadNative {
}
}
複製代碼
你看ActivityThread這個類,有過源碼基礎的同窗應該都知道這個類是rom裏咱們api裏沒有這個類的。用這種方法暴露出來之後就可使用它了。
扯遠了,回到正題,既然api 源碼裏看不到真正的實現 咱們只好去rom代碼裏看看了 這裏推薦一個在線查看源碼的網站
這裏明顯看出來咱們的DexClassLoader和pathClassLoader構造函數傳參上面就有不一樣,DexClassLoader須要提供一個額外的path路徑,這個path路徑咱們看下注釋很容易理解:用來存放解壓縮之後的dex文件。(apk和jar包解壓縮均可以 只要解壓縮之後的是dex文件),這個path就是解壓縮的目標路徑。可是PathClassLoader就沒有這個構造函數。只能直接操做dex文件。 總結: DexClassLoader能夠加載任何路徑的apk/dex/jar PathClassLoader只能加載/data/app中的apk,也就是已經安裝到手機中的apk。這個也是PathClassLoader做爲默認的類加載器的緣由,由於通常程序都是安裝了,在打開,這時候PathClassLoader就去加載指定的apk(解壓成dex,而後在優化成odex)就能夠了
繼續跟源碼 跟到BaseClassLoader
能夠看出來BaseClassLoader的構造函數 最終是調用的DexPathList函數,注意這裏2個構造函數也是分別對應的dex和path 兩種classLoader的
跟到DexPathList
嗯 發現這裏都是走的makeDex函數
發現了DexFile這個類,繼續跟:
嗯,一直到這裏,咱們發現最終的loadclass 在DexFile的 loadClassBinaryname方法完成了(這裏能夠和JVM中咱們自定義 的那個ClassLoader比對一下)。
有人要問既然DexFile完成的是最終classLoader須要完成的loadClass的操做,爲啥不直接用dexFile呢? 看註釋:
If you are not calling this from a class loader, this is most likely not going to do what you want. Use {@link Class#forName(String)} instead
這裏明確說了,若是你的DexFile的loadClass操做不是在classLoader裏作的,那你很大可能得不到你想要的結果。。。。
因此之後要記住了,DexFile相關的操做必定要在ClassLoader裏完成
寫個簡單的demo
package com.example.a16040657.androidclassloader;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("wuyue", "MainActivity的類加載加載器:" + MainActivity.class.getClassLoader());
Log.v("wuyue", "Context的類加載加載器:" + Context.class.getClassLoader());
Log.v("wuyue", "ListView的類加載器:" + ListView.class.getClassLoader());
Log.v("wuyue", "應用程序默認加載器:" + getClassLoader());
Log.v("wuyue", "系統類加載器:" + ClassLoader.getSystemClassLoader());
Log.v("wuyue", "系統類加載器和Context的類加載器是否相等:" + (Context.class.getClassLoader() == ClassLoader.getSystemClassLoader()));
Log.v("wuyue", "系統類加載器和應用程序默認加載器是否相等:" + (getClassLoader() == ClassLoader.getSystemClassLoader()));
Log.v("wuyue", "打印應用程序默認加載器的委派機制:");
ClassLoader classLoader = getClassLoader();
while (classLoader != null) {
Log.v("wuyue", "類加載器:" + classLoader);
classLoader = classLoader.getParent();
}
Log.v("wuyue", "打印系統加載器的委派機制:");
classLoader = ClassLoader.getSystemClassLoader();
while (classLoader != null) {
Log.v("wuyue", "類加載器:" + classLoader);
classLoader = classLoader.getParent();
}
}
}
複製代碼
再看看輸出的結果
注意看一下咱們的listview和Context的類加載器都是bootClassLoader,很好理解,和JVM中對照一下,其實一些系統類 都是交給bootClssLoader來加載的,只不過jVM中的是叫BootstrapClassLoader罷了。都是一個意思。
注意看一下系統類加載器和應用程序加載器雖然都是pathclassloader,可是這裏注意看一下 這倆的路徑是不一樣的。 一個是data/app 一個是sytesm/app 因此這倆是不一樣的類加載器。這裏要注意一下。