upgrade-support icon indicating copy to clipboard operation
upgrade-support copied to clipboard

0.62.1 Release APK: Unable to load script. Make sure you're either running a Metro server (run 'react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.

Open palkerecsenyi opened this issue 4 years ago • 75 comments

Environment

System:
    OS: macOS 10.15.4
    CPU: (6) x64 Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz
    Memory: 21.43 MB / 8.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
    Yarn: Not Found
    npm: 6.14.4 - ~/.nvm/versions/node/v10.16.3/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.8.4 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.4, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK:
      API Levels: 28, 29
      Build Tools: 28.0.3, 29.0.2
      Android NDK: Not Found
  IDEs:
    Android Studio: Not Found
    Xcode: 11.4/11E146 - /usr/bin/xcodebuild
  Languages:
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.11.0 => 16.11.0 
    react-native: ^0.62.1 => 0.62.1 
  npmGlobalPackages:
    *react-native*: Not Found

Upgrading version

0.61.5->0.62.1

Description

I built my app for debug on Android, and everything works perfectly. However, when I build it for release, the build succeeds but upon launching the app it creates the following error:

java.lang.RuntimeException: Unable to load script. Make sure you're either running a Metro server (run 'react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.
 	at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
 	at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(Unknown Source:2)
	at com.facebook.react.bridge.JSBundleLoader$1.loadScript(Unknown Source:10)
 	at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(Unknown Source:18)
	at com.facebook.react.r.a(Unknown Source:180)
 	at com.facebook.react.r.a(Unknown Source:0)
 	at com.facebook.react.r$e.run(Unknown Source:68)
	at java.lang.Thread.run(Thread.java:919)

I used this command to build my app:

cd android
./gradlew assembleRelease

Here is my build.gradle: https://gist.github.com/palkerecsenyi/1434ce9340c167b80847ea2dcee30528 app/build.gradle: https://gist.github.com/palkerecsenyi/6c09728996c4f982dc3bd11ad757c45b package.json: https://gist.github.com/palkerecsenyi/d42eb7f1af6a6d3a83db9e271e720aec index.js (not sure if that's fully relevant): https://gist.github.com/palkerecsenyi/90274eadedb9ca9c8f4ebb0ec42f10ca

Reproducible demo

I'm not sure if this happens with all upgrades, and I'm not sure how to reproduce it. I know at least one other person has experienced this issue: https://github.com/facebook/react-native/issues/25348#issuecomment-606182604.

Thanks in advance!

palkerecsenyi avatar Apr 04 '20 20:04 palkerecsenyi

Update

I solved this issue by doing this before each build:

npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle

and then run

cd android
./gradlew assembleRelease

Adding the --assets-dest option to the command then gave a duplicate assets error when building, so I skipped that and it works perfectly.

Also, I tried experimenting with the version of hermes-engine, but that had no effect. This issue doesn't seem to be anything to do with Hermes.

palkerecsenyi avatar Apr 05 '20 18:04 palkerecsenyi

@palkerecsenyi that's more like a workaround as the bundle is already generated when you run assembleRelease.

Have you tried removing this line: https://gist.github.com/palkerecsenyi/6c09728996c4f982dc3bd11ad757c45b#file-app-build-gradle-L81? just a shot in the dark here as this dictates where the bundle will be stored before being added to the .apk.

lucasbento avatar Apr 06 '20 08:04 lucasbento

Thanks for the reply @lucasbento! I tried commenting that line out, and once again the build finished perfectly. However, when launching the app I got a different fatal error:

com.facebook.jni.CppException: Could not get BatchedBridge, make sure your bundle is packaged correctly
 	at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
 	at android.os.Handler.handleCallback(Handler.java:883)
 	at android.os.Handler.dispatchMessage(Handler.java:100)
 	at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(Unknown Source:0)
	at android.os.Looper.loop(Looper.java:237)
 	at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(Unknown Source:37)
 	at java.lang.Thread.run(Thread.java:919)

