概述
1:該庫主要在插件開發(fā)過程中通過dependencies { compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.1' ... }
引入
2:該庫主要作用為 Java反射”來調(diào)用主程序中 RePlugin Host Library 的相關接口豺妓,通過com.qihoo360.replugin.RePlugin的各種方法第献,提供“雙向通信能力”
3:該庫入口為com.qihoo360.replugin.Entry,該類的create
唯一方法會在replugin-host-library
的com.qihoo360.loader2.Plugin load(int load, boolean useCache)
當load
傳Plugin.LOAD_APP
時去加載插件,并反射Entry的create方法
源碼分析
1: com.qihoo360.replugin.Entry
public static final IBinder create(Context context, ClassLoader cl, IBinder manager) {
// 初始化插件框架
RePluginFramework.init(cl);
// 初始化Env
RePluginEnv.init(context, cl, manager);
return new IPlugin.Stub() {
@Override
public IBinder query(String name) throws RemoteException {
return RePluginServiceManager.getInstance().getService(name);
}
};
}
2com.qihoo360.replugin.RePluginFramework
static boolean init(ClassLoader cl) {
synchronized (LOCK) {
return initLocked(cl);
}
}
private static boolean initLocked(ClassLoader cl) {
if (mInitialized) {
return mHostInitialized;
}
mInitialized = true;
try {
//
RePluginInternal.ProxyRePluginInternalVar.initLocked(cl);
RePlugin.ProxyRePluginVar.initLocked(cl);
PluginLocalBroadcastManager.ProxyLocalBroadcastManagerVar.initLocked(cl);
PluginProviderClient.ProxyRePluginProviderClientVar.initLocked(cl);
PluginServiceClient.ProxyRePluginServiceClientVar.initLocked(cl);
IPC.ProxyIPCVar.initLocked(cl);
mHostInitialized = true;
} catch (final Throwable e) {
if (LogRelease.LOGR) {
Log.e(TAG, e.getMessage(), e);
}
}
return mHostInitialized;
}
2.1 com.qihoo360.replugin.RePluginInternal.ProxyRePluginInternalVar
static class ProxyRePluginInternalVar {
private static MethodInvoker createActivityContext;
private static MethodInvoker handleActivityCreateBefore;
private static MethodInvoker handleActivityCreate;
private static MethodInvoker handleActivityDestroy;
private static MethodInvoker handleRestoreInstanceState;
private static MethodInvoker startActivity;
private static MethodInvoker startActivityForResult;
private static MethodInvoker loadPluginActivity;
static void initLocked(final ClassLoader classLoader) {
final String factory2 = "com.qihoo360.i.Factory2";
final String factory = "com.qihoo360.i.Factory";
// 初始化Factory2相關方法
createActivityContext = new MethodInvoker(classLoader, factory2, "createActivityContext", new Class<?>[]{Activity.class, Context.class});
handleActivityCreateBefore = new MethodInvoker(classLoader, factory2, "handleActivityCreateBefore", new Class<?>[]{Activity.class, Bundle.class});
handleActivityCreate = new MethodInvoker(classLoader, factory2, "handleActivityCreate", new Class<?>[]{Activity.class, Bundle.class});
handleActivityDestroy = new MethodInvoker(classLoader, factory2, "handleActivityDestroy", new Class<?>[]{Activity.class});
handleRestoreInstanceState = new MethodInvoker(classLoader, factory2, "handleRestoreInstanceState", new Class<?>[]{Activity.class, Bundle.class});
startActivity = new MethodInvoker(classLoader, factory2, "startActivity", new Class<?>[]{Activity.class, Intent.class});
startActivityForResult = new MethodInvoker(classLoader, factory2, "startActivityForResult", new Class<?>[]{Activity.class, Intent.class, int.class, Bundle.class});
// 初始化Factory相關方法
loadPluginActivity = new MethodInvoker(classLoader, factory, "loadPluginActivity", new Class<?>[]{Intent.class, String.class, String.class, int.class});
}
}
其實2.0中initLocked
的相關方法都是去初始化一些MethodInvoker
來反射調(diào)用主程序中 RePlugin Host Library的相關接口。
RePluginInternal.ProxyRePluginInternalVar.initLocked(cl)-----> 初始化為反射調(diào)用主程序中 com.qihoo360.i.Factory2及com.qihoo360.i.Factory相關方法
RePlugin.ProxyRePluginVar.initLocked(cl);-----> 初始化為反射調(diào)用主程序中 com.qihoo360.replugin.RePlugin相關控制插件install、preload及 activity控制相關方法
PluginLocalBroadcastManager.ProxyLocalBroadcastManagerVar.initLocked(cl)----->
初始化為反射調(diào)用主程序中相關控制Broadcast相關方法
PluginProviderClient.ProxyRePluginProviderClientVar.initLocked(cl)----->
初始化為反射調(diào)用主程序中 com.qihoo360.loader2.mgr.PluginProviderClient相關控制Provider相關方法
PluginServiceClient.ProxyRePluginServiceClientVar.initLocked(cl);----->
初始化為反射調(diào)用主程序中 com.qihoo360.loader2.mgr.PluginServiceClient相關控制service相關方法
IPC.ProxyIPCVar.initLocked(cl);----->初始化為反射調(diào)用主程序中com.qihoo360.replugin.base.IPC相關獲取儲存的IPC信息
3com.qihoo360.replugin.RePluginEnv
/**
* 插件環(huán)境相關變量的封裝
*
* @author RePlugin Team
*/
public class RePluginEnv {
private static Context sPluginContext;
private static Context sHostContext;
private static ClassLoader sHostClassLoader;
private static IBinder sPluginManager;
/**
* NOTE:如需使用MobileSafeHelper類,請務必先在Entry中調(diào)用此方法
*/
static void init(Context context, ClassLoader cl, IBinder manager) {
sPluginContext = context;
// 確保獲取的一定是主程序的Context
sHostContext = ((ContextWrapper) context).getBaseContext();
sHostClassLoader = cl;
// 從宿主傳過來的,目前恒為null
sPluginManager = manager;
}
/**
* 獲取宿主部分的Context對象
* 可用來:獲取宿主部分的資源画恰、反射類、Info等信息
* <p>
* 注意:此Context對象不能用于插件(如資源吸奴、類等)的獲取允扇。
* 若需使用插件的Context對象,可直接調(diào)用Activity中的getApplicationContext(7.1.0及以后才支持)则奥,或直接使用Activity對象即可
*/
public static Context getHostContext() {
return sHostContext;
}
/**
* 獲取宿主部分的ClassLoader對象
* 可用來:獲取宿主部分的反射類
* <p>
* 注意:同HostContext一樣考润,這里的ClassLoader不能加載插件中的class
*/
public static ClassLoader getHostCLassLoader() {
return sHostClassLoader;
}
/**
* 獲取該插件的PluginContext
* @return
*/
public static Context getPluginContext() {
return sPluginContext;
}
給插件環(huán)境賦值基本變量
4 com.qihoo360.replugin.RePluginServiceManager
private ConcurrentHashMap<String, IBinder> mServices = new ConcurrentHashMap<String, IBinder>();
/**
* 獲取已注冊的IBinder
*
* @param name
* @return
*/
public IBinder getService(final String name) {
if (LogDebug.LOG) {
LogDebug.d(TAG, "get service for IPlugin.query, name = " + name);
}
if (TextUtils.isEmpty(name)) {
throw new IllegalArgumentException("service name can not value null");
}
IBinder ret = mServices.get(name);
if (ret == null) {
return null;
}
if (!ret.isBinderAlive() || !ret.pingBinder()) {
mServices.remove(name);
return null;
}
return ret;
}
該類的作用就是通過mServices
緩存并提供對外提供通信的能力binder
至此Replugin4個庫分工職責 分析學習結(jié)束