diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java index 77505ca..b771d55 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java @@ -1,15 +1,22 @@ package cn.reactnative.modules.update; import android.app.Activity; +import android.app.Application; import android.content.Intent; +import android.util.Log; +import com.facebook.react.ReactApplication; +import com.facebook.react.ReactInstanceManager; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.UiThreadUtil; +import com.facebook.react.cxxbridge.JSBundleLoader; +import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; @@ -112,10 +119,32 @@ public class UpdateModule extends ReactContextBaseJavaModule{ @Override public void run() { updateContext.switchVersion(hash); - Activity activity = getCurrentActivity(); - Intent intent = activity.getIntent(); - activity.finish(); - activity.startActivity(intent); + try { + Activity activity = getCurrentActivity(); + Application application = activity.getApplication(); + ReactInstanceManager instanceManager = ((ReactApplication) application).getReactNativeHost().getReactInstanceManager(); + + if (instanceManager.getClass().getSimpleName().equals("XReactInstanceManagerImpl")) { + JSBundleLoader loader = JSBundleLoader.createFileLoader(application, UpdateContext.getBundleUrl(application)); + Field jsBundleField = instanceManager.getClass().getDeclaredField("mBundleLoader"); + jsBundleField.setAccessible(true); + jsBundleField.set(instanceManager, loader); + } else { + Field jsBundleField = instanceManager.getClass().getDeclaredField("mJSBundleFile"); + jsBundleField.setAccessible(true); + jsBundleField.set(instanceManager, UpdateContext.getBundleUrl(application)); + } + + final Method recreateMethod = instanceManager.getClass().getMethod("recreateReactContextInBackground"); + + final ReactInstanceManager finalizedInstanceManager = instanceManager; + + recreateMethod.invoke(finalizedInstanceManager); + + activity.recreate(); + } catch (Throwable err) { + Log.e("pushy", "Failed to restart application", err); + } } }); } diff --git a/docs/guide.md b/docs/guide.md index d2ce1ba..e8f3df4 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -11,22 +11,37 @@ ## 安装 在你的项目根目录下运行以下命令(不要输入开头的美元符号): + +RN 0.29及以上: + +```bash +$ npm install -g react-native-update-cli +$ npm install --save react-native-update +$ react-native link react-native-update +``` + +`npm install -g react-native-update-cli`这一句在每一台电脑上仅需运行一次。 + +RN 0.27-0.28: + ```bash $ npm install -g react-native-update-cli rnpm -$ npm install --save react-native-update +$ npm install --save react-native-update@2.x $ rnpm link react-native-update -``` +``` `npm install -g react-native-update-cli rnpm`这一句在每一台电脑上仅需运行一次。 -* 注意 * - -如果你的react-native版本小于或等于0.26,第二句请修改为 +RN 0.26及以下: ```bash +$ npm install -g react-native-update-cli rnpm $ npm install --save --save-exact react-native-update@1.0.x +$ rnpm link react-native-update ``` +`npm install -g react-native-update-cli rnpm`这一句在每一台电脑上仅需运行一次。 + * 注意 * 如果访问极慢或者显示网络失败,请使用淘宝镜像: @@ -41,7 +56,31 @@ npm config set disturl https://npm.taobao.org/dist --global 2、如果使用VS2015,你需要设置`npm config set msvs_version 2015 --global` +## 手动安装 +如果第一步已成功(iOS工程和安卓工程均能看到依赖),可以跳过此步骤 + +#### iOS + +1. 在XCode中的Project Navigator里,右键点击`Libraries` ➜ `Add Files to [你的工程名]` +2. 进入`node_modules` ➜ `react-native-update` ➜ `ios 并选中 `RCTHotUpdate.xcodeproj` +3. 在XCode中的project navigator里,选中你的工程,在 `Build Phases` ➜ `Link Binary With Libraries` 中添加 `libRCTHotUpdate.a` +4. Run your project (`Cmd+R`)< + +#### Android + +1. 在`android/settings.gradle`中添加如下代码: + ``` + include ':react-native-update' + project(':react-native-update').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-update/android') + ``` +2. 在`android/app/build.gradle`的 dependencies 部分增加如下代码: + ``` + compile project(':react-native-update') + ``` +3. 检查你的RN版本,如果是0.29及以上, 打开`android/app/src/main/java/[...]/MainApplication.java`,否则打开`android/app/src/main/java/[...]/MainActivity.java` + - 在文件开头增加 `import cn.reactnative.modules.update.UpdatePackage;` + - 在`getPackages()` 方法中增加 `new UpdatePackage()`(注意上一行可能要增加一个逗号) ## 配置Bundle URL(iOS) diff --git a/local-cli/src/package.js b/local-cli/src/package.js index fcd06c5..079647e 100644 --- a/local-cli/src/package.js +++ b/local-cli/src/package.js @@ -56,7 +56,7 @@ export const commands = { name, hash, }); - console.log('Ipa uploaded: id'); + console.log(`Ipa uploaded: ${id}`); }, uploadApk: async function({args}) { const fn = args[0]; @@ -72,7 +72,7 @@ export const commands = { name, hash, }); - console.log('Apk uploaded: id'); + console.log(`Apk uploaded: ${id}`); }, packages: async function({options}) { const platform = checkPlatform(options.platform || await question('Platform(ios/android):')); diff --git a/package.json b/package.json index 0d05689..2bbc4d5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-update", - "version": "2.1.0", + "version": "3.0.1", "description": "react-native hot update", "main": "lib/index.js", "scripts": { @@ -38,7 +38,7 @@ "read": "^1.0.7", "request": "^2.69.0", "rimraf": "^2.5.2", - "yauzl": "^2.4.1", + "yauzl": "2.4.1", "yazl": "2.3.0" }, "devDependencies": {