概述
1:该库主要在插件开发过程中通过dependencies { compile 'com.qihoo360.replugin:replugin-plugin-lib:2.2.1' ... }
引入
2:该库主要作用为 Java反射”来调用主程序中 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
来反射调用主程序中 RePlugin Host Library的相关接口。
RePluginInternal.ProxyRePluginInternalVar.initLocked(cl)-----> 初始化为反射调用主程序中 com.qihoo360.i.Factory2及com.qihoo360.i.Factory相关方法
RePlugin.ProxyRePluginVar.initLocked(cl);-----> 初始化为反射调用主程序中 com.qihoo360.replugin.RePlugin相关控制插件install、preload及 activity控制相关方法
PluginLocalBroadcastManager.ProxyLocalBroadcastManagerVar.initLocked(cl)----->
初始化为反射调用主程序中相关控制Broadcast相关方法
PluginProviderClient.ProxyRePluginProviderClientVar.initLocked(cl)----->
初始化为反射调用主程序中 com.qihoo360.loader2.mgr.PluginProviderClient相关控制Provider相关方法
PluginServiceClient.ProxyRePluginServiceClientVar.initLocked(cl);----->
初始化为反射调用主程序中 com.qihoo360.loader2.mgr.PluginServiceClient相关控制service相关方法
IPC.ProxyIPCVar.initLocked(cl);----->初始化为反射调用主程序中com.qihoo360.replugin.base.IPC相关获取储存的IPC信息
3com.qihoo360.replugin.RePluginEnv
/**
* 插件环境相关变量的封装
*
* @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中调用此方法
*/
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对象,可直接调用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;
}
给插件环境赋值基本变量
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个库分工职责 分析学习结束