React Native 混合开发(Android)入门实践

参考链接:

1.android ReactNative混合开发之最新教程

2.AndroidStudio嵌入ReactNative混合开发环境搭建

3.关于React-native里Android原生模块和组件的写法

4.React Native 常见错误总结

配置步骤

1.使用AS创建一个新的项目

2.在项目中集成React Native

使用AS自带的terminal工具执行以下命令:

  npm init
  npm install --save react react-native
  curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig

init 主要根据提醒生成package.json文件
install --save react react-native 安装React 和React Native
curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig 下载.flowconfig文件
在这里插入图片描述

3.React Native相关配置
添加"start": "node node_modules/react-native/local-cli/cli.js start" 到package.json 文件下 scripts标签
在这里插入图片描述

添加index.android.js文件到项目中
在这里插入图片描述

 'use strict';
 import React from 'react';
 import {
   AppRegistry,
   StyleSheet,
   Text,
   View
 } from 'react-native';
 class HelloWorld extends React.Component {
   render() {
     return (
       <View style={styles.container}>
         <Text style={styles.hello}>Hello, World</Text>
       </View>
     )
   }
 }
 var styles = StyleSheet.create({
   container: {
     flex: 1,
     justifyContent: 'center',
   },
   hello: {
     fontSize: 20,
     textAlign: 'center',
     margin: 10,
   },
 });
 AppRegistry.registerComponent('HelloWorld', () => HelloWorld);

4.App build.gradle配置

dependencies {
 ...
 compile "com.facebook.react:react-native:+" // From node_modules.
}

在这里插入图片描述
5.整个工程build.gradle配置

allprojects {
 repositories {
     ...
     maven {
         // All of React Native (JS, Android binaries) is installed from npm
         url "$rootDir/node_modules/react-native/android"
     }
 }
 ...
}

在这里插入图片描述
6.AndroidManifest文件配置
添加<uses-permission android:name="android.permission.INTERNET" />到AndroidManifest.xml中。

至此,同步一下,检查react native等库是否被添加到External Libraries中

代码操作

1.修改MainActivity
在这里插入图片描述
2.自定义一个Application
在这里插入图片描述
在这里插入图片描述
注:Arrays.<~>asList 等价于 Arrays.asList

3.在AndroidManifest文件中对自定义的Application进行注册
添加android:name=".MyApplication"到application标签中

部署项目

1.开启react本地服务器 npm start

2.连接安卓真机,访问8081端口 adb reverse tcp:8081 tcp:8081

3.在AS中安装应用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值