@@ -26,8 +26,14 @@ private PluginCreator() {
26
26
* @return
27
27
*/
28
28
public static DexClassLoader createPluginClassLoader (String absolutePluginApkPath , boolean isStandalone ) {
29
- return new DexClassLoader (absolutePluginApkPath , new File (absolutePluginApkPath ).getParent (), null ,
30
- PluginLoader .class .getClassLoader ());
29
+ if (!isStandalone ) {
30
+ return new DexClassLoader (absolutePluginApkPath , new File (absolutePluginApkPath ).getParent (), null ,
31
+ PluginLoader .class .getClassLoader ());
32
+ } else {
33
+ return new DexClassLoader (absolutePluginApkPath , new File (absolutePluginApkPath ).getParent (), null ,
34
+ PluginLoader .class .getClassLoader ().getParent ());
35
+ }
36
+
31
37
}
32
38
33
39
/**
@@ -44,32 +50,50 @@ public static Resources createPluginResource(Application application, String abs
44
50
AssetManager assetMgr = AssetManager .class .newInstance ();
45
51
Method addAssetPaths = AssetManager .class .getDeclaredMethod ("addAssetPaths" , String [].class );
46
52
47
- String [] assetPaths = new String [2 ];
48
-
49
- //不可更改顺序否则不能兼容4.x
50
- assetPaths [0 ] = application .getApplicationInfo ().sourceDir ;
51
- assetPaths [1 ] = absolutePluginApkPath ;
52
-
53
- if ("vivo" .equalsIgnoreCase (Build .BRAND ) || "oppo" .equalsIgnoreCase (Build .BRAND )) {
54
- //但是!!!如是OPPO或者vivo4.x系统的话 ,要吧这个顺序反过来,否则在混合模式下会找不到资源
55
- assetPaths [0 ] = absolutePluginApkPath ;
56
- assetPaths [1 ] = application .getApplicationInfo ().sourceDir ;
57
- }
58
-
53
+ //如果是独立插件的话,本来是可以不合并主程序资源的。
54
+ //但是由于插件运行时可能会通过getActivityInfo等
55
+ //会拿到到PluginStubActivity的ActivityInfo以及ApplicationInfo
56
+ //这两个info里面有部分资源id是在宿主程序的Manifest中配置的,比如logo和icon
57
+ //尝试通过插件Context获取这些资源会导致异常
58
+ //所以这里强制合并资源。
59
+ //强制合并资源,又需要另外一个前提条件,即id不重复。
60
+ //所以不管是独立插件还是非独立插件,都需要在编译时引入public.xml文件来给资源id分组
61
+ String [] assetPaths = buildAssetPath (false , application .getApplicationInfo ().sourceDir , absolutePluginApkPath );
62
+
59
63
addAssetPaths .invoke (assetMgr , new Object [] { assetPaths });
60
64
61
65
Resources mainRes = application .getResources ();
62
66
Resources pluginRes = new Resources (assetMgr , mainRes .getDisplayMetrics (), mainRes .getConfiguration ());
63
67
64
- LogUtil .d ("create Plugin Resource from: " , assetPaths [0 ], assetPaths [1 ]);
65
-
66
68
return pluginRes ;
67
69
} catch (Exception e ) {
68
70
e .printStackTrace ();
69
71
}
70
72
return null ;
71
73
}
72
74
75
+ private static String [] buildAssetPath (boolean isStandalone , String app , String plugin ) {
76
+ String [] assetPaths = new String [isStandalone ?1 :2 ];
77
+
78
+ if (!isStandalone ) {
79
+ //不可更改顺序否则不能兼容4.x
80
+ assetPaths [0 ] = app ;
81
+ assetPaths [1 ] = plugin ;
82
+ if ("vivo" .equalsIgnoreCase (Build .BRAND ) || "oppo" .equalsIgnoreCase (Build .BRAND ) || "Coolpad" .equalsIgnoreCase (Build .BRAND )) {
83
+ //但是!!!如是OPPO或者vivo4.x系统的话 ,要吧这个顺序反过来,否则在混合模式下会找不到资源
84
+ assetPaths [0 ] = plugin ;
85
+ assetPaths [1 ] = app ;
86
+ }
87
+ LogUtil .d ("create Plugin Resource from: " , assetPaths [0 ], assetPaths [1 ]);
88
+ } else {
89
+ assetPaths [0 ] = plugin ;
90
+ LogUtil .d ("create Plugin Resource from: " , assetPaths [0 ]);
91
+ }
92
+
93
+ return assetPaths ;
94
+
95
+ }
96
+
73
97
/*package*/ static Resources createPluginResourceFor5 (Application application , String absolutePluginApkPath ) {
74
98
try {
75
99
AssetManager assetMgr = AssetManager .class .newInstance ();
0 commit comments