Building React Native APKs without Metro Bundler dependency for SauceLabs, LambdaTest, etc.

Sandeep Dinesh
2 min readDec 16, 2021

The Android APKs built with the out-of-the-box ‘react-native run-android’ command uses a metro bundler (either it boots up a new instance or re-use an already running instance..)

Issue: When we use such APKs in cloud device farms like SauceLabs or LambdaTest, the APK fails to load on the test device because it was unable to connect to a Metro bundler, thereby failing the test automation scripts. Running a local/ build system machine metro bundler instance won’t help since the connectivity from the hub would be incognizant of the instance.

Solution/ TLDR: Instead of relying on run-android command, use the assets assembler of the react native and the gradle script shipped in the android project to assemble the APK type which you want to test.

# From the project directory of your react-native projectmkdir -p android/app/src/main/assets/# Create an android asset file in the directory created with the 
# name 'index.android.bundle' and go back to the project directory
cd android/app/src/main/assets/ && > index.android.bundle && cd -# Bundle the Android Assetsreact-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# Remove existing buildrm -rf android/app/build# Go to android folder and issue gradle command to assemble
# 'assembleDebug' for debug build, 'assembleRelease' for release
# release build involve sigining your APK, for test automation of
# development code usually the debugBuild is good enough
cd android && ./gradlew clean assembleDebug && cd ..# If all the steps are successful, your APK which won't require a
# metro instance running to render the UI would be available at
# android/app/build/outputs/apk/debug/app-debug.apk

You can now upload this APK into your cloud device farm (SauceLabs, LambdaTest, etc) using their REST interface or web page and trigger your tests.

Happy Test Automation!

--

--