I tried running with both npx react-native run-android --variant=release and ./gradlew assembleRelease but both of them give the same error.

palkerecsenyi avatar Apr 06 '20 11:04 palkerecsenyi

Update

Commenting out these lines fixed it, and my app runs successfully (with Hermes enabled):

if (useIntlJsc) {
    implementation 'org.webkit:android-jsc-intl:+'
} else {
    implementation 'org.webkit:android-jsc:+'
}

palkerecsenyi avatar Apr 06 '20 11:04 palkerecsenyi

Beginner in React Native :- For me, I am getting this error because it seems like the build script is looking assets folder inside the Android project.

So, I just created the folder: mkdir android/app/src/main/assets

and ran the command: (Found here) npx 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 --verbose

And then when I run npx react-native run-android: I can see my app.js content on android emulator. Woohoo! However, now I am trying to figure out, why are the changes not live! I can see the changes in App.js reflect immediately on iOS simulator, but not on Android emulator. But that should be a seperate thread altogether. ✌️

Ayyanchira avatar Apr 06 '20 16:04 Ayyanchira

@Ayyanchira I'm not completely sure, but I think it might have something to do with the fact that you've set --dev false in the bundle command.

Try using this instead:

npx react-native bundle --platform android --dev true --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res --verbose

Again, I'm not too sure about this. In debug mode, I usually do this:

cd android
./gradlew assembleDebug
adb install app/build/outputs/apk/debug/app-debug.apk

cd ..
npx react-native start

and then launch the app on the phone/emulator. This seems to work fine and was unaffected by the update to 0.62.x

palkerecsenyi avatar Apr 06 '20 18:04 palkerecsenyi

The ./gradlew assembleDebug is completely fine to me.

However the ./gradlew assembleRelease fails with error:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:mergeReleaseResources'.

And a bunch of error like: android/app/build/generated/res/react/release/raw/node_modules_reactnativevectoricons_glyphmaps_ionicons.json: Error: Duplicate resources

Then it works if I go to android/app/src/res/ and delete all drawable-hdpi,mdpi,xhdpi,xxhdpi,xxxhdpi and raw folders.

Does anyone knows any ticket to fix this? I didn't find.

Thank you

wilsolutions avatar Apr 07 '20 19:04 wilsolutions

Pessoal, essa solução funcionou para mim:

Unable to load script from assets 'index.android.bundle'. Make sure... Esse erro geralmente acontece porque o sistema não conseguiu criar o bundle inicial que contém todo o código Javascript da aplicação.

1- Para resolver comece criando uma pasta assets dentro da pasta android/app/src/main.

2- Logo após, execute o comando:

npx 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/

3- Agora, feche as abas do terminal e execute novamente o comando:

npx react-native run-android ou yarn run react-native run-android ou yarn android

Fonte: https://react-native.rocketseat.dev/erros/android/

Taynan-Vieira avatar Apr 22 '20 19:04 Taynan-Vieira

Facing this issue even in debug build. Any solid solution yet?

Relax594 avatar May 11 '20 07:05 Relax594

Currently facing this issue on 0.62.2, the bundle created from gradlew bundleRelease wants to connect to the metro server somehow

duncte123 avatar May 28 '20 10:05 duncte123

@duncte123 Did you find a solution to this?

singhvivek2503 avatar Jun 18 '20 15:06 singhvivek2503

@duncte123 Did you find a solution to this?

Yes I did, I compiled on java 11 for some reason. Switching to java 8 solved the issue.

duncte123 avatar Jun 18 '20 17:06 duncte123

Still seeing the issue and confirmed we're using Java 8 to compile

lfalkner avatar Jun 23 '20 21:06 lfalkner

I had this issue last week and the root cause was that it was using node 13 (the system version on my Mac - Big Sur) to bundle rather than node 10. Once I forced it to use node 10, it finally stopped crashing for release bundles. For some reason nvm stopped working 🤷‍♂️.

