@@ -26,8 +26,14 @@ private PluginCreator() {
2626 * @return
2727 */
2828 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+
3137 }
3238
3339 /**
@@ -44,32 +50,50 @@ public static Resources createPluginResource(Application application, String abs
4450 AssetManager assetMgr = AssetManager .class .newInstance ();
4551 Method addAssetPaths = AssetManager .class .getDeclaredMethod ("addAssetPaths" , String [].class );
4652
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+
5963 addAssetPaths .invoke (assetMgr , new Object [] { assetPaths });
6064
6165 Resources mainRes = application .getResources ();
6266 Resources pluginRes = new Resources (assetMgr , mainRes .getDisplayMetrics (), mainRes .getConfiguration ());
6367
64- LogUtil .d ("create Plugin Resource from: " , assetPaths [0 ], assetPaths [1 ]);
65-
6668 return pluginRes ;
6769 } catch (Exception e ) {
6870 e .printStackTrace ();
6971 }
7072 return null ;
7173 }
7274
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+
7397 /*package*/ static Resources createPluginResourceFor5 (Application application , String absolutePluginApkPath ) {
7498 try {
7599 AssetManager assetMgr = AssetManager .class .newInstance ();
0 commit comments