参考如下内容:
首先在Android中嵌入React Native:
每一个Android Studio project下包含多个module,所以,从命令行进入到module根目录,运行:
[plain] view plain copy
$ npm init
$ npm install --save react-native
$ curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig
上述命令会创建一些react-native依赖的一些模块,在package.json文件的scripts属性中添加:
[javascript] view plain copy
"start": "node node_modules/react-native/local-cli/cli.js start"
注:如果scripts中存在其他键值对,则以逗号为分隔符。
在module的根目录创建index.android.js文件,并将一下内容下入index.android.js文件:
[javascript] view plain copy
'use strict';
import React from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
class HelloWorld extends React.Component {
render() {
return (
)
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
hello: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
AppRegistry.registerComponent('HelloWorld', () => HelloWorld);
在Android Studio中,在moudle的build.gradle文件中添加一下内容:
[javascript] view plain copy
allprojects {
repositories {
...
maven {
// All of React Native (JS, Android binaries) is installed from npm
url "$rootDir/node_modules/react-native/android"
}
}
...
}
在AndroidManifest.xml文件中添加网络访问权限(仅在开发或调试中需要该权限):