jstheoriginal avatar Jul 08 '20 17:07 jstheoriginal

@lfalkner Still seeing the issue and confirmed we're using Java 8 to compile

You did not find a solution?

Imperyall avatar Jul 08 '20 17:07 Imperyall

@duncte123 Currently facing this issue on 0.62.2, the bundle created from gradlew bundleRelease wants to connect to the metro server somehow

Same problem, java 8 version

Imperyall avatar Jul 08 '20 17:07 Imperyall

@duncte123 Currently facing this issue on 0.62.2, the bundle created from gradlew bundleRelease wants to connect to the metro server somehow

Same problem, java 8 version

Which node version are you using when you put node -v in the command line in your project directory?

jstheoriginal avatar Jul 08 '20 19:07 jstheoriginal

@ duncte123 В настоящее время сталкивается с этой проблемой на 0.62.2, пакет, созданный из gradlew bundleReleaseхочет как-то подключиться к серверу метро

Та же проблема, версия Java 8

Какую версию узла вы используете, когда помещаете node -vв командную строку в каталоге вашего проекта?

v13.11.0

Imperyall avatar Jul 08 '20 19:07 Imperyall

That’s the same issue I had. It doesn’t work.

If you change it to use 10.16.3, does it work? That was my issue and it resolved once I used that older node version.

jstheoriginal avatar Jul 08 '20 19:07 jstheoriginal

Это та же проблема, что и у меня. Не работает

Если вы измените его на использование 10.16.3, это работает? Это была моя проблема, и она разрешилась, когда я использовал ту старую версию узла.

Switch to v10.21.0, nothing has changed

Imperyall avatar Jul 08 '20 20:07 Imperyall

I did have one other thing that might be required as well. I set the metro preset to exactly 0.58.

jstheoriginal avatar Jul 08 '20 21:07 jstheoriginal

I have a similar problem. i'm upgrading from 61.5 to 63.00. i changed com.android.tools.build:gradle:3.4.2 -> com.android.tools.build:gradle:3.5.3 and https://services.gradle.org/distributions/gradle-5.5-all.zip -> https://services.gradle.org/distributions/gradle-6.2-all.zip

When i launch ./gradlew bundleDevRelease i don't find into the app.bundle the index.android.bundle. If i discard the previous changes it works. Do other people have the same mistake?

Has something changed with the new gradle?

lorenzoangelini avatar Jul 09 '20 13:07 lorenzoangelini

I solved the problem when changing the build tools version from 4.0.1 to 3.5.3

Imperyall avatar Aug 10 '20 06:08 Imperyall

After creating the next bundle, everything repeated

Imperyall avatar Aug 12 '20 15:08 Imperyall

Not working yet, Only works with assembleDebug and assembleRelease, with the bundleRelease fails when it's deployed in the play store.

It's hard to test for this to fail when the bundleRelease cannot be installed for testing, or is there a way to know it will fail before deploying?

package.json: "react": "16.13.1", "react-native": "^0.63.0" "@react-native-community/async-storage": "^1.11.0", "@react-native-community/datetimepicker": "^2.6.1", "@react-native-community/google-signin": "^4.0.3", (recently added) "react-native-calendars": "^1.403.0", (recently added) "@react-native-community/hooks": "^2.6.0", "@react-native-community/masked-view": "^0.1.10", "@react-native-community/netinfo": "^5.9.5", "@react-native-community/picker": "^1.6.6",

node v10.16.3 java version "1.8.0_261" com.android.tools.build:gradle:3.5.3

Crashlytics Report

Fatal Exception: java.lang.RuntimeException: Unable to load script. Make sure you're either running a Metro server (run 'react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release. at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(CatalystInstanceImpl.java) at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:2) at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:10) at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:18) at com.facebook.react.ReactInstanceManager.a(ReactInstanceManager.java:264) at com.facebook.react.ReactInstanceManager.access$000(ReactInstanceManager.java) at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:68) at java.lang.Thread.run(Thread.java:919)

