我已经构建了我的应用程序,我可以在我的本地模拟器上运行它(也可以在同一网络内通过更改调试服务器在我的android设备上运行)。

然而,我想构建一个APK,我可以发送给没有访问开发服务器的人,我希望他们能够测试应用程序。

我看到有一节使用离线捆绑在iOS部分的文档。但我不知道如何在android上实现同样的功能。这可能吗?如果有,怎么做?

更新:关于这个问题的答案(Android无法加载JS包),据说离线包可以从开发服务器下载。但是当我从开发服务器获取包时,图像文件无法加载。


当前回答

如果你得到一个涉及index.android.js的错误。这是因为您正在使用新的React-native版本(我使用的是0.55.4) 只需将"index.android.js"替换为"index.js"

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/

其他回答

对我来说这是可行的(MACOS)

打开终端

cd project_directory

执行此命令

yarn start

打开另一个终端

运行这些命令

cd android
./gradlew assembleRelease

使用android studio为react的android项目生成签名apk也是一个很好的和简单的选择,在android studio中打开react的android项目并生成击键和签名apk。

React Native 0.49及以上版本

你应该在终端的工程目录下运行这个命令

1 - mkdir android/app/src/main/assets
2 - 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

如果低于0.49

  1 - mkdir android/app/src/main/assets
  2 - 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 studio在你的react原生应用目录中打开“android”文件夹,它会要求升级gradle和其他一些东西。去build->生成签名APK,并按照那里的指示。那好。

对于最新的react原生版本来捆绑项目

安卓

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/

iOS

React-native bundle——entry-file index.js——platform ios——dev false——bundle-output ios/main。Jsbundle——assets-dest ios

进入工程目录,执行如下命令:

cd android && ./gradlew assembleRelease