rn使用mobx時,debug模式?jīng)]有任何問題,但是release時發(fā)現(xiàn)ios和android都會出現(xiàn)閃退現(xiàn)象。ios比較好弄,android費了一天功夫查閱各種資料最終解決了。下面把解決過程分享一下。
環(huán)境
"react": "16.4.1",
"react-native": "0.56.0",
"mobx": "^5.9.4",
"mobx-react": "5.2.0"
pckage.json
"devDependencies": {
? ? "@babel/core": "7.0.0-beta.47",
? ? "@babel/plugin-proposal-decorators": "7.0.0-beta.47",
? ? "@babel/plugin-transform-runtime": "7.0.0-beta.47",
? ? "@babel/runtime": "7.0.0-beta.47",
? ? "babel-jest": "24.1.0",
? ? "babel-preset-mobx": "^2.0.0",
? ? "babel-preset-react-native": "^5",
? ? "jest": "24.1.0",
? ? "metro-react-native-babel-preset": "^0.53.1",
? ? "react-test-renderer": "16.4.1"
? },
.babelrc
{
? "presets": ["react-native"],
? "plugins": [
????????[ "@babel/plugin-proposal-decorators", {"legacy": true}],
????????[ "@babel/plugin-transform-runtime",
????????{
????????"helpers": true,
? ? ? ? "polyfill": false,
? ? ? ? "regenerator": false
?????????}
? ? ]
]
}
--------------------------------------------------華麗的分割線--------------------------------------------------------
1.ios閃退解決方案
將你的index.js文件挪動到下一級文件夾,比如我的是src
外層index.js像下面這樣
import { AppRegistry } from 'react-native';
import applyDecoratedDescriptor from '@babel/runtime/helpers/es6/applyDecoratedDescriptor';
import initializerDefineProperty from '@babel/runtime/helpers/es6/initializerDefineProperty';
Object.assign(babelHelpers, {
? applyDecoratedDescriptor,
? initializerDefineProperty
});
import App from './src';
AppRegistry.registerComponent('rn', () => App);
2.android閃退解決方案
android-studio下debug發(fā)現(xiàn)報錯
Can not finde Symbol
Module AppRegistry is not a registered callable module
如果遇到這種錯誤,大多因為mobx的版本太高,android本身jscore不支持高版本的mobx,有兩種解決方案,看mobx作者給的方案,https://github.com/mobxjs/mobx/issues/1582#issuecomment-416131218
我采用的是第一種方案,升級android 的jsc,具體過程參照https://fir.im/uxn7?release_id=5c3ea122ca87a81d10588db8,不過值得一提的是,packge.json里的jsc-android的版本必須與
build.gradle里的一致。注意
packge.json
"dependencies": {
.....省略
? ? "axios": "^0.18.0",
? ? "git": "^0.1.5",
? ? "jsc-android": "^236355.1.1",
.....省略
}
configurations.all {resolutionStrategy {force 'org.webkit:android-jsc:r236355'}}