public static abstract class Component { public final ArrayList intents; public final String className; public Bundle metaData; public Package owner; /** The order of this component in relation to its peers */ public int order; ComponentName componentName; String componentShortName; //其他方法实现 … }
注意对于静态注册在插件AndroidManifest.xml文件中的Receiver,宿主需要知道Receiver的className以及其对应注册的Action,这样在宿主中才能实现广播的中转;因此整个解析过程也就需要解析出广播的ClassName以及其注册的Action就OK了。
对于ClassName在Component已经出现,而对于Action我们则继续看Component类中的intents这个列表,该列表中所存储的对象对应的类集成至IntentInfo,因此我们还需要继续往上看才行。
public static abstract class IntentInfo extends IntentFilter { public boolean hasDefault; // 其他属性以及方法 … }
而该内部类又继承至类IntentFilter,这里IntentFilter则不再是PackageParser类的内部类了,它就是我们在动态注册Reciver所用到的IntentFilter;而在IntentFilter类中我们则能够直接通过其mActions属性拿到当前ClasName对应Receiver所静态注册的Action了。
2、插件中静态注册广播解析实现
接下来就是通过反射的方式调用PackageParser的parsePackage获取到PackageParser$Package对象,接着就是一步一步的对该对象进行解析了。对应源码如下:
public static void parsePackage(String apkPath) { try { //根据插件本地存储的文件地址生成对应的文件 File file = new File(apkPath); if (!file.exists()) { Log.i(TAG, “parse plugin receiver apk not exist”); return; } Log.i(TAG, "parse package path is " + apkPath); //获取到PackageParser类对象 Class cls = Class.forName(“android.content.pm.PackageParser”); Object packageParserObj = RefInvoke.createObject(cls, null, null); if (null == packageParserObj) { Log.i(TAG, “parse package create packageParser object failed”); return; } //调用parsePackage方法 Object packageObj = RefInvoke.on(packageParserObj, “parsePackage”, new Class[]{File.class, int.class}) .invoke(file, PackageManager.GET_RECEIVERS); if (null == packageObj) { Log.i(TAG, “parse package get packageObj failed”); return; } //获取PackageParser$Package对象中的receivers列表 List receivers = (List) RefInvoke.getFieldValue(RefInvoke.getField(packageObj.getClass(), “receivers”), packageObj); if (null == receivers) { Log.i(TAG, “parse package get receivers failed”); return; } //遍历receivers列表获取AndroidManifest.xml文件中所注册的Receiver信息 for (Object receiver : receivers) { parseAction(receiver, apkPath); } } catch (Exception e) { Log.i(TAG, "parse package failed " + e); } }
上面代码就是通过反射的方式调用PackageParser的parsePackage方法,并遍历获取到的receivers列表。接下来就就是对receivers列表中的每个Receiver相关信息进行解析了。
private static void parseAction(Object receiver, String path) { try { //根据反射获取到PackageParserC o m p o n e n t 对象中的 i n t e n t s 列表 C l a s s < ? > c l s = R e f I n v o k e . g e t C l a s s ( " a n d r o i d . c o n t e n t . p m . P a c k a g e P a r s e r Component对象中的intents列表 Class cls = RefInvoke.getClass("android.content.pm.PackageParserComponent对象中的intents列表Class<?>cls=RefInvoke.getClass("android.content.pm.PackageParserComponent"); ArrayList intents = (ArrayList) RefInvoke.getFieldValue(RefInvoke.getField(cls, “intents”), receiver); if (null == intents || 0 == intents.size()) { return; } //获取该Receiver对应的ClassName String clsName = (String) RefInvoke.getFieldValue(RefInvoke.getField(cls, “className”), receiver); Log.i(TAG, "parseAction current receiver name is " + clsName); //根据插件所在文件地址生成对应的ClassLoader并根据获取到的ClassName生成对应的对象 Object receiverObj = creatReceiverObj(clsName, path); if (null == receiverObj) { Log.i(TAG, “parseAction create receiver obj failed”); return; } //接着就是遍历该Receiver中所注册的Action并存储在内存中以便宿主在接收到对应Action的广播之后能够直接进行转发 for (IntentFilter intentFilter : intents) { Class intentFilterCls = RefInvoke.getClass(“android.content.IntentFilter”); Log.i(TAG, "field: " + RefInvoke.getField(intentFilterCls, “mActions”)); List actions = (List) RefInvoke.getFieldValue(RefInvoke.getField(intentFilterCls, “mActions”), intentFilter); for (String action : actions) { registerActionToReceiver(action, receiverObj); } } } catch (Exception e) { Log.i(TAG, "parseAction failed " + e); } } private static Object creatReceiverObj(String clsName, String path) { try { DexClassLoader dexClassLoader = DeHostDexClassloader.getInstance().getDexClassLoader(DePluginApplication.getContext(), path); Class cls = dexClassLoader.loadClass(clsName); return RefInvoke.createObject(cls, null, null); } catch (Exception e) { Log.i(TAG, "createReceiverObj failed " + e.getCause()); } return null; }