Android集成React Native 踩坑合集 (github 有demo)

把RN项目的package.json拷贝到Android项目根目录

npm install 命令

新建js文件夹

如果说版本最新只到了0.20 那么mevn有问题

改一下

  maven {

            url "$rootDir/node_modules/react-native/android"
        }

react native 版本升级降级

查看版本

react-native –version

查看npm包管理的React Native 版本

npm info react-native

根据package.json 的版本更新

npm install


react native 升级

npm WARN React-native@0.35.0 requires a peer of react@~15.3.1 but none was installed.

解决方案:

方法一:npm install -save react@~15.3.1

方法二:在package.json中可以添加依赖

“dependencies”: {
“react”: “^15.3.2”,
“react-native”: “^0.35.0”,
“react-native-orientation”: “^1.17.0”,
“react-native-scrollable-tab-view”: “^0.6.0”,
}


react 升级

npm install react@16.0.0-alpha.8

16.0.0-alpha.8 是版本号

执行命令 生成 bundle

手动生成bundle文件,需要执行如下命令:

react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output app/src/main/assets/index.android.bundle --assets-dest app/src/main/res/

上面的不行就用下面的 大家注意 index.android.js 是你自己的js根目录的注册的控件 还有文件位置 必须自己注意
比如你如果是在项目文件夹里面又建立了一个文件夹js 那么就是

react-native bundle --platform android --de
v false --entry-file ./js/index.android.js --bundle-output ./app/src/main/asset
s/index.android.bundle --assets-dest ./app/src/main/res/


常见错误

1.invariant violation:expected a component class,got[object object]
创建自定义组件首字母要大写,否则会报错.

2.Module 0 is not a registered callable module.
将gradle升级成最新版本(cd
Android 进入android目录执行:sudo
./gradlew clean) 或者通过android studio工具升级.

3.android.view.WindowManager$BadTokenException: Unable to add window – token null is not valid; is your activity running?
该错误属于安卓Native的错误,如果引用的Activity不存在或者已经销毁,再次引用就会报该错误,如果是React native 调用原生控件的话,创建控件需要引用:getCurrentActivity()

4.android.app.Application cannot be cast to com.facebook.react.ReactApplication
需要将创建的MainApplication在AndroidManifest.xml配置好.

5.Element type is invalid: expected a string (for built-in components) or a class/function but got: object
发生原生一般是你引用了无效的组件,如果组件确实正确,看下引用的组件是否正常导出:(export defalut)

6.react native undefined is not an object (evaluating this….
发生该错误的一般是忘记bind(this),只要回调函数中需要用到this的,一般都需要bind.

7.React Native - expected a component class, got [object Object]

该错误可能是你引用了小写的组件,组件首字母一定要大写,比如应该写成

———记录我的package 的文件

{
“name”: “reacenativeappdemo”,
“version”: “1.0.0”,
“description”: “react native app demo”,
“main”: “index.android.js”,
“scripts”: {
“start”: “node node_modules/react-native/local-cli/cli.js start”
},

“author”: “zhangdongling”,
“license”: “ISC”,
“dependencies”: {
“react”: “16.0.0-alpha.12”,
“react-native”: “0.44.0”
}
}

github 地址 右上角点个star 支持下 谢谢
https://github.com/liudao01/ReactNativeAppDemo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值