ReactNative→打包

零基础用react-native开发android app

原理

我们的debug程序中,js代码是调试时,不断从server加载到移动端的。但是发布成独立apk时,总不能让app启动的时候远程加载js代码吧。RN的做法是将所有js打成一个bundle文件,作为android的资源,放在assets目录下面。而assets下的文件是会在安装时,随工程一并安装到移动端本地的。这样apk安装好后,RN会负责去加载。

步骤

  • 将js代码导成资源目前android版本需要手动去做。参考issues。相信Facebook后面的版本会马上会发布自动导资源的命令。实际现在也挺简单:当react-native的server启动后。把http://localhost:8081/index.android.bundle?platform=android
    这个地址的js拷出来即可。
    1.cd to the project directory

    2.Start the react-native packager if not started
    
    3.Download the bundle to the asset folder: curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"
    
  • 生成签名apk参考官网教程。这里不用android studio,纯命令行就可以,官网教程很详细。最后生成的apk在android/app/build/outputs
    下面。

  • 嫌麻烦可以把以上步骤放在一个shell脚本中。

你可能感兴趣的:(ReactNative→打包)