React Native环境部署

Node

1
2
3
brew install node
npm config set registry https://registry.npm.taobao.org --global
npm config set disturl https://npm.taobao.org/dist --global

Android Studio

  1. 下载Android Studio

  2. 环境变量,vi ~/.bash_profile

    1
    2
    3
    4
    5
    # JAVA_HOME
    export JAVA_HOME=$(/usr/libexec/java_home)
    # ANDROID_HOME
    export ANDROID_HOME=/Users/ybbz/Library/Android/sdk
    export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

最后source ~/.bash_profile

Android虚拟机

  1. Android虚拟机可以在Android Studio中下载、配置和启动
  2. 命令行快速启动虚拟机方法
    1
    2
    android list avd
    emulator -avd name(虚拟机名)

React Native

1
2
3
react-native init AwesomeProject
cd AwesomeProject
react-native run-android

遇到的问题

  1. JAVA_HOME报错:

    1
    2
    3
    4
    # 旧
    JAVA_HOME="/System/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/"
    # 新
    JAVA_HOME=$(/usr/libexec/java_home)
  2. Strict mode does not allow function declarations in a lexically nested statement
    找到node_modules/react-native/Libraries/Core/InitializeCore.js文件的112行,将function handleError(e, isFatal)修改为var handleError = function(e, isFatal),如下所示:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    // Set up error handler
    if (!global.__fbDisableExceptionsManager) {
    // function handleError(e, isFatal) {
    var handleError = function(e, isFatal) {
    try {
    ExceptionsManager.handleException(e, isFatal);
    } catch (ee) {
    /* eslint-disable no-console-disallow */
    console.log('Failed to print error: ', ee.message);
    /* eslint-enable no-console-disallow */
    throw e;
    }
    }

    const ErrorUtils = require('ErrorUtils');
    ErrorUtils.setGlobalHandler(handleError);
    }

参考

八宝粥 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!