React Native 开篇
近期打算入脑RN,把踩到的坑及心得记录下来:
搭建开发环境
可能会碰到的问题
安装Watchman时, 运行./autogen.sh :
./autogen.sh: 9: ./autogen.sh: aclocal: not found
./autogen.sh: 10: ./autogen.sh: autoheader: not found
./autogen.sh: 11: ./autogen.sh: automake: not found
./autogen.sh: 12: ./autogen.sh: autoconf: not found
解决方法:
sudo apt-get install autoconf
安装Watchman时, 执行make :
pywatchman/bser.c:31:20: fatal error: Python.h: 没有那个文件或目录
#include <Python.h>
^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
make[1]: *** [py-build] 错误 1
解决方法:
sudo apt-get install python-dev
安装Flow时, 执行npm install -g flow-bin :
npm ERR! tar.unpack untar error /home/zhuzi/.npm/flow-bin/0.36.0/package.tgz
npm ERR! Linux 3.13.0-79-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "-g" "flow-bin"
npm ERR! node v4.6.2
npm ERR! npm v2.15.11
npm ERR! path /usr/lib/node_modules/flow-bin
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir
....
npm ERR! [ '/usr/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:35:25',
npm ERR! '/usr/lib/node_modules/npm/node_modules/mkdirp/index.js:47:53',
npm ERR! 'FSReqWrap.oncomplete (fs.js:82:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! Please include the following file with any support request:
解决方法:
sudo npm install -g flow-bin
编辑器的选择
根据多年的开发经验,还是直接上手Facebook的亲儿子Nuclide吧,Nuclide是由Facebook提供的基于atom的集成开发环境,可用于编写、运行和 调试React Native应用。
安装atom:
sudo add-apt-repository ppa:webupd8team/atom
sudo apt-get update
sudo apt-get install atom
安装nuclide插件到atom, nuclide的已经开源 https://github.com/facebook/nuclide
安装过程参考https://nuclide.io/docs/editor/setup/#linux
打开Atom
在welcome guide中点击install a package
在install package界面的搜索框里输入nuclide然后回车
搜索出来后点击安装
整个过程没有遇到需要调整上网姿势的问题,开心!
测试安装
react-native init AwesomeProject
cd AwesomeProject
react-native run-android
碰到的问题,执行react-native run-android时,竟然强势要求build tools是23.0.1,我的环境有23.0.2 , 24+都编译不过。没办法,只好再补一个23.0.1。
然后我有碰到下面的错误
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:installDebug'.
> com.android.builder.testing.api.DeviceException: No connected devices!
好吧,那就打开模拟器或者连接一个可以debug的手机吧
ok, 再次运行react-native run-android终于见到
Installing APK 'app-debug.apk' on 'Galaxy_Nexus_API_21(AVD) - 5.0.2'
Installed on 1 device.
BUILD SUCCESSFUL
Total time: 9.699 secs
那么,代码什么结构,什么原理,持续研究中