android ReactNative之Cannot find entry file index.android.js in any of the roots


android ReactNative之Cannot find entry file index.android.js in any of the roots

 

由於公司要求,想讓我去了解一下ReactNative開發,說是為了以后開發的以后方便移植到IOS上。暈!!!沒辦法,照着React Native的中文開發文檔配置環境https://reactnative.cn/docs/0.51/getting-started.html#content,配置過程還算簡單,以為很簡單,其實還是我太單純。照着文檔創建項目,創建好后運行項目,坑出現了,這個坑我埋了半天的時間,真是太耽誤時間了。遇到的其實別的博客上都有說,Cannot find entry file index.android.js in any of the roots,就是這個錯誤日志,按照其他的博客的解答方式我一一嘗試,不幸無一能解決我的問題,反而使用了他們的方案衍生出其他的error日志,是我本來離答案很近又被扯遠了,我連續創建了三個項目才將問題解決,在此記錄,為人填坑。

在react native以前的版本,index.android.js與index.ios.js是分開的兩個文件,在最新版本中這兩個文件合並成index.js一個文件了。但是如果你在創建項目之后直接運行,肯定會報Cannot find entry file index.android.js in any of the roots這種類似的錯誤,因為在根目錄下已經不存在index.anroid.js和index.ios.js這兩個文件,所以肯定不會檢索到,你更改App.js的內容后更不可能生效。

我解決的方式是:創建項目時

 

react-native init AwesomeProject cd AwesomeProject
執行完這兩個命令后,不要直接去運行,先執行以下命令去創建assets目錄:


 

通過執行
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/
命令會在assets目錄下生成兩個文件


 

控制器會提示生成成功

 

 這個時候你再運行react-native run-android,就不會報錯了,完美運行。你可以測試一下,通過更改App.js的樣式,然后重新reload,樣式就會及時生效。

純是個人經過4、5個小時嘗試的方法,真實有效,歡迎來懟。


方法二:
2down voteaccepted

Changed the version of react project to a lower one from here

npm install -g rninit

rninit init [Project Name] --source react-native@0.40.0

Worked okay for both on both iOS and Android on . If you get Error Could not get BatchedBridge. run this

adb reverse tcp:8081 tcp:8081

This worked for me.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM