Created
February 25, 2025 00:25
-
-
Save GNUGradyn/7c425e734723b197deeadc0b163d79cb to your computer and use it in GitHub Desktop.
launch emulator and app only script for react native
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Most of the time when you launch the app you don't actually need to recompile the native app bundle. You only need to do that when you've added/removed/updated a native module. This allows running the emulator without compiling the app via `yarn emu` | |
const { execSync, spawn } = require("child_process"); | |
const path = require("path"); | |
const os = require("os"); | |
const fs = require("fs"); | |
const gradlePath = path.join(__dirname, "android", "app", "build.gradle"); | |
const gradleContents = fs.readFileSync(gradlePath, "utf8"); | |
const match = gradleContents.match(/applicationId\s+"(.+?)"/); | |
const applicationId = match ? match[1] : null; | |
if (!applicationId) { | |
console.error("Could not determine application ID."); | |
process.exit(1); | |
} | |
console.log(`Detected application ID: ${applicationId}`); | |
const androidHome = process.env.ANDROID_HOME || process.env.ANDROID_SDK_ROOT; | |
if (!androidHome) { | |
console.error("ANDROID_HOME or ANDROID_SDK_ROOT is not set."); | |
process.exit(1); | |
} | |
const emulatorPath = path.join(androidHome, "emulator", os.platform() === "win32" ? "emulator.exe" : "emulator"); | |
const adbPath = path.join(androidHome, "platform-tools", os.platform() === "win32" ? "adb.exe" : "adb"); | |
const runApp = () => { | |
console.log("Launching the app..."); | |
const nullDevice = os.platform() === "win32" ? "NUL" : "/dev/null"; | |
execSync(`"${adbPath}" shell monkey -p ${applicationId} -c android.intent.category.LAUNCHER 1 > ${nullDevice} 2>&1`, { stdio: "ignore" }); | |
} | |
try { | |
const avds = execSync(`"${emulatorPath}" -list-avds`, { encoding: "utf8" }) | |
.trim() | |
.split("\n"); | |
if (avds.length === 0) { | |
console.error("No emulators found. Create one with `avdmanager`."); | |
process.exit(1); | |
} | |
const emulatorName = avds[0]; // Default to first emulator | |
console.log(`Starting emulator: ${emulatorName}`); | |
// Check if an emulator is already running | |
const devices = execSync(`"${adbPath}" devices`, { encoding: "utf8" }) | |
.split("\n") | |
.slice(1) | |
.filter(line => line.trim().endsWith("device")); | |
if (devices.length > 0) { | |
console.log("An emulator is already running. Skipping launch."); | |
runApp(); | |
} else { | |
const emulatorProcess = spawn(emulatorPath, ["-avd", emulatorName], { | |
stdio: "inherit", | |
detached: true | |
}); | |
emulatorProcess.unref(); | |
console.log("Waiting for emulator to boot..."); | |
let loops = 0; | |
const interval = setInterval(() => { | |
try { | |
const output = execSync(`"${adbPath}" shell getprop sys.boot_completed`, { encoding: "utf8", stdio: "pipe" }).trim(); | |
if (output === "1") { | |
clearInterval(interval); | |
console.log("Emulator is ready!"); | |
runApp(); | |
} | |
else if (loops < 15) { | |
loops++ | |
} else { | |
console.error("Gave up waiting for emulator"); | |
clearInterval(interval); | |
} | |
} catch(error) {}; | |
}, 2000) | |
} | |
} catch (error) { | |
console.error("Error:", error.message); | |
process.exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make executable and add this to your package.json scripts