美文网首页
【坑】React-Native android 开发踩坑记

【坑】React-Native android 开发踩坑记

作者: qhrosl | 来源:发表于2022-03-11 11:20 被阅读0次

    <meta charset="utf-8">

    本文采用的是

    react-native init *project*
    
    

    然后

    react-native run-android
    
    

    以运行项目。
    教程原文

    1.java.io.IOException: Unable to delete directory...

    这是由于缓存导致
    【解决方法】

    cd android
    
    gradlew clean
    
    

    参考原文

    2.RN项目总是有缓存,如何清理之

    可以直接运行

    yarn --reset-cache
    
    

    3.emulator-5554 cannot be found

    运行

    adb kill-serrver
    adb start-server
    
    

    如果仍然找不到,你需要在任务管理器里面杀掉一些占用pid的进程

    4.在RN项目中使用 echarts

    推荐使用 native-charts

    目前你可以在网上搜索到的解决方案大多针对 ios(直接上手使用即可),对于android,你需要略作修改。
    依赖:

    因为 WebView 控件即将于 react-native core 中移除,所以我们需要安装其插件。

    要在安卓环境下使用 echarts,你需要以下动作:
    1.将 node_modules\native-echarts\src\components\Echarts 下 tpl.html 移动至
    android/app/src/main/assets 目录下。
    2.更改 node_modules\native-echarts\src\components\Echarts 下 index.js 的代码:

    import React, { Component } from 'react';
    import { View, StyleSheet, Platform } from 'react-native';
    
    //注意在这里如此修改,可以有效解决黄页报警以及图标加载慢的问题
    import { WebView } from 'react-native-webview';
    
    import renderChart from './renderChart';
    import echarts from './echarts.min';
    
    //加这么一句话以判断你的运行平台是否为 ios,或是 android
    const iosPlatform = Platform.OS === "ios"?true:false;
    
    export default class App extends Component {
    
      constructor(props) {
        super(props);
        this.setNewOption = this.setNewOption.bind(this);
      }
    
      componentWillReceiveProps(nextProps) {
        if(nextProps.option !== this.props.option) {
          this.refs.chart.reload();
        }
      }
    
      setNewOption(option) {
        this.refs.chart.postMessage(JSON.stringify(option));
      }
    
      render() {
        return (
          <View style={{flex: 1, height: this.props.height || 400,}}>
            <WebView
              ref="chart"
              scrollEnabled = {false}
              injectedJavaScript = {renderChart(this.props)}
              style={{
                height: this.props.height || 400,
                backgroundColor: this.props.backgroundColor || 'transparent'
              }}
              scalesPageToFit={Platform.OS !== 'ios'}
              originWhitelist={['*']}
              source={iosPlatform?require('./tpl.html'):{uri:"file:///android_asset/tpl.html"}}    //android 平台的静态文件必须放置在指定目录且不能使用 require() 
              onMessage={event => this.props.onPress ? this.props.onPress(JSON.parse(event.nativeEvent.data)) : null}
            />
          </View>
        );
      }
    }
    
    

    然后你就可以愉快地按照教程示例照葫芦画瓢了。

    5. ReactNative运行报错 Command run-android unrecognized. Make sure that you have run npm install and that you are inside a react-native project.

    发生这种错误可能是因为你在代码迁移时,node_modules 内的文件发生变化,如果你运行 npm install 之后依然报此错误,可能是因为 package.json 发生变化,请确保其内容然后运行 npm install 。之后便可正常运行 react-native run-android

    6.Execution failed for task ':app:packageDebug'. > Java heap space

    发生这种错误可能是由于你打包的文件过大(一说不得大于350M)

    7.Unable to install ---- app-debug.apk

    com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException: INSTALL_FAILED_UPDATE_INCOMPATIBLE

    这可能是您手机上现存的app与新编译的的文件发生冲突,只需将设备上的同app卸载掉即可。

    7.有时候一些css样式无法生效

    这个时候你可以尝试给样式失效的元素添加一个容器,并把样式应用到容器组件上试试。

    8.debugger 卡顿

    我遇到过一种这样的问题,RN运行着一段时间后就发生了“调试卡死”这种奇怪的现象,也就是说,每当开启调试debugger之后,就变得巨慢无比,交互事件响应会变得缓慢反应或者直接就不反应。当你关闭debugger之后就会消失,并且webview嵌入页不受此现象制约。这就是明摆着不给你看console.log()呀。
    解决办法:
    很神奇的解决办法,来自stackoverflow。将你手机的时间调至比你的电脑慢1分钟即可。

    作者:zbc0012
    链接:https://www.jianshu.com/p/e489f844de0a
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    相关文章

      网友评论

          本文标题:【坑】React-Native android 开发踩坑记

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