ReactNative打離線包-android篇

官方文檔:http://facebook.github.io/react-native/docs/running-on-device-android.html#contenthtml

官方文檔2:http://facebook.github.io/react-native/docs/signed-apk-android.html#contentjava

離線包就是把RN和你寫的js圖片等資源都打包放入app,不須要走網絡下載。node

打包命令說明

react-native bundlereact

Options:android

--entry-file Path to the root JS file, either absolute or relative to JS root [required]ios

--platform Either "ios" or "android"git

--transformer Specify a custom transformer to be used (absolute path) [default: "/Users/babytree-mbp13/projects/xcodeProjects/AwesomeProject/node_modules/react-native/packager/transformer.js"]github

--dev If false, warnings are disabled and the bundle is minified [default: true]json

--prepack If true, the output bundle will use the Prepack format. [default: false]react-native

--bridge-config File name of a a JSON export of __fbBatchedBridgeConfig. Used by Prepack. Ex. ./bridgeconfig.json

--bundle-output File name where to store the resulting bundle, ex. /tmp/groups.bundle [required]

--bundle-encoding Encoding the bundle should be written in (https://nodejs.org/api/buffer.html#buffer_buffer). [default: "utf8"]

--sourcemap-output File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map

--assets-dest Directory name where to store assets referenced in the bundle

--verbose Enables logging [default: false]

安卓打包步驟

  1. 在工程根目錄下執行打包命令,好比 react-native bundle --entry-file demo/index.js --bundle-output ./android/app/src/main/assets/index.android.jsbundle --platform android --assets-dest ./android/app/src/main/res/ --dev false 請參考上面命令說明,根據本身的狀況進行修改再執行。注意要先保證[./android/app/src/main/assets/]文件夾存在。

  2. 命令執行完生成資源如圖
    clipboard.png

  3. 保證MainActivity.java中的setBundleAssetName與你的jsbundle文件名一致,好比.setBundleAssetName("index.android.jsbundle")就與我生成的資源名一致

  4. 一切OK 打包測試吧

To disable the developer menu for production builds:

For iOS open your project in Xcode and select Product → Scheme → Edit Scheme... (or press ⌘ + <). Next, select Run from the menu on the left and change the Build Configuration to Release.

For Android, by default, developer menu will be disabled in release builds done by gradle (e.g with gradle assembleRelease task). Although this behavior can be customized by passing proper value to ReactInstanceManager#setUseDeveloperSupport.

相關文章
相關標籤/搜索