美文网首页
React Native拆包(Android端)

React Native拆包(Android端)

作者: caiwenshu | 来源:发表于2020-02-19 15:11 被阅读0次
    
    
    // 添加容器
    RNStackUtil.single().createContext();
    
    
    
    // 添加业务代码
    RNStackUtil.single().loadBusinessScript();
    
    
    // 执行跳转
    Intent inventoryIntent = new Intent();
    inventoryIntent.putExtra(AppConst.RN_PAGE, "inventory_list");
    inventoryIntent.putExtra(AppConst.RN_PRODUCT, AppConst.RN_PRODUCT_INVENTORY);
    inventoryIntent.setClass(context, InventoryReactActivity.class);
    context.startActivity(inventoryIntent);
    
    

    RNStackUtil 类中代码块

    
      private static ReactNativeHost host;
    
       public static ReactNativeHost getReactNativeHost(Application application) {
    
            if (host == null) {
    
                final String path = CommUtil.getContext().getFilesDir().getAbsolutePath()
                        + File.separator + "ShenmaEV_RN_Inventory" + File.separator + "framework"+ File.separator + "common.jsbundle";
    
                host = new ReactNativeHost(application, "ShenmaEV_RN_Inventory") {
                    @Override
                    public boolean getUseDeveloperSupport() {
                        return CommUtil.isDebug();
                    }
    
                    @Override
                    protected List<ReactPackage> getPackages() {
                        return Arrays.<ReactPackage>asList(
                                new MainReactPackage(),
                                new LinearGradientPackage(),
                                new RNDeviceInfo(),
                                new ContactPackage()
                        );
                    }
    
                    @Override
                    protected String getJSMainModuleName() {
                        return "index";
                    }
    
                    @NonNull
                    @Override
                    protected String getJSBundleFile() {
                        return path;
                    }
    
    //                @Override
    //                protected String getBundleAssetName() {
    //                    return "common_framework_.bundle";
    //                }
    
                };
            }
    
            return host;
        }
    
    
        private String TAG = "RNStackUtil";
        private long time1;
        private long time2;
        private long time3;
    
        public void createContext() {
            Log.d(TAG, "start createContext");
            SoLoader.init(AppContext.get(), /* native exopackage */ false);
            time1 = new Date().getTime();
            final ReactInstanceManager manager = ((ReactApplication) AppContext.get()).getReactNativeHost().getReactInstanceManager();
            if (!manager.hasStartedCreatingInitialContext()) {
                manager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
                    @Override
                    public void onReactContextInitialized(ReactContext context) {
                        time2 = new Date().getTime();
                        onContextInitialized();
                    }
                });
                ((ReactApplication)AppContext.get()).getReactNativeHost().getReactInstanceManager().createReactContextInBackground();
            }
        }
    
        public void unLoad() {
            ((ReactApplication)AppContext.get()).getReactNativeHost().getReactInstanceManager().destroy();
            Toast.makeText(AppContext.get(), "ReactContext已卸载完成~", Toast.LENGTH_LONG).show();
        }
    
        private void onContextInitialized() {
    
            Toast.makeText(AppContext.get(), "ReactContext初始化完毕", Toast.LENGTH_LONG).show();
            Log.e(TAG, "onContextInitialized");
    //        // 方式一,通过反射加载bundle[图片资源可以顺带被加载]
    
    ////        // 方式二,通过JSBundleLoader直接加载[单纯加载bundle,图片资源无法加载]
    //////        loadBundleFromAssetsByJSBundleLoader("asset:///subA_.bundle");
    //////        loadBundleFromAssetsByJSBundleLoader("asset:///subB_.bundle");
    //////        loadBundleFromAssetsByJSBundleLoader("asset:///subC_.bundle");
            time3 = new Date().getTime();
            Log.d(TAG, "onContextInitialized, time1: " + time1 + ", " + time2 + ", " + time3);
        }
    
        public void loadBusinessBundle() {
            loadBundleFromAssets("index_.bundle");
        }
    
        public void loadBundleFromAssets(String bundlepath) {
            String source = "assets://" + bundlepath;
            Log.e("RNN", "wk loadScriptFromAsset:"+source);
            try {
                Method method = CatalystInstanceImpl.class.getDeclaredMethod("loadScriptFromAssets",
                        AssetManager.class,
                        String.class,
                        boolean.class);
                method.setAccessible(true);
                method.invoke(((ReactApplication)AppContext.get()).getReactNativeHost().getReactInstanceManager().getCurrentReactContext().getCatalystInstance(), (AppContext.get()).getAssets(), source, false);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    
    
        //外部地址加载
        public void loadBusinessScript() {
    
            CatalystInstance instance = ((ReactApplication)AppContext.get()).getReactNativeHost().getReactInstanceManager().getCurrentReactContext().getCatalystInstance();
    //        CatalystInstance instance = context.getCatalystInstance();
    
            final String path = CommUtil.getContext().getFilesDir().getAbsolutePath()
                    + File.separator + "ShenmaEV_RN_Inventory" + File.separator + "business" + File.separator + "index_.jsbundle";
    
    //        String path = EffectApplication.getInstance().getExternalCacheDir()+"/business.android.jsbundle";
    //        String path = "";
            try {
                Method method = CatalystInstanceImpl.class
                        .getDeclaredMethod("loadScriptFromFile",
                                String.class,
                                String.class,
                                boolean.class);
                method.setAccessible(true);
                method.invoke(instance, path , path , false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:React Native拆包(Android端)

          本文链接:https://www.haomeiwen.com/subject/zqeafhtx.html