Well, I just got a problem about distributing APK for testing on real Android device before we publish or generate bundle and upload it into Google PlayStore. We know when we develop an App using Android Studio then if we want to try or test it on Android Simulator, Android Studio will automatically create app-debug.apk and this file will be able to install on real Android device.
React-Native also automatically create app-debug.apk once we run app for testing on Android Simulator, but app-debug.apk file which created by React-Native can’t run on real Android device. You will get error message “Unable to load script. Make sure you’re either running a Metro server …” just like on image above. Basically React-Native need Metro server to run, so how to generate APK that no need server (Serverless) or how do we get the Debug APK file to run in real Android device?
Follow these steps to generate Serverless APK or Debug APK in React Native:
- Open Terminal or Command Prompt and go to your react native project.
- Start Metro Server
npm start
- Open new Terminal or Command Prompt just like step number 1, then create a directory called assets
mkdir -p android/app/src/main/assets
- Create android bundle
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
- Download the javascript files created to the index.android.bundle
curl "http://localhost:8081/index.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"
- Build it
cd android && ./gradlew clean assembleDebug
Finally, checkout in your generated APK in project_directory/android/app/build/outputs/apk/debug. Then try to install and run it on your real Android device, hope those steps will help you out, because it works for me. Thanks to him for the solution.