前言
插件化技術(shù)火熱已久花墩,為什么會(huì)有插件化复斥,時(shí)勢(shì)造英雄吧营密,隨著移動(dòng)互聯(lián)網(wǎng)的快速發(fā)展,業(yè)務(wù)的飛速增長目锭,如何在有限時(shí)間給用戶提供高質(zhì)量的APP评汰,當(dāng)線上出現(xiàn)各種BUG,如何快速修復(fù)并發(fā)布上線痢虹,插件化的意義也就在這里了被去。目前插件化解決方案分為兩個(gè)方向,一是以張勇的DroidPlugin框架為代表的動(dòng)態(tài)替換方案奖唯,對(duì)Android底層的各種類進(jìn)行Hook惨缆,來達(dá)到加載插件的四大組件的目的;二是以任玉剛的DL框架為代表的靜態(tài)代理方案,通過ProxyActivity統(tǒng)一加載插件中的Activity坯墨。如何學(xué)好插件化這不是一件容易的事寂汇,需要我們對(duì)一些底層的知識(shí)有所了解,本篇文章講解Binder和AIDL原理捣染。
Binder原理
Binder是Android系統(tǒng)提供的一種IPC機(jī)制健无,從Android整體上看就是一個(gè)基于Binder通信的C/S架構(gòu),在Binder中分為Client端和Server端液斜,哪個(gè)發(fā)消息哪個(gè)就是Client端累贤,哪個(gè)接受消息哪個(gè)就是Server端。
通過上面Binder的架構(gòu)圖可以知道Client少漆、Server和ServiceManager三者間關(guān)系臼膏,Server進(jìn)程注冊(cè)的各種Service會(huì)保存在ServiceManager中,對(duì)于ServiceManager來說示损,Server是ServiceManager的客戶端渗磅,那么ServiceManager就是服務(wù)端了。如果Client進(jìn)程想要使用某個(gè)Service检访,需要到ServiceManager中獲取始鱼,對(duì)ServiceManager來說,Client是ServiceManager的客戶端脆贵。Client與Server通過ServiceManager建立了通信的道路医清,上面的IPC就是進(jìn)程間通信,Client與Server分屬兩個(gè)不同的進(jìn)程卖氨,這兩個(gè)進(jìn)程如果想要通信就需要ServiceManager來配合会烙。
接著看下圖的Binder通信流程圖:
Client端想要調(diào)用Server端的方法,由于Client和Server在兩個(gè)不同的進(jìn)程中筒捺,因此不能直接訪問柏腻,這時(shí)需要Server端向ServiceManager注冊(cè)服務(wù),注冊(cè)的過程中會(huì)向Binder驅(qū)動(dòng)的全局鏈表binder_procs中插入服務(wù)端的信息系吭,ServiceManager的svcinfo列表用于緩存注冊(cè)過的服務(wù)五嫂,Client會(huì)通過ServiceManager的svcinfo列表查詢并返回服務(wù)端的代理,拿到服務(wù)端代理對(duì)象就可以調(diào)用代理對(duì)象的方法肯尺,調(diào)用過程中通過BinderProxy將請(qǐng)求參數(shù)發(fā)送給ServiceManager沃缘,通過共享內(nèi)存的方式完成通信,Binder的整體通信流程已經(jīng)講了大概蟆盹,下面稍微補(bǔ)充下共享內(nèi)存的知識(shí)孩灯。
跨進(jìn)程通信是需要內(nèi)核空間支持的闺金,在Android系統(tǒng)中逾滥,這個(gè)運(yùn)行在內(nèi)核空間,負(fù)責(zé)各個(gè)用戶進(jìn)程通過Binder實(shí)現(xiàn)通信的內(nèi)核模塊就叫Binder驅(qū)動(dòng)。Binder IPC 機(jī)制中涉及到的內(nèi)存映射通過 mmap() 來實(shí)現(xiàn)寨昙,mmap() 是操作系統(tǒng)中一種內(nèi)存映射的方法讥巡。內(nèi)存映射簡(jiǎn)單的講就是將用戶空間的一塊內(nèi)存區(qū)域映射到內(nèi)核空間。映射關(guān)系建立后舔哪,用戶對(duì)這塊內(nèi)存區(qū)域的修改可以直接反應(yīng)到內(nèi)核空間欢顷;反之內(nèi)核空間對(duì)這段區(qū)域的修改也能直接反應(yīng)到用戶空間。
內(nèi)存映射能減少數(shù)據(jù)拷貝次數(shù)捉蚤,實(shí)現(xiàn)用戶空間和內(nèi)核空間的高效互動(dòng)抬驴。兩個(gè)空間各自的修改能直接反映在映射的內(nèi)存區(qū)域,從而被對(duì)方空間及時(shí)感知缆巧。也正因?yàn)槿绱瞬汲郑瑑?nèi)存映射能夠提供對(duì)進(jìn)程間通信的支持。
Binder IPC通信過程是這樣的陕悬,首先Binder驅(qū)動(dòng)在內(nèi)核空間創(chuàng)建一個(gè)數(shù)據(jù)接收緩存區(qū)题暖,接著在內(nèi)核空間開辟一塊內(nèi)核緩存區(qū),建立內(nèi)核緩存區(qū)和內(nèi)核中數(shù)據(jù)接收緩存區(qū)以及內(nèi)核中數(shù)據(jù)接收緩存區(qū)和接受進(jìn)程用戶空間地址之間的映射關(guān)系捉超。發(fā)送方進(jìn)程通過系統(tǒng)調(diào)用 copy_from_user() 將數(shù)據(jù) copy 到內(nèi)核中的內(nèi)核緩存區(qū)胧卤,由于內(nèi)核緩存區(qū)和接收進(jìn)程的用戶空間存在內(nèi)存映射,因此也就相當(dāng)于把數(shù)據(jù)發(fā)送到了接收進(jìn)程的用戶空間拼岳,這樣便完成了一次進(jìn)程間的通信枝誊。
AIDL原理
首頁創(chuàng)建一個(gè)Test.aidl,里面一個(gè)add方法惜纸,生成的代碼如下:
public interface Test extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.administrator.project01.Test
{
private static final java.lang.String DESCRIPTOR = "com.example.administrator.project01.Test";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.administrator.project01.Test interface,
* generating a proxy if needed.
*/
public static com.example.administrator.project01.Test asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.administrator.project01.Test))) {
return ((com.example.administrator.project01.Test)iin);
}
return new com.example.administrator.project01.Test.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
java.lang.String descriptor = DESCRIPTOR;
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(descriptor);
return true;
}
case TRANSACTION_add:
{
data.enforceInterface(descriptor);
int _arg0;
_arg0 = data.readInt();
int _arg1;
_arg1 = data.readInt();
int _result = this.add(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements com.example.administrator.project01.Test
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public int add(int first, int second) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(first);
_data.writeInt(second);
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public int add(int first, int second) throws android.os.RemoteException;
}
IBinder 是一個(gè)接口侧啼,代表了一種跨進(jìn)程通信的能力。只要實(shí)現(xiàn)了這個(gè)接口堪簿,這個(gè)對(duì)象就能跨進(jìn)程傳輸痊乾。IInterface 代表的就是 Server 進(jìn)程對(duì)象具備什么樣的能力(能提供哪些方法,其實(shí)對(duì)應(yīng)的就是 AIDL 文件中定義的接口)椭更,BinderProxy 類是 Binder 類的一個(gè)內(nèi)部類哪审,它代表遠(yuǎn)程進(jìn)程的 Binder 對(duì)象的本地代理;這兩個(gè)類都繼承自 IBinder, 因而都具有跨進(jìn)程傳輸?shù)哪芰β瞧伲粚?shí)際上湿滓,在跨進(jìn)程的時(shí)候,Binder 驅(qū)動(dòng)會(huì)自動(dòng)完成這兩個(gè)對(duì)象的轉(zhuǎn)換舌狗。Stub繼承了 Binder, 說明它是一個(gè) Binder 本地對(duì)象叽奥,它實(shí)現(xiàn)了 IInterface 接口,表明它具有 Server 承諾給 Client 的能力痛侍;Stub 是一個(gè)抽象類朝氓,具體的 IInterface 的相關(guān)實(shí)現(xiàn)需要開發(fā)者自己實(shí)現(xiàn)。
public static com.example.administrator.project01.Test asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.administrator.project01.Test))) {
return ((com.example.administrator.project01.Test)iin);
}
return new com.example.administrator.project01.Test.Stub.Proxy(obj);
}
在Stub中一個(gè)asInterface方法,它的用處就是判斷Client和Server是否在同一個(gè)進(jìn)程中赵哲,如果在同一個(gè)進(jìn)程中就是直接返回本地對(duì)象待德,否則返回代理對(duì)象Proxy。
private static class Proxy implements com.example.administrator.project01.Test
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public int add(int first, int second) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(first);
_data.writeInt(second);
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
代理類Proxy實(shí)現(xiàn)了Test接口枫夺,add方法中将宪,通過Parcel將數(shù)據(jù)序列化,_data用于裝載數(shù)據(jù)橡庞,將函數(shù)名稱和函數(shù)參數(shù)寫入到_data中较坛,_reply用于接收函數(shù)返回值,最后使用IBinder的transact方法將數(shù)據(jù)傳遞給Binder的Server端了扒最,mRemote就是通過asInterface方法傳遞過來的obj參數(shù)燎潮,在Server端接收到Client進(jìn)程傳遞過來的數(shù)據(jù),調(diào)用對(duì)應(yīng)的函數(shù)扼倘,得到結(jié)果并返回确封。