I am trying to bundle and build the debug build of the application to put on the phone and have it run without the packager running.
What I have tried so far are the following two ways:
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug
and
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
after each of which I then ran ./gradlew assembleDebug to create the APK.
In either case the app did not run. I get an error with the message
"Unable to load script.Make sure you are either running a Metro server or that your bundle 'index.android.bundle' is packaged correctly for release"
So my questions are:
I wonder what's the use case of having debug build on a device, without packager instance? Anyway, there's a big comment blob inside app/build.gradle of your project. You'll need to adjust these. Feel free to also check the source of what's passed to the bundle command (it's ran by react.gradle script anyway so no need to run it by yourself other than for debugging purposes): https://github.com/facebook/react-native/blob/9b0adb5ad132b8ff37e707a4943411d92b4e58dc/react.gradle#L129-L137
Closing, as this is not a CLI issue and there's a documentation on how to use it. This might have been easier to find though. PRs to official RN docs are always welcome :)
Alright thanks, I'll look into it. I did come across that file but didn't think I'd have to modify anything there as the answers to this stackoverflow question indicated that all that would have to be done is run react-native bundle and then ./gradlew assembleDebug
As for the use case: That way the build doesn't have to be signed. It allows me (and other team members, who might not have access to the keystore) to put builds on a device for other people to try out (e.g. testers), without having to keep the packager running.
I don't think it's a good idea to give debug builds to testers. They're way slower than release builds, plus include visible warning/error messages. I'd advise using a proper distribution platform, like App Center, if possible. It makes distribution quite easy, worth giving it a shot.
We do use a distribution platform. Sometimes it's a lot more convenient and faster to just put a build directly on the phone from your laptop (as those distribution platforms can take quite a while).
As for the visible warnings/error messages: According to the docs passing the --dev true--dev false-- option should disable the warnings no?
If false, warnings are disabled and the bundle is minified
Yes, sorry I miswrote. I meant --dev false
Yea, this would produce production JS bundle, that's right.
Alright.
By the way does this
Anyway, there's a big comment blob inside app/build.gradle of your project. You'll need to adjust these.
mean that all the answers on the linked Stackoverflow question are wrong, as none of them ever mention adjusting anything in the app/build.gradle file?
The answer is just an approximation of the solution and it's from 2016. The actual paths may have been changed since then. That's why you need to make sure the react.gradle script is able to reach for your manually built JS bundle.
Ah okay, that makes sense. If I figure it out I'll make sure to add it to the docs.
PR created!
I'm having a somewhat similar issue. I'm trying to run instrumentation AndroidTests on a headless emulator without running the packager, but keep getting the "unable to load script" error when the test kicks off. Does the bundleInDebug not work for the installDebugAndroidTest directive?