简体   繁体   English

React Native应用程序无法在USB或APK格式的Android上构建,但在模拟器中可以正常构建

[英]React Native app won't build for Android on USB or in APK form, but builds fine in emulator

My react-native app builds and runs perfectly fine in emulator, but whenever I try to build to a USB Device I get the following error: 我的react-native应用程序可以在模拟器中构建并正常运行,但是每当尝试构建到USB设备时,都会出现以下错误:

Unable to load script from assets 'index.android.bundle'. Make sure your bundle is packaged correctly or you're running a package server.

jniLoadScriptFromAssets
catalystInstanceImpl.java

loadScriptFromAssets
JSBundleLoader.java:31

runJSBundle
CatalystInstanceImpl.java:234

CreateREactContext
ReactInstanceManager.java:114*

In the AndroidStudio debug console I'm seeing the following errors on build: 在AndroidStudio调试控制台中,我在构建时看到以下错误:

D/ReactNative: Initializing React Xplat Bridge after initializeBridge
               CatalystInstanceImpl.runJSBundle()
E/unknown:ReactNative: Exception in native call
                       java.lang.RuntimeException: Unable to load script from assets 'index.android.bundle'. Make sure your bundle is packaged correctly or you're running a packager server.
                           at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
                           at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:216)
                           at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:31)
                           at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:234)
                           at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1114)
                           at com.facebook.react.ReactInstanceManager.access$900(ReactInstanceManager.java:113)
                           at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:911)
                           at java.lang.Thread.run(Thread.java:764)

I'm stuck and tired of banging my head against the wall. 我被困住并且厌倦了把头撞在墙上。

Debug Builds 调试版本

If you're starting your build from within Android Studio, I found that it doesn't always bundle the Javascript code with the built app. 如果您是从Android Studio中开始构建的,那么我发现它并不总是将Javascript代码与构建的应用程序捆绑在一起。 To fix it, run your app on the USB device with: 要修复它,请使用以下命令在USB设备上运行您的应用:

react-native run-android

I've had much better results using the react-native CLI tool than with Android Studio. 使用react-native CLI工具比使用Android Studio获得了更好的结果。

Release Builds 发布版本

Currently (as of React Native v0.56), there is a bug with bundling image assets that might be affecting you. 当前(从React Native v0.56开始),存在一个捆绑图像资产的错误,可能会影响您。 In my experience, Android Studio would build the signed APK just fine, but would fail to include the bundled Javascript when using the latest Gradle. 以我的经验,Android Studio可以很好地构建签名的APK,但是在使用最新的Gradle时将无法包含捆绑的Javascript。 There is a fix coming in 0.57, but until then, there are several workarounds described in the related issue and pull request . 0.57中有一个修复程序,但是在此之前,相关问题拉取请求中描述了几种解决方法。

I have seen this error before, then I found that I didn't start up react native package server correctly. 我之前曾见过此错误,然后发现我没有正确启动反应本机软件包服务器。

You should first startup the react native server(using react-native start ), then you can begin installing your app by using android-studio. 您应该先启动react native服务器(使用react-native start ),然后才能使用android-studio开始安装应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM