├── README.md └── reactNativeSplit ├── .babelrc ├── .buckconfig ├── .flowconfig ├── .gitattributes ├── .gitignore ├── .watchmanconfig ├── __tests__ ├── index.android.js └── index.ios.js ├── android ├── app │ ├── BUCK │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── assets │ │ ├── common.bundle │ │ ├── common.bundle.meta │ │ ├── suba.bundle │ │ └── subb.bundle │ │ ├── java │ │ └── com │ │ │ └── reactnativesplit │ │ │ ├── LoadScriptTask.java │ │ │ ├── MainActivity.java │ │ │ ├── MainApplication.java │ │ │ ├── RNUtil.java │ │ │ ├── SubAActivity.java │ │ │ ├── SubBActivity.java │ │ │ ├── SubBundleActivity.java │ │ │ └── SubBundleDelegate.java │ │ └── res │ │ ├── layout │ │ └── main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ └── values │ │ ├── strings.xml │ │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── keystores │ ├── BUCK │ └── debug.keystore.properties └── settings.gradle ├── app.json ├── common ├── common.bundle ├── common.bundle.meta └── common.js ├── index.android.js ├── index.ios.js ├── ios ├── reactNativeSplit-tvOS │ └── Info.plist ├── reactNativeSplit-tvOSTests │ └── Info.plist ├── reactNativeSplit.xcodeproj │ ├── project.pbxproj │ └── xcshareddata │ │ └── xcschemes │ │ ├── reactNativeSplit-tvOS.xcscheme │ │ └── reactNativeSplit.xcscheme ├── reactNativeSplit │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Base.lproj │ │ └── LaunchScreen.xib │ ├── Images.xcassets │ │ └── AppIcon.appiconset │ │ │ └── Contents.json │ ├── Info.plist │ ├── MainViewController.h │ ├── MainViewController.m │ ├── MyDelegate.h │ ├── MyDelegate.m │ ├── RCTBridge.h │ ├── RCTBridge.m │ ├── RCTCxxBridge.mm │ ├── UtilModule.h │ ├── UtilModule.m │ ├── common.bundle │ ├── main.m │ ├── suba.bundle │ └── subb.bundle └── reactNativeSplitTests │ ├── Info.plist │ └── reactNativeSplitTests.m ├── jsconfig.json ├── package.json ├── subA ├── index.android.js ├── suba.bundle └── suba.bundle.meta ├── subB ├── index.android.js ├── subb.bundle └── subb.bundle.meta └── yarn.lock /README.md: -------------------------------------------------------------------------------- 1 | 本demo的ios和android版本都可以直接运行,只需要在reactNativeSplit目录下执行npm init安装好环境即可。 2 | 3 | ios版本需要稍微修改一点源代码,在RCTBridge.h上增加一个loadModule接口。需要修改的三个文件RCTBridge.h RCTBridge.m RCTCxxBridge.mm放在了ios/reactNativeSplit文件夹内。 4 | 其中RCTCxxBridge.mm内的loadModule函数的onComplete接口在不同RN版本上有一点差异,如果报错,修复一下即可。 5 | 6 | android版本因为可以使用反射,所以没有修改任何源代码,直接运行即可。 7 | 8 | 拆分bundle并分步加载的原理其实很简单,在ios和android平台都是一样的,源代码是把创建View和加载bundle耦合在一起,我们把先加载common.bundle并创建好js context,然后保存起来, 9 | 在android平台这个context的管理类是ReactInstanceManager, 在ios平台是RCTBridge。 然后当需要打开界面时,手动创建一个View,在android平台是ReactRootView,在ios平台是RCTRootView,同时 10 | 用保存起来的js context加载业务bundle即可,所以需要把加载bundle的接口从源代码里暴露出来以供我们调用,在ios平台是新建了一个loadModule方法,在android平台则是通过反射执行了CatalystInstanceImpl的loadScriptFromFile方法 11 | 12 | 这个demo只是实现了bundle的分步加载,demo跑起来后还有很多额外的事情需要做,例如bundle如何拆分,运行时可能出现的异常等等。但我们的拆分bundle项目已经在线上稳定运行了半年多时间,基本没什么问题了。 13 | 14 | bundle的拆分方案是这样的:设定一个common.js,对它执行react-native bundle后生成common bundle,业务代码的入口例如index.android.js里最开始require的内容保持和common.js里一致。生成suba.bundle后,把里面common.bundle的内容去掉。 15 | 通过修改react-native/local-cli里的代码,可以给不同业务的moduleID加一个增量,比如suba.bundle就从10000起,subb.bundle从20000起,否则moduleId冲突的话,同时加载多个业务bundle就会出错了。 16 | 17 | 加载业务Bundle时,它们各自的资源查找也会出问题,需要手动修改一下源代码,在加载某个业务Bundle时,调整资源的查找路径。 -------------------------------------------------------------------------------- /reactNativeSplit/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["react-native"] 3 | } 4 | -------------------------------------------------------------------------------- /reactNativeSplit/.buckconfig: -------------------------------------------------------------------------------- 1 | 2 | [android] 3 | target = Google Inc.:Google APIs:23 4 | 5 | [maven_repositories] 6 | central = https://repo1.maven.org/maven2 7 | -------------------------------------------------------------------------------- /reactNativeSplit/.flowconfig: -------------------------------------------------------------------------------- 1 | [ignore] 2 | ; We fork some components by platform 3 | .*/*[.]android.js 4 | 5 | ; Ignore "BUCK" generated dirs 6 | /\.buckd/ 7 | 8 | ; Ignore unexpected extra "@providesModule" 9 | .*/node_modules/.*/node_modules/fbjs/.* 10 | 11 | ; Ignore duplicate module providers 12 | ; For RN Apps installed via npm, "Libraries" folder is inside 13 | ; "node_modules/react-native" but in the source repo it is in the root 14 | .*/Libraries/react-native/React.js 15 | .*/Libraries/react-native/ReactNative.js 16 | 17 | [include] 18 | 19 | [libs] 20 | node_modules/react-native/Libraries/react-native/react-native-interface.js 21 | node_modules/react-native/flow 22 | flow/ 23 | 24 | [options] 25 | emoji=true 26 | 27 | module.system=haste 28 | 29 | munge_underscores=true 30 | 31 | module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub' 32 | 33 | suppress_type=$FlowIssue 34 | suppress_type=$FlowFixMe 35 | suppress_type=$FixMe 36 | 37 | suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(>=0\\.\\(4[0-9]\\|[1-3][0-9]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\) 38 | suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(>=0\\.\\(4[0-9]\\|[1-3][0-9]\\|[0-9]\\).[0-9]\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+ 39 | suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy 40 | suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError 41 | 42 | unsafe.enable_getters_and_setters=true 43 | 44 | [version] 45 | ^0.49.1 46 | -------------------------------------------------------------------------------- /reactNativeSplit/.gitattributes: -------------------------------------------------------------------------------- 1 | *.pbxproj -text 2 | -------------------------------------------------------------------------------- /reactNativeSplit/.gitignore: -------------------------------------------------------------------------------- 1 | # OSX 2 | # 3 | .DS_Store 4 | 5 | # Xcode 6 | # 7 | build/ 8 | *.pbxuser 9 | !default.pbxuser 10 | *.mode1v3 11 | !default.mode1v3 12 | *.mode2v3 13 | !default.mode2v3 14 | *.perspectivev3 15 | !default.perspectivev3 16 | xcuserdata 17 | *.xccheckout 18 | *.moved-aside 19 | DerivedData 20 | *.hmap 21 | *.ipa 22 | *.xcuserstate 23 | project.xcworkspace 24 | 25 | # Android/IntelliJ 26 | # 27 | build/ 28 | .idea 29 | .gradle 30 | local.properties 31 | *.iml 32 | 33 | # node.js 34 | # 35 | !node_modules/ 36 | node_modules/* 37 | !node_modules/react-native/ 38 | npm-debug.log 39 | yarn-error.log 40 | 41 | # BUCK 42 | buck-out/ 43 | \.buckd/ 44 | *.keystore 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots 56 | -------------------------------------------------------------------------------- /reactNativeSplit/.watchmanconfig: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /reactNativeSplit/__tests__/index.android.js: -------------------------------------------------------------------------------- 1 | import 'react-native'; 2 | import React from 'react'; 3 | import Index from '../index.android.js'; 4 | 5 | // Note: test renderer must be required after react-native. 6 | import renderer from 'react-test-renderer'; 7 | 8 | it('renders correctly', () => { 9 | const tree = renderer.create( 10 | 11 | ); 12 | }); 13 | -------------------------------------------------------------------------------- /reactNativeSplit/__tests__/index.ios.js: -------------------------------------------------------------------------------- 1 | import 'react-native'; 2 | import React from 'react'; 3 | import Index from '../index.ios.js'; 4 | 5 | // Note: test renderer must be required after react-native. 6 | import renderer from 'react-test-renderer'; 7 | 8 | it('renders correctly', () => { 9 | const tree = renderer.create( 10 | 11 | ); 12 | }); 13 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/BUCK: -------------------------------------------------------------------------------- 1 | # To learn about Buck see [Docs](https://buckbuild.com/). 2 | # To run your application with Buck: 3 | # - install Buck 4 | # - `npm start` - to start the packager 5 | # - `cd android` 6 | # - `keytool -genkey -v -keystore keystores/debug.keystore -storepass android -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US"` 7 | # - `./gradlew :app:copyDownloadableDepsToLibs` - make all Gradle compile dependencies available to Buck 8 | # - `buck install -r android/app` - compile, install and run application 9 | # 10 | 11 | lib_deps = [] 12 | 13 | for jarfile in glob(['libs/*.jar']): 14 | name = 'jars__' + jarfile[jarfile.rindex('/') + 1: jarfile.rindex('.jar')] 15 | lib_deps.append(':' + name) 16 | prebuilt_jar( 17 | name = name, 18 | binary_jar = jarfile, 19 | ) 20 | 21 | for aarfile in glob(['libs/*.aar']): 22 | name = 'aars__' + aarfile[aarfile.rindex('/') + 1: aarfile.rindex('.aar')] 23 | lib_deps.append(':' + name) 24 | android_prebuilt_aar( 25 | name = name, 26 | aar = aarfile, 27 | ) 28 | 29 | android_library( 30 | name = "all-libs", 31 | exported_deps = lib_deps, 32 | ) 33 | 34 | android_library( 35 | name = "app-code", 36 | srcs = glob([ 37 | "src/main/java/**/*.java", 38 | ]), 39 | deps = [ 40 | ":all-libs", 41 | ":build_config", 42 | ":res", 43 | ], 44 | ) 45 | 46 | android_build_config( 47 | name = "build_config", 48 | package = "com.reactnativesplit", 49 | ) 50 | 51 | android_resource( 52 | name = "res", 53 | package = "com.reactnativesplit", 54 | res = "src/main/res", 55 | ) 56 | 57 | android_binary( 58 | name = "app", 59 | keystore = "//android/keystores:debug", 60 | manifest = "src/main/AndroidManifest.xml", 61 | package_type = "debug", 62 | deps = [ 63 | ":app-code", 64 | ], 65 | ) 66 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: "com.android.application" 2 | 3 | import com.android.build.OutputFile 4 | 5 | /** 6 | * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets 7 | * and bundleReleaseJsAndAssets). 8 | * These basically call `react-native bundle` with the correct arguments during the Android build 9 | * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the 10 | * bundle directly from the development server. Below you can see all the possible configurations 11 | * and their defaults. If you decide to add a configuration block, make sure to add it before the 12 | * `apply from: "../../node_modules/react-native/react.gradle"` line. 13 | * 14 | * project.ext.react = [ 15 | * // the name of the generated asset file containing your JS bundle 16 | * bundleAssetName: "index.android.bundle", 17 | * 18 | * // the entry file for bundle generation 19 | * entryFile: "index.android.js", 20 | * 21 | * // whether to bundle JS and assets in debug mode 22 | * bundleInDebug: false, 23 | * 24 | * // whether to bundle JS and assets in release mode 25 | * bundleInRelease: true, 26 | * 27 | * // whether to bundle JS and assets in another build variant (if configured). 28 | * // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants 29 | * // The configuration property can be in the following formats 30 | * // 'bundleIn${productFlavor}${buildType}' 31 | * // 'bundleIn${buildType}' 32 | * // bundleInFreeDebug: true, 33 | * // bundleInPaidRelease: true, 34 | * // bundleInBeta: true, 35 | * 36 | * // whether to disable dev mode in custom build variants (by default only disabled in release) 37 | * // for example: to disable dev mode in the staging build type (if configured) 38 | * devDisabledInStaging: true, 39 | * // The configuration property can be in the following formats 40 | * // 'devDisabledIn${productFlavor}${buildType}' 41 | * // 'devDisabledIn${buildType}' 42 | * 43 | * // the root of your project, i.e. where "package.json" lives 44 | * root: "../../", 45 | * 46 | * // where to put the JS bundle asset in debug mode 47 | * jsBundleDirDebug: "$buildDir/intermediates/assets/debug", 48 | * 49 | * // where to put the JS bundle asset in release mode 50 | * jsBundleDirRelease: "$buildDir/intermediates/assets/release", 51 | * 52 | * // where to put drawable resources / React Native assets, e.g. the ones you use via 53 | * // require('./image.png')), in debug mode 54 | * resourcesDirDebug: "$buildDir/intermediates/res/merged/debug", 55 | * 56 | * // where to put drawable resources / React Native assets, e.g. the ones you use via 57 | * // require('./image.png')), in release mode 58 | * resourcesDirRelease: "$buildDir/intermediates/res/merged/release", 59 | * 60 | * // by default the gradle tasks are skipped if none of the JS files or assets change; this means 61 | * // that we don't look at files in android/ or ios/ to determine whether the tasks are up to 62 | * // date; if you have any other folders that you want to ignore for performance reasons (gradle 63 | * // indexes the entire tree), add them here. Alternatively, if you have JS files in android/ 64 | * // for example, you might want to remove it from here. 65 | * inputExcludes: ["android/**", "ios/**"], 66 | * 67 | * // override which node gets called and with what additional arguments 68 | * nodeExecutableAndArgs: ["node"], 69 | * 70 | * // supply additional arguments to the packager 71 | * extraPackagerArgs: [] 72 | * ] 73 | */ 74 | 75 | apply from: "../../node_modules/react-native/react.gradle" 76 | 77 | /** 78 | * Set this to true to create two separate APKs instead of one: 79 | * - An APK that only works on ARM devices 80 | * - An APK that only works on x86 devices 81 | * The advantage is the size of the APK is reduced by about 4MB. 82 | * Upload all the APKs to the Play Store and people will download 83 | * the correct one based on the CPU architecture of their device. 84 | */ 85 | def enableSeparateBuildPerCPUArchitecture = false 86 | 87 | /** 88 | * Run Proguard to shrink the Java bytecode in release builds. 89 | */ 90 | def enableProguardInReleaseBuilds = false 91 | 92 | android { 93 | compileSdkVersion 23 94 | buildToolsVersion "23.0.1" 95 | 96 | defaultConfig { 97 | applicationId "com.reactnativesplit" 98 | minSdkVersion 16 99 | targetSdkVersion 22 100 | versionCode 1 101 | versionName "1.0" 102 | ndk { 103 | abiFilters "armeabi-v7a", "x86" 104 | } 105 | } 106 | splits { 107 | abi { 108 | reset() 109 | enable enableSeparateBuildPerCPUArchitecture 110 | universalApk false // If true, also generate a universal APK 111 | include "armeabi-v7a", "x86" 112 | } 113 | } 114 | buildTypes { 115 | release { 116 | minifyEnabled enableProguardInReleaseBuilds 117 | proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" 118 | } 119 | } 120 | // applicationVariants are e.g. debug, release 121 | applicationVariants.all { variant -> 122 | variant.outputs.each { output -> 123 | // For each separate APK per architecture, set a unique version code as described here: 124 | // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits 125 | def versionCodes = ["armeabi-v7a":1, "x86":2] 126 | def abi = output.getFilter(OutputFile.ABI) 127 | if (abi != null) { // null for the universal-debug, universal-release variants 128 | output.versionCodeOverride = 129 | versionCodes.get(abi) * 1048576 + defaultConfig.versionCode 130 | } 131 | } 132 | } 133 | } 134 | 135 | dependencies { 136 | compile fileTree(dir: "libs", include: ["*.jar"]) 137 | compile "com.android.support:appcompat-v7:23.0.1" 138 | compile project(':ReactAndroid') 139 | } 140 | 141 | // Run this once to be able to run the application with BUCK 142 | // puts all compile dependencies into folder libs for BUCK to use 143 | task copyDownloadableDepsToLibs(type: Copy) { 144 | from configurations.compile 145 | into 'libs' 146 | } 147 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /usr/local/Cellar/android-sdk/24.3.3/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Disabling obfuscation is useful if you collect stack traces from production crashes 20 | # (unless you are using a system that supports de-obfuscate the stack traces). 21 | -dontobfuscate 22 | 23 | # React Native 24 | 25 | # Keep our interfaces so they can be used by other ProGuard rules. 26 | # See http://sourceforge.net/p/proguard/bugs/466/ 27 | -keep,allowobfuscation @interface com.facebook.proguard.annotations.DoNotStrip 28 | -keep,allowobfuscation @interface com.facebook.proguard.annotations.KeepGettersAndSetters 29 | -keep,allowobfuscation @interface com.facebook.common.internal.DoNotStrip 30 | 31 | # Do not strip any method/class that is annotated with @DoNotStrip 32 | -keep @com.facebook.proguard.annotations.DoNotStrip class * 33 | -keep @com.facebook.common.internal.DoNotStrip class * 34 | -keepclassmembers class * { 35 | @com.facebook.proguard.annotations.DoNotStrip *; 36 | @com.facebook.common.internal.DoNotStrip *; 37 | } 38 | 39 | -keepclassmembers @com.facebook.proguard.annotations.KeepGettersAndSetters class * { 40 | void set*(***); 41 | *** get*(); 42 | } 43 | 44 | -keep class * extends com.facebook.react.bridge.JavaScriptModule { *; } 45 | -keep class * extends com.facebook.react.bridge.NativeModule { *; } 46 | -keepclassmembers,includedescriptorclasses class * { native ; } 47 | -keepclassmembers class * { @com.facebook.react.uimanager.UIProp ; } 48 | -keepclassmembers class * { @com.facebook.react.uimanager.annotations.ReactProp ; } 49 | -keepclassmembers class * { @com.facebook.react.uimanager.annotations.ReactPropGroup ; } 50 | 51 | -dontwarn com.facebook.react.** 52 | 53 | # TextLayoutBuilder uses a non-public Android constructor within StaticLayout. 54 | # See libs/proxy/src/main/java/com/facebook/fbui/textlayoutbuilder/proxy for details. 55 | -dontwarn android.text.StaticLayout 56 | 57 | # okhttp 58 | 59 | -keepattributes Signature 60 | -keepattributes *Annotation* 61 | -keep class okhttp3.** { *; } 62 | -keep interface okhttp3.** { *; } 63 | -dontwarn okhttp3.** 64 | 65 | # okio 66 | 67 | -keep class sun.misc.Unsafe { *; } 68 | -dontwarn java.nio.file.* 69 | -dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement 70 | -dontwarn okio.** 71 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 7 | 8 | 9 | 12 | 13 | 19 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/assets/common.bundle.meta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangguang1029/ReactNativeSplit/3355c890aefd9e319871536f19dadf90bf05f31a/reactNativeSplit/android/app/src/main/assets/common.bundle.meta -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/assets/suba.bundle: -------------------------------------------------------------------------------- 1 | 2 | __d(function(e,t,r,n){Object.defineProperty(n,"__esModule",{value:!0});var l=t(12),o=babelHelpers.interopRequireDefault(l),i=t(26),a=function(e){function t(){return babelHelpers.classCallCheck(this,t),babelHelpers.possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return babelHelpers.inherits(t,e),babelHelpers.createClass(t,[{key:"render",value:function(){return o.default.createElement(i.View,{style:c.container},o.default.createElement(i.Text,{style:c.welcome},"Welcome to Bundle A"))}}]),t}(l.Component);n.default=a;var c=i.StyleSheet.create({container:{flex:1,justifyContent:"center",alignItems:"center",backgroundColor:"#F5FCFF"},welcome:{fontSize:20,textAlign:"center",margin:10},instructions:{textAlign:"center",color:"#333333",marginBottom:5}});i.AppRegistry.registerComponent("suba",function(){return a})},10000); 3 | console.log("fuck yangg suba bundle"); 4 | ;require(57); 5 | ;require(10000); -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/assets/subb.bundle: -------------------------------------------------------------------------------- 1 | __d(function(e,t,r,n){Object.defineProperty(n,"__esModule",{value:!0});var l=t(12),o=babelHelpers.interopRequireDefault(l),i=t(26),a=function(e){function t(){return babelHelpers.classCallCheck(this,t),babelHelpers.possibleConstructorReturn(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return babelHelpers.inherits(t,e),babelHelpers.createClass(t,[{key:"render",value:function(){return o.default.createElement(i.View,{style:c.container},o.default.createElement(i.Text,{style:c.welcome},"Welcome to Bundle B"))}}]),t}(l.Component);n.default=a;var c=i.StyleSheet.create({container:{flex:1,justifyContent:"center",alignItems:"center",backgroundColor:"#F5FCFF"},welcome:{fontSize:20,textAlign:"center",margin:10},instructions:{textAlign:"center",color:"#333333",marginBottom:5}});i.AppRegistry.registerComponent("subb",function(){return a})},20000); 2 | 3 | ;require(57); 4 | ;require(20000); -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/java/com/reactnativesplit/LoadScriptTask.java: -------------------------------------------------------------------------------- 1 | package com.reactnativesplit; 2 | 3 | import android.content.Context; 4 | import android.os.AsyncTask; 5 | 6 | import com.facebook.react.ReactApplication; 7 | import com.facebook.react.bridge.Callback; 8 | import com.facebook.react.bridge.CatalystInstance; 9 | 10 | /** 11 | * Created by yangguang01 on 2017/8/16. 12 | */ 13 | 14 | public class LoadScriptTask extends AsyncTask { 15 | 16 | private Callback mcallback = null; 17 | private ReactApplication mApplication; 18 | private String mAssetPath; 19 | private String mComponentName; 20 | private boolean mIsUpdate = false; 21 | 22 | public LoadScriptTask(ReactApplication application, String assetPath, 23 | String componentName, 24 | Callback cb) { 25 | mcallback = cb; 26 | mApplication = application; 27 | mAssetPath = assetPath; 28 | mComponentName = componentName; 29 | } 30 | 31 | @Override 32 | protected Void doInBackground(Void... params) { 33 | CatalystInstance instance = RNUtil.getCatalystInstance(mApplication.getReactNativeHost()); 34 | Context context = (Context)mApplication; 35 | RNUtil.loadScript(context, 36 | instance, 37 | mAssetPath, 38 | mComponentName); 39 | return null; 40 | } 41 | 42 | @Override 43 | protected void onPostExecute(Void aVoid) { 44 | if(mcallback != null){ 45 | mcallback.invoke(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/java/com/reactnativesplit/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.reactnativesplit; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.view.View; 8 | import android.widget.Button; 9 | 10 | import com.facebook.react.ReactActivity; 11 | import com.facebook.react.ReactApplication; 12 | import com.facebook.react.ReactInstanceManager; 13 | import com.facebook.react.ReactNativeHost; 14 | import com.facebook.react.bridge.ReactContext; 15 | import com.facebook.react.bridge.UiThreadUtil; 16 | 17 | public class MainActivity extends Activity { 18 | 19 | private final String TAG = "MainActivity"; 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState){ 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.main); 24 | 25 | createContext(); 26 | 27 | Button b1 = (Button)findViewById(R.id.button1); 28 | b1.setOnClickListener(new Button.OnClickListener() { 29 | @Override 30 | public void onClick(View v) { 31 | startActivity(new Intent(v.getContext(), SubAActivity.class)); 32 | } 33 | }); 34 | 35 | Button b2 = (Button)findViewById(R.id.button2); 36 | b2.setOnClickListener(new Button.OnClickListener() { 37 | @Override 38 | public void onClick(View v) { 39 | startActivity(new Intent(v.getContext(), SubBActivity.class)); 40 | } 41 | }); 42 | } 43 | 44 | private ReactNativeHost getReactNativeHost() { 45 | return ((ReactApplication) getApplication()).getReactNativeHost(); 46 | } 47 | 48 | private void createContext(){ 49 | Log.e(TAG, "yangg start createContext"); 50 | final ReactInstanceManager manager = getReactNativeHost().getReactInstanceManager(); 51 | if (!manager.hasStartedCreatingInitialContext()) { 52 | manager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() { 53 | @Override 54 | public void onReactContextInitialized(ReactContext context) { 55 | MainActivity.this.onContextInitialized(); 56 | } 57 | }); 58 | manager.createReactContextInBackground(); 59 | } 60 | } 61 | private void onContextInitialized(){ 62 | Log.e(TAG, "yangg onContextInitialized"); 63 | UiThreadUtil.runOnUiThread(new Runnable() { 64 | @Override 65 | public void run() { 66 | final ReactInstanceManager manager = getReactNativeHost().getReactInstanceManager(); 67 | manager.onHostResume(MainActivity.this, null); 68 | } 69 | }); 70 | 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/java/com/reactnativesplit/MainApplication.java: -------------------------------------------------------------------------------- 1 | package com.reactnativesplit; 2 | 3 | import android.app.Application; 4 | import android.support.annotation.Nullable; 5 | import android.util.Log; 6 | 7 | import com.facebook.react.ReactApplication; 8 | import com.facebook.react.ReactInstanceManager; 9 | import com.facebook.react.ReactNativeHost; 10 | import com.facebook.react.ReactPackage; 11 | import com.facebook.react.bridge.ReactContext; 12 | import com.facebook.react.shell.MainReactPackage; 13 | import com.facebook.soloader.SoLoader; 14 | 15 | import java.util.Arrays; 16 | import java.util.List; 17 | 18 | public class MainApplication extends Application implements ReactApplication { 19 | 20 | private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { 21 | @Override 22 | public boolean getUseDeveloperSupport() { 23 | return BuildConfig.DEBUG; 24 | } 25 | 26 | @Override 27 | protected List getPackages() { 28 | return Arrays.asList( 29 | new MainReactPackage() 30 | ); 31 | } 32 | 33 | @Nullable 34 | @Override 35 | protected String getBundleAssetName() { 36 | return "common.bundle"; 37 | } 38 | }; 39 | 40 | @Override 41 | public ReactNativeHost getReactNativeHost() { 42 | return mReactNativeHost; 43 | } 44 | 45 | @Override 46 | public void onCreate() { 47 | super.onCreate(); 48 | SoLoader.init(this, /* native exopackage */ false); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/java/com/reactnativesplit/RNUtil.java: -------------------------------------------------------------------------------- 1 | package com.reactnativesplit; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.res.AssetManager; 6 | import android.os.AsyncTask; 7 | import android.os.Bundle; 8 | import android.support.annotation.Nullable; 9 | import android.support.annotation.WorkerThread; 10 | import android.util.Log; 11 | 12 | import com.facebook.react.ReactActivity; 13 | import com.facebook.react.ReactActivityDelegate; 14 | import com.facebook.react.ReactApplication; 15 | import com.facebook.react.ReactInstanceManager; 16 | import com.facebook.react.ReactNativeHost; 17 | import com.facebook.react.ReactRootView; 18 | import com.facebook.react.bridge.Callback; 19 | import com.facebook.react.bridge.CatalystInstance; 20 | import com.facebook.react.bridge.ReactContext; 21 | import com.facebook.react.bridge.CatalystInstanceImpl; 22 | 23 | import java.io.File; 24 | import java.lang.reflect.Field; 25 | import java.lang.reflect.InvocationTargetException; 26 | import java.lang.reflect.Method; 27 | import java.util.HashSet; 28 | import java.util.Set; 29 | 30 | /** 31 | * Created by yangguang01 on 2017/7/21. 32 | */ 33 | 34 | public class RNUtil { 35 | private static Set sLoadedScript = new HashSet<>(); 36 | 37 | public static ReactActivityDelegate getDelegate(ReactActivity activity) { 38 | try { 39 | Field field = ReactActivity.class.getDeclaredField("mDelegate"); 40 | field.setAccessible(true); 41 | return (ReactActivityDelegate) field.get(activity); 42 | } catch (NoSuchFieldException e) { 43 | e.printStackTrace(); 44 | } catch (IllegalAccessException e) { 45 | e.printStackTrace(); 46 | } 47 | return null; 48 | } 49 | 50 | public static Activity getActivity(ReactActivityDelegate delegate) { 51 | try { 52 | Field field = ReactActivityDelegate.class.getDeclaredField("mActivity"); 53 | field.setAccessible(true); 54 | return (Activity) field.get(delegate); 55 | } catch (NoSuchFieldException e) { 56 | e.printStackTrace(); 57 | } catch (IllegalAccessException e) { 58 | e.printStackTrace(); 59 | } 60 | return null; 61 | } 62 | 63 | public static ReactRootView getRootView(ReactActivityDelegate delegate) { 64 | try { 65 | Field field = ReactActivityDelegate.class.getDeclaredField("mReactRootView"); 66 | field.setAccessible(true); 67 | return (ReactRootView) field.get(delegate); 68 | } catch (NoSuchFieldException e) { 69 | e.printStackTrace(); 70 | } catch (IllegalAccessException e) { 71 | e.printStackTrace(); 72 | } 73 | return null; 74 | } 75 | 76 | public static void setRootView(ReactActivityDelegate delegate, ReactRootView rootView) { 77 | try { 78 | Field field = ReactActivityDelegate.class.getDeclaredField("mReactRootView"); 79 | field.setAccessible(true); 80 | field.set(delegate, rootView); 81 | } catch (NoSuchFieldException e) { 82 | e.printStackTrace(); 83 | } catch (IllegalAccessException e) { 84 | e.printStackTrace(); 85 | } 86 | } 87 | 88 | public static void setJsModuleName(ReactRootView rootView, String moduleName) { 89 | try { 90 | Field field = ReactRootView.class.getDeclaredField("mJSModuleName"); 91 | field.setAccessible(true); 92 | field.set(rootView, moduleName); 93 | } catch (NoSuchFieldException e) { 94 | e.printStackTrace(); 95 | } catch (IllegalAccessException e) { 96 | e.printStackTrace(); 97 | } 98 | } 99 | 100 | public static void setLaunchOptions(ReactRootView rootView, Bundle options) { 101 | // try { 102 | // Field field = ReactRootView.class.getDeclaredField("mLaunchOptions"); 103 | // field.setAccessible(true); 104 | // field.set(rootView, options); 105 | // } catch (NoSuchFieldException e) { 106 | // e.printStackTrace(); 107 | // } catch (IllegalAccessException e) { 108 | // e.printStackTrace(); 109 | // } 110 | } 111 | 112 | public static void setViewAttached(ReactRootView rootView, boolean bAttached) { 113 | try { 114 | Field field = ReactRootView.class.getDeclaredField("mIsAttachedToInstance"); 115 | field.setAccessible(true); 116 | field.set(rootView, bAttached); 117 | } catch (NoSuchFieldException e) { 118 | e.printStackTrace(); 119 | } catch (IllegalAccessException e) { 120 | e.printStackTrace(); 121 | } 122 | } 123 | 124 | public static void setReactInstanceManager(ReactRootView rootView, ReactInstanceManager manager) { 125 | try { 126 | Field field = ReactRootView.class.getDeclaredField("mReactInstanceManager"); 127 | field.setAccessible(true); 128 | field.set(rootView, manager); 129 | } catch (NoSuchFieldException e) { 130 | e.printStackTrace(); 131 | } catch (IllegalAccessException e) { 132 | e.printStackTrace(); 133 | } 134 | } 135 | 136 | @Nullable 137 | public static CatalystInstance getCatalystInstance(ReactNativeHost host) { 138 | ReactInstanceManager manager = host.getReactInstanceManager(); 139 | if (manager == null) { 140 | Log.e("RNUtils", " manager is null"); 141 | return null; 142 | } 143 | 144 | ReactContext context = manager.getCurrentReactContext(); 145 | if (context == null) { 146 | Log.e("RNUtils", " context is null"); 147 | return null; 148 | } 149 | return context.getCatalystInstance(); 150 | } 151 | 152 | public static void clearLoadedScript(String bundlePath) { 153 | sLoadedScript.remove(bundlePath); 154 | } 155 | 156 | public static void loadScriptInBackground(ReactApplication application, String bundlepath, String productName, Callback cb){ 157 | Log.e("RNN", " loadScriptInBackground bundlepath:"+bundlepath); 158 | LoadScriptTask task = new LoadScriptTask(application, bundlepath, productName, cb); 159 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 160 | } 161 | 162 | public static void loadScript(Context context, 163 | CatalystInstance instance, 164 | String bundlepath, 165 | String productname){ 166 | 167 | Log.e("RNN", "yangg bundlepath:"+bundlepath); 168 | loadScriptFromAsset(context,instance,bundlepath); 169 | } 170 | 171 | @WorkerThread 172 | private static void loadScriptFromAsset(Context context, 173 | CatalystInstance instance, 174 | String bundlepath) { 175 | if (sLoadedScript.contains(bundlepath)) { 176 | Log.e("RNN", "yangg bundle already loaded return: "+bundlepath); 177 | return; 178 | } 179 | 180 | String source = "assets://" + bundlepath; 181 | Log.e("RNN", "yangg loadScriptFromAsset:"+source); 182 | try { 183 | Method method = CatalystInstanceImpl.class.getDeclaredMethod("loadScriptFromAssets", 184 | AssetManager.class, 185 | String.class, 186 | boolean.class); 187 | method.setAccessible(true); 188 | method.invoke(instance, context.getAssets(), source, false); 189 | sLoadedScript.add(bundlepath); 190 | } catch (IllegalAccessException e) { 191 | e.printStackTrace(); 192 | } catch (NoSuchMethodException e) { 193 | e.printStackTrace(); 194 | } catch (InvocationTargetException e) { 195 | e.printStackTrace(); 196 | } 197 | } 198 | @WorkerThread 199 | private static void loadScriptFromFile(Context context, 200 | CatalystInstance instance, 201 | String bundlepath) { 202 | if (sLoadedScript.contains(bundlepath)) { 203 | Log.e("RNN", "yangg bundle already loaded return: "+bundlepath); 204 | return; 205 | } 206 | Log.e("RNN", "yangg loadScriptFromFile:"+bundlepath); 207 | try { 208 | Method method = CatalystInstanceImpl.class.getDeclaredMethod("loadScriptFromFile", 209 | String.class, 210 | String.class, 211 | boolean.class); 212 | method.setAccessible(true); 213 | method.invoke(instance, bundlepath, bundlepath, false); 214 | sLoadedScript.add(bundlepath); 215 | } catch (IllegalAccessException e) { 216 | e.printStackTrace(); 217 | } catch (NoSuchMethodException e) { 218 | e.printStackTrace(); 219 | } catch (InvocationTargetException e) { 220 | e.printStackTrace(); 221 | } 222 | } 223 | } 224 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/java/com/reactnativesplit/SubAActivity.java: -------------------------------------------------------------------------------- 1 | package com.reactnativesplit; 2 | 3 | /** 4 | * Created by yangguang01 on 2017/9/28. 5 | */ 6 | 7 | public class SubAActivity extends SubBundleActivity{ 8 | 9 | 10 | @Override 11 | protected String getMainComponentName() { 12 | return "suba"; 13 | } 14 | 15 | @Override 16 | protected String getScriptPath() { 17 | return "suba.bundle"; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/java/com/reactnativesplit/SubBActivity.java: -------------------------------------------------------------------------------- 1 | package com.reactnativesplit; 2 | 3 | /** 4 | * Created by yangguang01 on 2017/9/28. 5 | */ 6 | 7 | public class SubBActivity extends SubBundleActivity{ 8 | 9 | 10 | @Override 11 | protected String getMainComponentName() { 12 | return "subb"; 13 | } 14 | 15 | @Override 16 | protected String getScriptPath() { 17 | return "subb.bundle"; 18 | } 19 | } -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/java/com/reactnativesplit/SubBundleActivity.java: -------------------------------------------------------------------------------- 1 | package com.reactnativesplit; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.IntentFilter; 7 | import android.os.Bundle; 8 | import android.support.annotation.Nullable; 9 | import android.util.Log; 10 | 11 | import com.facebook.react.ReactActivity; 12 | import com.facebook.react.ReactActivityDelegate; 13 | import com.facebook.react.ReactRootView; 14 | import com.reactnativesplit.RNUtil; 15 | 16 | /** 17 | * Created by yangguang01 on 2017/8/11. 18 | */ 19 | 20 | public abstract class SubBundleActivity extends ReactActivity { 21 | private BroadcastReceiver mReceiver; 22 | 23 | private final String TAG ="SubBundleActivity"; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | Log.e(TAG, "yangg onCreate"); 28 | super.onCreate(savedInstanceState); 29 | intiReceiver(); 30 | } 31 | 32 | private void intiReceiver(){ 33 | mReceiver = new BroadcastReceiver() { 34 | @Override 35 | public void onReceive(Context context, Intent intent) { 36 | String action = intent.getAction(); 37 | String name = intent.getStringExtra("name"); 38 | if(action.equals("reload")){ 39 | recreateContext(name); 40 | } 41 | } 42 | }; 43 | IntentFilter filter = new IntentFilter(); 44 | filter.addAction("reload"); 45 | this.registerReceiver(mReceiver, filter); 46 | } 47 | 48 | public void recreateContext(String name){ 49 | if(name != null && !getMainComponentName().toLowerCase().equals(name)) { 50 | return; 51 | } 52 | SubBundleDelegate delegate = (SubBundleDelegate)RNUtil.getDelegate(this); 53 | delegate.recreateContext(); 54 | } 55 | 56 | @Override 57 | protected ReactActivityDelegate createReactActivityDelegate() { 58 | return new SubBundleDelegate(this, getMainComponentName()); 59 | } 60 | 61 | 62 | protected @Nullable 63 | ReactRootView.ReactRootViewEventListener getEventListener(){return null;} 64 | 65 | public void onScriptsLoaded(){ 66 | Log.e(TAG, "yangg onScriptsLoaded"); 67 | } 68 | 69 | @Override 70 | protected void onDestroy() { 71 | if (mReceiver != null) { 72 | this.unregisterReceiver(mReceiver); 73 | mReceiver = null; 74 | } 75 | super.onDestroy(); 76 | } 77 | 78 | protected abstract String getMainComponentName(); 79 | protected abstract String getScriptPath(); 80 | 81 | } 82 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/java/com/reactnativesplit/SubBundleDelegate.java: -------------------------------------------------------------------------------- 1 | package com.reactnativesplit; 2 | 3 | import android.app.Activity; 4 | import android.os.AsyncTask; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.view.ViewGroup; 8 | 9 | import com.facebook.react.ReactActivityDelegate; 10 | import com.facebook.react.ReactApplication; 11 | import com.facebook.react.ReactInstanceManager; 12 | import com.facebook.react.ReactRootView; 13 | import com.facebook.react.bridge.Callback; 14 | import com.facebook.react.bridge.CatalystInstance; 15 | import com.facebook.react.bridge.ReactContext; 16 | import com.facebook.react.common.LifecycleState; 17 | import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; 18 | import com.reactnativesplit.RNUtil; 19 | 20 | import javax.annotation.Nullable; 21 | 22 | /** 23 | * Created by yangguang01 on 2017/8/11. 24 | */ 25 | 26 | public class SubBundleDelegate extends ReactActivityDelegate { 27 | 28 | private String mParams = null; 29 | 30 | private final String TAG = "SubBundleDelegate"; 31 | 32 | public String getParams() { 33 | return mParams; 34 | } 35 | 36 | public void setParams(String params) { 37 | this.mParams = params; 38 | } 39 | 40 | private SubBundleActivity getActivity(){ 41 | return (SubBundleActivity)RNUtil.getActivity(this); 42 | } 43 | 44 | public SubBundleDelegate(Activity activity, @Nullable String mainComponentName) { 45 | super(activity, mainComponentName); 46 | } 47 | 48 | private String getMainComponentName(){ 49 | return getActivity().getMainComponentName(); 50 | } 51 | 52 | private String getScriptPath(){ 53 | return getActivity().getScriptPath(); 54 | } 55 | 56 | @Nullable 57 | @Override 58 | protected Bundle getLaunchOptions() { 59 | if(mParams != null) { 60 | Bundle bundle =new Bundle(); 61 | bundle.putString("params",mParams); 62 | return bundle; 63 | } 64 | return null; 65 | } 66 | 67 | public void recreateContext(){ 68 | Log.e(TAG, "yangg recreateContext"); 69 | ReactRootView view = RNUtil.getRootView(this); 70 | view.unmountReactApplication(); 71 | ReactInstanceManager manager = getReactNativeHost().getReactInstanceManager(); 72 | manager.detachRootView(view); 73 | ViewGroup vg = (ViewGroup)(view.getParent()); 74 | vg.removeView(view); 75 | 76 | RNUtil.clearLoadedScript(getScriptPath()); 77 | loadApp(getMainComponentName()); 78 | } 79 | 80 | @Override 81 | protected void loadApp(String appKey) { 82 | Log.e(TAG, "yangg loadApp"); 83 | ReactRootView view = createRootView(); 84 | RNUtil.setRootView(this, view); 85 | ReactInstanceManager manager = getReactNativeHost().getReactInstanceManager(); 86 | RNUtil.setReactInstanceManager(view, manager); 87 | RNUtil.setJsModuleName(view, getMainComponentName()); 88 | view.setEventListener(getActivity().getEventListener()); 89 | RNUtil.setLaunchOptions(view, getLaunchOptions()); 90 | getActivity().setContentView(view); 91 | 92 | if (manager.getCurrentReactContext() == null) { 93 | 94 | if(manager.hasStartedCreatingInitialContext()){ //既然已经开始加载common了,就坐等加载完 95 | Log.e(TAG, "yangg loadApp hasStartedCreatingInitialContext"); 96 | }else{ 97 | Log.e(TAG, "yangg loadApp will create"); 98 | manager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() { 99 | @Override 100 | public void onReactContextInitialized(ReactContext reactContext) { 101 | loadScriptAsync(); 102 | } 103 | }); 104 | manager.createReactContextInBackground(); 105 | } 106 | }else { 107 | Log.e(TAG, "yangg loadApp current context not null"); 108 | loadScriptAsync(); 109 | } 110 | } 111 | 112 | private void loadScriptAsync() { 113 | Log.e(TAG, "yangg loadScriptAsync"); 114 | ReactRootView view = RNUtil.getRootView(this); 115 | RNUtil.setViewAttached(view, true); 116 | ReactInstanceManager manager = getReactNativeHost().getReactInstanceManager(); 117 | SubBundleActivity activity = getActivity(); 118 | manager.onHostResume(activity, (DefaultHardwareBackBtnHandler)activity); 119 | Callback cb = new Callback() { 120 | @Override 121 | public void invoke(Object... args) { 122 | SubBundleDelegate.this.onScriptLoaded(); 123 | } 124 | }; 125 | ReactApplication app = (ReactApplication) activity.getApplication(); 126 | RNUtil.loadScriptInBackground(app, getScriptPath(), getMainComponentName(), cb); 127 | } 128 | 129 | private void onScriptLoaded(){ 130 | Log.e(TAG, "yangg onScriptLoaded"); 131 | ReactRootView view = RNUtil.getRootView(this); 132 | ReactInstanceManager manager = getReactNativeHost().getReactInstanceManager(); 133 | manager.attachRootView(view); 134 | getActivity().onScriptsLoaded(); 135 | } 136 | 137 | @Override 138 | protected void onDestroy() { 139 | ReactRootView view = RNUtil.getRootView(this); 140 | if (view != null) { 141 | view.unmountReactApplication(); 142 | RNUtil.setRootView(this, null); 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /reactNativeSplit/android/app/src/main/res/layout/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 |