fabianmedina09 avatar Sep 08 '20 13:09 fabianmedina09

This issue appears to crop up when using classpath 'com.android.tools.build:gradle:4.1.0' (4.0 or greater). Changing back to 3.6.4 works but I'd like to use 4.1.0 for other reasons. As a workaround you can manually bundle as people above have said before running ./gradlew :app:assembleRelease

evelant avatar Oct 13 '20 19:10 evelant

After banging my head off this all day I believe I've found a lead in this bug https://github.com/facebook/react-native/issues/29398

Check your android/app/build/generated/assets/react/release folder. For me index.android.bundle is sitting there, correctly compiled and ready to go. What seems to be happening is that the build process just never adds it to the APK. This appears to be some sort of non-deterministic or gradle task ordering issue. On the other thread some people report that running bundleRelease twice picks up the js bundle the second time and correctly includes it. For me using assembleRelease running twice did not fix it.

This certainly seems to be an issue with the react-native gradle scripts. Digging in I haven't been able to find much. I think maybe the copy${targetName}BundledJs (copyReleaseBundledJs in this case) task in node_modules/react-native/react.gradle should be the thing that adds the file but I'm not sure.

I have no real experience with gradle and the Android build system so I don't know what actually does the final step of copying the file into the APK. Wherever that happens I think we'll find the root of this issue. I'd guess that the copy-bundle-to-apk step somehow happens before the bundle is finished or copied to its final output location. Can anyone point me toward where the actual "add stuff to the apk" step happens?

evelant avatar Oct 14 '20 00:10 evelant

OK I've found a workaround for now. This probably isn't a great solution since it doesn't take into account build variants and whatnot but it works for a basic :app:assembleRelease.

I added the following to android/app/build.gradle

//Fix for bundle not getting included in APK or AAB with android gradle plugin 4+
//Seems like there's a task ordering issue and the assets get computed before the bundle is copied
//This forces the copy task to run before merge resources
project.afterEvaluate {
        tasks.findAll { task ->
            task.name.startsWith('merge') && task.name.endsWith('Resources')
        }.each { t -> t.dependsOn "copyReleaseBundledJs" }
}

This forces gradle to run the "copy the js bundle to assets" task before the "merge assets into the apk" task. It seems that for whatever reason with gradle 4+ the task ordering can be wrong and the bundle gets moved to intermediate dir after gradle has already picked up all the assets so it never gets included in the APK.

Hopefully someone with more knowledge of gradle than I can chime in with a better solution.

evelant avatar Oct 14 '20 17:10 evelant

I am experiencing this issue on react-native: 0.63.2 after upgrading from a lower version. As some other users have also mentioned, I am running a classpath 'com.android.tools.build:gradle:4.1.0' which might be causing the problem. When running ./gradlew assembleRelease the APK might or might not work. This is happening kind of randomly.


I also tried running the command:

npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle

and it does work. However, I noticed the startup time of the app is 3 TIMES LONGER than it usually is! Something unnecessary is getting bundled and making the initial parsing VERY slow.

wmonecke avatar Oct 16 '20 16:10 wmonecke

@wmonecke I think that's probably a separate issue -- it seems there are two issues with similar symptoms here.

  1. If you don't have usesCleartextTraffic then I think your app can fail to run in development mode but IIRC react native has defaulted to API 29 for awhile now. I don't remember if the default template has usesCleartextTraffic or not.

  2. The JS bundle doesn't get included in the production apk or aab because the gradle tasks run out of order. If you're still running into the "sometimes the production bundle is missing but not always" it's probably this one. There's a PR to fix it https://github.com/facebook/react-native/pull/30177 and I posted a workaround above.

evelant avatar Oct 16 '20 23:10 evelant