修改gradle-wrapper.properties中的值
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
修改成
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
常見錯誤css
1.RN項目必定要注意版本問題,不管是react native仍是其餘組件的版本都必定要注意,否則會報錯。儘可能不要使用過高的版本,會有不少的問題。基本上全部的錯誤若是你實在解決不了均可以經過下降版本解決。html
新版本的react-native初始化的項目會有這個錯誤(當時是0.59以上的版本報了這個錯誤),能夠下降版本或者以下解決辦法java
遇到如下問題的請注意:
Unable to resolve module `./index` from `E:\reactNative\AProject\node_modules\react-native\scripts/.`: The module `./index` could not be found from `E:\reactNative\AProject\node_modules\react-native\scripts/.`. Indeed, none of these files exist:
這個問題是因爲react-native找不到index文件所致使
解決方法:
找到目錄文件:node_modules\react-native\scripts\launchPackager.bat
將:
node "%~dp0..\cli.js" start
修改成:
node "%~dp0..\cli.js" start --projectRoot ../../../
重啓服務便可node
2.有時候react-native的第三方組件會有Cannot read property xxx' of undefined的錯誤,多是由於react的版本低致使的,由於高版本的react中再也不有proptypes屬性,須要導入prop-types組件。react
如下是我測試demo時使用的版本及用其餘版本遇到的問題:android
"dependencies": {
"react": "16.3.1", //該版本不帶proptypes屬性,須要引入prop-types組件進行屬性類型檢驗
"react-native": "0.55.4", //最低須要react16.3.1版本,react-native版本和gradle版本須要匹配,不然會報錯。而有些組件也會要求和gradle版本匹配。因此,安裝時儘可能不要改變gradle版本,能夠經過改變組件的版原本適配
"react-native-scrollable-tab-view": "^0.10.0", //0.7.0版本使用的是react的低版本(帶proptypes屬性的版本)會報錯,後來升級到0.10.0就沒問題了
"react-native-tab-navigator": "^0.3.4", //0.3.3版本使用的是react的低版本(帶proptypes屬性的版本)
"react-native-swiper": "^1.5.14", //
"react-native-vector-icons": "^5.0.0", //以前用4.2.0版本會報Execution failed for task ':react-native-vector-icons:compileReleaseJavaWith 這個錯誤,後來升級到5.0.0版本就正常了
"react-navigation": "^1.5.11" //該版本和其高版本對gradle版本的要求彷佛不一樣,須要注意一下。
}
這是我在網上下載的開源RN項目的依賴版本,供參考。這裏使用的都是基於react低版本的(包括proptypes屬性的版本)npm
"dependencies": { "react": "^16.0.0-alpha.12",//這裏的react是低版本(包括proptypes屬性的版本),因此其餘組件都必須是使用低版本react的版本,不然會報錯 "react-native": "^0.45.1", "react-native-deprecated-custom-components": "^0.1.0", "react-native-scrollable-tab-view": "^0.7.4", "react-native-swiper": "^1.5.3", "react-native-tab-navigator": "^0.3.3", "react-native-vector-icons": "^4.2.0" },
安裝問題:json
這裏經過yarn安裝。yarn add react-native-vector-icons
react-native
react-native link
babel
安裝完成
修改gradle-wrapper.properties中的值
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
修改成
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
4.error: bundling failed: TypeError: Cannot read property 'bindings' of null
When upgrading to 0.56, make sure to bump your babel-preset-react-native package.json dependency to ^5.0.1 or newer.
5.react-native-video ^3.0.0須要使用版本27 SDK和版本27.0.3 BuildTools,因此0.55.0版本的react-native不合適。能夠經過升級react-native的版本修改改配置文件中sdk和gradle的版本或者直接修改sdk和gradle的版本(容易引起其餘配置錯誤)。
6.error: bundling failed: TypeError: Cannot read property 'bindings' of null (升級到react-native 0.56版本時報的錯誤)
解決辦法:
When upgrading to 0.56, make sure to bump your babel-preset-react-native package.json dependency to ^5.0.1 or newer.
7.Unable to resolve module 'AccessibilityInfo', when trying to create release bundle(在react-native 0.56.0版本中報的錯)
It seems like a bug in 0.56 related to dependencies. The "solution" is to find the correct combination of dependencies' versions. We found a workaround by installing those versions EXACTLY: react-native >> 0.55.4 babel-core >> latest babel-loader >> latest babel-preset-react-native >> 4.0.0 So you have to run those commands in order: react-native init AwesomeProject cd AwesomeProject react-native run-android npm uninstall react-native npm install --save react-native@0.55.4 react-native run-android npm install --save babel-core@latest babel-loader@latest npm uninstall --save babel-preset-react-native npm install --save babel-preset-react-native@4.0.0 react-native run-android
-------------------------------------------------------------------------------------------------------------------------------------
更新版本
若是你要知道如今React Native的最新版本
npm info react-native
假設如今拿到一個已經在好久之前寫好的項目,或者本身的項目須要更新,或者RN更新了,這個時候咱們須要對手上的項目進行更新,以使用更新的API。
這裏有兩個方法:
修改項目的package.json文件
將那個「react-native」後面的版本改成當前最新版本 0.55.4,而後
npm install
完成之後,再次查看項目版本就會發現版本已經更新到指定版本了。
或者直接npm
npm install --save react-native@0.55.4
版本更新好了以後,剩下的就是更新項目文件了
react-native upgrade
這個過程比較耗時,會進行一些文件的更改、替換操做。等待完成後,升級就完成了。
有的時候,發現最新版本的RN不穩定,須要回退。
上面兩個方法隨便選一個,改下版本號,一樣的操做, 不贅述。
建立通常react-native項目:
react-native init 項目名稱
建立指定版本的react-native項目
react-native init 項目名稱 --version 0.55.4
注:--version 前面必須兩個「-」
查看react-native版本
進入項目目錄:
react-native -V
注:-V,V必須大寫,能夠經過react-native -h查看
cd android
gradlew clean 清除