最近想写个App,又觉得Native App 太无趣了Web App又没那么成熟然后发现了Facebook在9月发布的React Native比较新奇,所以决定捣鼓看看;

  React Native为Facebook开源的使用Javascript与React开发Android、IOS原生跨平台App,React也是Factbook开源的JS框架使用了虚拟DOM的概念,Factbook自己的应用如Groups也是基于React Native在国内也有淘宝iPad版本是哟个了React Native;

  React Native最早支持的是Mac,for Android windows的支持9月份才刚发布还存在不少坑;

基本环境安装

  开发Android App当然首先要安装:JDK、Android SDK配置好JDk、SDK环境变量;

  安装C++编译环境(可以选择Visual Studio 或mingw等),编译nodejs的C++模块会使用;

  安装git、安装Node.js环境;

React-Native 安装

  如果没有VPN最好设置npm镜像不然及其考验你的耐性

npm config set registry https://registry.npm.taobao.org

安装 React-Native

npm install -g react-native-cli

创建项目

进入你希望项目存放的工作目录,运行

react-native init HelloWorld



请耐性等待,第一次下载依赖会比较久;

运行packager

npm start或react-native start

  查看 http://localhost:8081/index.android.bundle?platform=android 是否能打开;

  启动模拟器,运行adb devices查看是否设备已连接上;

保持packager开启,另外打开一个命令行窗口,然后在工程目录下运行

运行React Native

react-native run-android

异常

A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
> Could not find com.android.support:appcompat-v7:23.1.1.

解决方案:安装Android Support Repository与Android Support Library

如没有VPN要耐性等待,gradle下载一些依赖。

运行成功

生成APK

进入项目目录进入Android目录,在命令行运行:

gradlew assembleRelease

在React-native 0.14.2版本的时候会存在两个坑,应该算是Bug;

在https://github.com/danhanfry/react-native-windows-apk发现了解决方案;

坑 1:

> A problem occurred starting process 'command 'react-native''

解决方案:进入项目目录》android目录》app目录打开react.gradle文件

在文件头加入:import org.apache.tools.ant.taskdefs.condition.Os

找到

commandLine "react-native", "bundle", "--platform", "android", "--dev", "true", "--entry-file",
entryFile, "--bundle-output", jsBundleFileDebug, "--assets-dest", resourcesDirDebug

修改为:

 if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine "cmd", "/c", "react-native", "bundle", "--platform", "android", "--dev", "true", "--entry-file",
entryFile, "--bundle-output", jsBundleFileDebug, "--assets-dest", resourcesDirDebug
} else {
commandLine "react-native", "bundle", "--platform", "android", "--dev", "true", "--entry-file",
entryFile, "--bundle-output", jsBundleFileDebug, "--assets-dest", resourcesDirDebug
}

找到:

commandLine "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file",
entryFile, "--bundle-output", jsBundleFileRelease, "--assets-dest", resourcesDirRelease

修改为:

if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine "cmd","/c", "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file",
entryFile, "--bundle-output", jsBundleFileRelease, "--assets-dest", resourcesDirRelease
} else {
commandLine "react-native", "bundle", "--platform", "android", "--dev", "false", "--entry-file",
entryFile, "--bundle-output", jsBundleFileRelease, "--assets-dest", resourcesDirRelease
}

坑2:

Execution failed for task ':app:bundleReleaseJsAndAssets'.
> Process 'command 'cmd'' finished with non-zero exit value 1

解决方案:

在HelloWorld\node_modules\react-native\packager\react-packager\src\SocketInterface目录找到index.js文件在40行左右把:

const sockPath = path.join(
tmpdir,
'react-packager-' + hash.digest('hex')
);

修改为:

let sockPath = path.join(
tmpdir,
'react-packager-' + hash.digest('hex')
);
if (process.platform==='win32'){
sockPath = sockPath.replace(/^\//, '')
sockPath = sockPath.replace(/\//g, '-')
sockPath = '\\\\.\\pipe\\' + sockPath
}

生成成功后就可以在:android\app\build\outputs\apk目录下看到生成的apk;

文章首发地址:Solinx

http://www.solinx.co/archives/507

React Native初试:Windows下Andriod环境搭建的更多相关文章

  1. React Native在window下的环境搭建(二):创建新项目

    React Native创建一个新项目: react-native init TestAndroidApp 提示:你可以使用--version参数(注意是两个杠)创建指定版本的项目.例如react-n ...

  2. React Native在window下的环境搭建(一)

    React Native官方开发文档 以下是本人抄录的: 初次接触React Native感觉和React很像,却是有点类似,但不完全是,React Native有自己的组件对象,不过它也自定义的组件 ...

  3. React Native - 1 Windows下的环境配置(Windows+Android)

    参考:https://facebook.github.io/react-native/docs/getting-started.html(要FQ)     网站上建议使用Chocolatey去配环境, ...

  4. Windows下的环境搭建Erlang

    Windows下的环境搭建 Erlang 一.安装编译器 在http://www.erlang.org/download.html下载R16B01 Windows Binary File并安装. 二. ...

  5. Redis在windows下的环境搭建

    Redis在windows下的环境搭建 下载windows版本redis,,官方下载地址:http://redis.io/download, 不过官方没有Windows版本,官网只提供linux版本的 ...

  6. 2017.7.18 windows下ELK环境搭建

    参考来自:Windows环境下ELK平台的搭建 另一篇博文:2017.7.18 linux下ELK环境搭建 0 版本说明 因为ELK从5.0开始只支持jdk 1.8,但是项目中使用的是JDK 1.7, ...

  7. windows下React-native 环境搭建

    公司决定试水react-native,mac审批还没下来,没办法,先用windows硬着头皮上吧. 参考文章: React Native 中文网官方文档 史上最全Windows版本搭建安装React ...

  8. React Native入门教程 1 -- 开发环境搭建

    有人问我为啥很久不更新博客..我只能说在学校宿舍真的没有学习的环境..基本上在宿舍里面很颓废..不过要毕业找工作了,我要渐渐把这个心态调整过来,就从react-native第一篇博客开始.话说RN也出 ...

  9. windows下 react-native环境搭建

    跟着慕课网做案例,搭建rn环境遇到很大问题. 下面说一下: 首先看一下文档:http://reactnative.cn/docs/0.44/getting-started.html#content 注 ...

随机推荐

  1. 利用servlet3.0上传,纯原生上传,不依赖任何第三方包

    tomcat7里面自带的servlet3.0.jar,支持很多新特性,例如,annotation配置servlet,上传,异步等等.... 如果你的tomcat版本低于7的话,单独在项目中引入serv ...

  2. mysql分页查询详解

    我们做的后端项目一般都会有admin管理端,当管理端将要展示数据的时候,就需要用到分页.所以分页的考查在面试中也相当多.在mysql中进行分页查询时,一般会使用limit查询,而且通常查询中都会使用o ...

  3. (学习笔记)HTML的<link>标签

    在HTML中<link>标签用于定义文档与外部资源的关系. <link>标签只存在于head部分. <head> <link rel="styles ...

  4. Android总结篇系列:Android广播机制

    1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...

  5. Java Session超时设置

    1.jsp页面直接设置                                                                        ); 2.web.xml设置,覆盖 ...

  6. 太可爱了!CSS3 & SVG 制作的米老鼠钟表

    米老鼠是大家非常熟悉的迪斯尼动画形象.这是一个可爱的效果,结合 CSS & SVG 图形实现的米老鼠钟表效果.Web 技术让很多生活中的事物都能搬到网上去,后面的推荐阅读也有很多的效果,感兴趣 ...

  7. EventRay UI Kit – Web & Mobile 的素材

    EventRay UI 工具包是一个免费的,可以现成使用的界面套件.包括多个为  Web 和移动应用设计的布局和 UI 元素.所有你需要做的就是下载这个 UI 工具包,点击源码下载打开的页面即可下载. ...

  8. CSS3文本温故

    1.CSS早期属性,分为三大类:字体.颜色和文本: 2.CSS文本类型有11个属性: 注:还有一个颜色属性:color,主要用来设置文本颜色 3.CSS3文本阴影属性:text-shadow语法:te ...

  9. clang LLVM 介绍和安装(Ubuntu10 64位)

    http://www.csdn.net/article/2013-11-27/2817632 的对Stanley B.Lippman采访提到clang的一些优点,以前程序员杂志也写过,为了提高系统的性 ...

  10. Android Content Provider基础

    Android Content Provider基础 Content Providers Content providers管理对一个结构化的数据集合的访问.它们封装了数据,并且提供了保护数据安全性的 ...