假設我們已經(jīng)開發(fā)和調試好程序了。但是怎么發(fā)布呢?
我們知道現(xiàn)在我們運行程序是要把npm啟起來的,所以我們是把js代碼部署到服務器上嗎?抱著這個疑問我找到了一個博客:http://blog.csdn.net/qq229200/article/details/78134227 里面的步驟是這樣的:
1:terminal進入到項目根目錄下執(zhí)行React-native start,然后瀏覽器http://localhost:8081/index.android.bundle?platform=android 檢查rn打包服務器是否正常運行起來了。
2: 檢查app/main下是否存在assets文件夾,不存在就創(chuàng)建它。然后執(zhí)行 curl -k "http://localhost:8081/index.android.bundle" > android/app/src/main/assets/index.android.bundle 將bundle文件下載并存儲到assets文件夾內(window操作系統(tǒng)可能運行不了,就在git bash里執(zhí)行)
3:打包,以下命令看自己需求(別告訴我你看不懂哈)
配置了簽名密鑰的可以:gradle aR gradle aD gradle iD
4:到這里就結束了,安裝(沒資源圖片的)apk吧,然后就會發(fā)現(xiàn)js里調用的圖片出不來。
又引發(fā)一絲疑問,難道RN開發(fā)所用到的圖片都應該是網(wǎng)絡上的?
1:帶著這個疑問重新找資料找到了這個博客:http://blog.csdn.net/non_attack/article/details/56676246 里面的介紹也是要將index.android.bundle編譯出來放到assets下,只不過多了個把資源文件也編譯到res下的圖片文件夾里,執(zhí)行這行命令就行了
react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
具體什么意思直接去文中的博客鏈接里看好了。android/app/src/main/assets這個路徑別搞錯了就行
2:到這里就結束了,安裝(有圖片的)apk吧。
新版本建的RN項目里面沒有"index.android.js"和"index.ios.js"文件,就"index.js"文件,這可如何是好?
開發(fā)調試的時候都很順利。打包的時候檢查打包服務器發(fā)現(xiàn)
http://localhost:8081/index.bundle?platform=android 也有東西啊,是不是執(zhí)行 curl -k "http://localhost:8081/index.bundle" > android/app/src/main/assets/index.bundle 或者 curl -k "http://localhost:8081/index.bundle" > android/app/src/main/assets/index.android.bundle這兩句話也是可以的呢,答案都是否定的,第一句話會報錯找不到index.android.bundle,第二句會報錯Parse error.(index.android.bundle:1),證明了index.bundle拷貝到assets時我們把他改成index.android.bundle也是不行的。
解決辦法:把項目里的index.js重命名成index.android.js,然后app/build.gradle里面的project.ext.react = [entryFile: "index.js"]也要做相應的修改
感謝這兩個博主的分享
參考博客:http://blog.csdn.net/qq229200/article/details/78134227
http://blog.csdn.net/non_attack/article/details/56676246