pushy更新成功.也提示下载成功,但是调用switchVersion没立即重新启动.退出再进入UI也咩有变化.什么原因



  • **switchVersion 有变化,重新启动了.但是UI没刷新.还是以前的UI
    步骤:1,执行react-native bundle --platform android --dev false --entry-file ./index.js --bundle-output ./app/src/main/assets/index.android.bundle --assets-dest ./build/intermedia/android --编译输出index.android.bundle文件到assets.
    2, ./gradlew installRelease --编译运行
    3,pushy uploadApk /Users/binbin/WORK/rntest/android/app/build/outputs/apk/release/app-release.apk --上传apk到pushy.
    4,修改index.js 添加/修改标签 <Text style={styles.welcome}>
    欢迎使用热更新服务!!!
    </Text>
    5,再次执行步骤1(如果不执行index.android.bundle不会输出)
    6, pushy bundle --platform android --生成ppk文件执行上传.

    现象:打开apk应用(非debug模式),点击检查更新,提示更新,已经下载成功,是否需要重新启动,也重新启动了.但是UI没变化.也没崩溃也没报错.
    再次实验多遍也不行.**



  • @zhubinbin1
    package文件:
    {
    "name": "rn_project",
    "version": "0.0.1",
    "private": true,
    "scripts": {
    "start": "yarn react-native start",
    "bundle-android": "react-native bundle --platform android --dev false --entry-file ./index.js --bundle-output ./app/src/main/assets/index.android.bundle --assets-dest ./build/intermedia/android"
    },
    "dependencies": {
    "@babel/core": "*",
    "cli": "^1.0.1",
    "core-js": "3",
    "metro-react-native-babel-transformer": "0.56.0",
    "react": "16.8.6",
    "react-native": "^0.60.0",
    "react-native-update": "^5.3.0"
    }
    }

    index.js 文件:
    import React, {
    Component,
    } from 'react';

    import {
    AppRegistry,
    StyleSheet,
    Platform,
    Text,
    View,
    Alert,
    TouchableOpacity,
    Linking,
    } from 'react-native';

    import {
    isFirstTime,
    isRolledBack,
    packageVersion,
    currentVersion,
    checkUpdate,
    downloadUpdate,
    switchVersion,
    switchVersionLater,
    markSuccess,
    } from 'react-native-update';
    import {name as appName} from './package.json';
    import _updateConfig from './update.json';
    const {appKey} = _updateConfig[Platform.OS];

    class MyProject extends Component {
    componentDidMount(){
    console.log("isFirstTime===:"+isFirstTime + " ===isRolledBack:"+isRolledBack )
    if (isFirstTime) {
    Alert.alert('提示', '这是当前版本第一次启动,是否要模拟启动失败?失败将回滚到上一版本', [
    {text: '是', onPress: ()=>{throw new Error('模拟启动失败,请重启应用')}},
    {text: '否', onPress: ()=>{markSuccess()}},
    ]);
    } else if (isRolledBack) {
    Alert.alert('提示', '刚刚更新失败了,版本被回滚.');
    }
    }
    doUpdate = async (info) => {
    try {
    const hash = await downloadUpdate(info);
    Alert.alert('提示', '下载完毕,是否重启应用?', [
    {text: '是', onPress: ()=>{switchVersion(hash);}},
    {text: '否',},
    {text: '下次启动时', onPress: ()=>{switchVersionLater(hash);}},
    ]);
    } catch(err) {
    Alert.alert('提示', '更新失败.');
    }
    };
    checkUpdate = async () => {
    if (DEV) {
    // 开发模式不支持热更新,跳过检查
    Alert.alert('提示', '测试包,不支持热更新');
    return;
    }
    let info;
    try {
    info = await checkUpdate(appKey);
    } catch (err) {
    console.warn(err);
    return;
    }
    console.log("zhubin===checkUpdate:"+info)
    if (info.expired) {
    Alert.alert('提示', '您的应用版本已更新,请前往应用商店下载新的版本', [
    {text: '确定', onPress: ()=>{info.downloadUrl && Linking.openURL(info.downloadUrl)}},
    ]);
    } else if (info.upToDate) {
    Alert.alert('提示', '您的应用版本已是最新.');
    } else {
    console.log("zhubin===checkUpdate:"+info.update)
    Alert.alert('提示', '检查到新的版本'+info.name+',是否下载?\n'+ info.description, [
    {text: '是', onPress: ()=>{this.doUpdate(info)}},
    {text: '否',},
    ]);
    }
    };
    // showAlert(){
    // Alert.alert('提示', '您的应用版本已是最新.');
    // }
    render() {
    return (
    <View style={styles.container}>
    <Text style={styles.welcome}>
    欢迎使用热更新服务!!!
    </Text>
    <Text style={styles.welcome}>
    欢迎使用热更新服务!!!
    </Text>
    <Text style={styles.instructions}>
    这是版本一 {'\n'}
    当前包版本号: {packageVersion}{'\n'}
    当前版本Hash: {currentVersion||'(空)'}{'\n'}
    </Text>
    <TouchableOpacity onPress={this.checkUpdate}>
    <Text style={styles.instructions}>
    点击这里检查更新
    </Text>
    </TouchableOpacity>
    </View>
    );
    }
    }

    const styles = 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,
    },
    });
    AppRegistry.registerComponent(appName, () => MyProject);

    MainActivity文件:
    class MainActivity() : AppCompatActivity(), DefaultHardwareBackBtnHandler {
    private var mReactRootView: ReactRootView? = null
    var mReactInstanceManager: ReactInstanceManager? = null
    override fun invokeDefaultOnBackPressed() {
    super.onBackPressed()
    }

    private val OVERLAY_PERMISSION_REQ_CODE = 1  // 任写一个值
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    

    // setContentView(R.layout.activity_main)
    // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    // if (!Settings.canDrawOverlays(this)) {
    // val intent = Intent(
    // Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
    // Uri.parse("package:$packageName")
    // )
    // startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE)
    // }
    // }
    permissionCheck()
    mReactRootView = ReactRootView(this)
    mReactInstanceManager = ReactInstanceManager.builder()
    .setApplication(application)
    .setCurrentActivity(this)
    .setBundleAssetName("index.android.bundle")
    .setJSMainModulePath("./index")
    .addPackage(MainReactPackage())
    .addPackage(UpdatePackage())
    .setUseDeveloperSupport(BuildConfig.DEBUG)
    .setInitialLifecycleState(LifecycleState.RESUMED)
    .build()
    // 注意这里的MyReactNativeApp必须对应“index.js”中的
    // “AppRegistry.registerComponent()”的第一个参数
    mReactRootView?.startReactApplication(mReactInstanceManager, MODULE_NAME, null)

        setContentView(mReactRootView)
    }
    

    private fun permissionCheck(){
    val permissionCheck = ContextCompat.checkSelfPermission(this,
    Manifest.permission.WRITE_EXTERNAL_STORAGE)
    if (permissionCheck != PackageManager.PERMISSION_GRANTED){
    ActivityCompat.requestPermissions(this,
    arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
    }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(this)) {
                    // SYSTEM_ALERT_WINDOW permission not granted
                }
            }
        }
    }
    
    override fun onPause() {
        super.onPause()
    
        if (mReactInstanceManager != null) {
            mReactInstanceManager?.onHostPause(this)
        }
    }
    
    override fun onResume() {
        super.onResume()
    
        if (mReactInstanceManager != null) {
            mReactInstanceManager?.onHostResume(this, this)
        }
    }
    
    override fun onDestroy() {
        super.onDestroy()
    
        if (mReactInstanceManager != null) {
            mReactInstanceManager?.onHostDestroy(this)
        }
        if (mReactRootView != null) {
            mReactRootView?.unmountReactApplication()
        }
    }
    
    override fun onBackPressed() {
        if (mReactInstanceManager != null) {
            mReactInstanceManager?.onBackPressed()
        } else {
            super.onBackPressed()
        }
    }
    
    override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
            mReactInstanceManager?.showDevOptionsDialog()
            return true
        }
        return super.onKeyUp(keyCode, event)
    }
    
    companion object {
        const val MODULE_NAME = "rn_project"
    }
    

    }

    application文件:

    class MainApplication : Application(),ReactApplication {

    private val mReactNativeHost = object : ReactNativeHost(this) {
        override fun getPackages(): MutableList<ReactPackage> {
            return packagess
        }
    
        override fun getUseDeveloperSupport(): Boolean {
            return BuildConfig.DEBUG
        }
    
        override fun getJSBundleFile(): String? {
            return UpdateContext.getBundleUrl(this@MainApplication)
        }
    
    }
    private val packagess: MutableList<ReactPackage>
        get() = Arrays.asList(
            MainReactPackage(),
            UpdatePackage()
        )
    
    override fun getReactNativeHost(): ReactNativeHost {
        return mReactNativeHost
    }
    
    override fun onCreate() {
        super.onCreate()
        SoLoader.init(this, /* native exopackage */ false)
    }
    

    }

    工程目录:(在原有项目基础添加rn,node_modules模块和app模块同级目录):
    0_1570760345146_06231243-9dce-4107-8f8f-e8f18709d3c3-image.png

    0_1570760403167_b621713c-9582-4400-86eb-bacb15dde206-image.png

    0_1570760422561_ea89a675-b91e-4d25-bc00-e204c77ed855-image.png

    0_1570760443656_10ec0994-bed5-494b-abfe-e8f5c930aa14-image.png

    0_1570760470615_0b50796f-f57a-4154-bd8f-7e8f82771d55-image.png



  • 版本:
    react-native-cli: 2.0.1
    react-native: 0.60.6
    node:v11.10.1



  • 不要执行react-native bundle命令啊,为啥要执行?



  • @晴明 生成不了index.android.bundle.即使不执行也不行.



  • @晴明 而且首次不执行生成index.android.bundle会崩溃.



  • 崩溃的日志贴一下



  • 我仔细看了下,你这个不是纯rn项目,是嵌入原生是吧,那是需要手动bundle,不过之后生成ppk不需要额外bundle
    能捕捉下switchVersion执行前后的adb logcat吗



  • @晴明 崩溃日志说的是assert没index.android.bundle文件.你qq是多少?
    switchVersion重新启动了,但是UI没更新.也没看到可用的log信息.今天我不是嵌入原生的,直接新建的rn项目更新没问题,但是我运行的是index.android.js.但是更新后运行的是index.js项目的.但是我 protected String getJSMainModuleName() {
    return "index.android";
    }这个回调接口是写的index.android.为啥更新生成ppk的时候打包的是index.js

    0_1570777438980_628f09e8-e1ea-44e1-894c-8da9f65c1c90-image.png



  • @晴明 今天我不是嵌入原生的,直接新建的rn项目更新没问题,但是我运行的是index.android.js.但是更新后运行的是index.js项目的.但是我 protected String getJSMainModuleName() {
    return "index.android";
    }这个回调接口是写的index.android.为啥更新生成ppk的时候打包的是index.js

    说白了就是我首次运行的时候运行的是index.android.js.但是我更新更新成功了,但是运行的是index.js里面的代码/这个是在哪里设置的吗?和嵌入原生的问题是不是同一个问题.


Log in to reply