你好呀!
如果您一直在使用 React Native,您可能遇到过模拟器问题。您运行 React Native 命令,但未检测到模拟器。即使在 .zshrc、.bash_profile 或类似文件中设置了所有必要的环境变量后,它仍然无法工作。
以下是一些常见问题和修复:
找不到模拟器:如果 Android SDK 路径设置不正确,可能会发生这种情况。仔细检查 ANDROID_HOME 和 PATH 环境变量是否指向正确的目录。您可以将这些行添加到 .zshrc 或 .bash_profile:
export ANDROID_HOME=$HOME/Library/Android/sdk export PATH=$PATH:$ANDROID_HOME/emulator export PATH=$PATH:$ANDROID_HOME/tools export PATH=$PATH:$ANDROID_HOME/tools/bin export PATH=$PATH:$ANDROID_HOME/platform-tools
进行这些更改后,重新启动终端或运行 source ~/.zshrc (或 source ~/.bash_profile)。
Watchman 错误:有时,Watchman(Facebook 的文件监视服务)可能会导致陈旧文件缓存出现问题。要解决此问题,请运行:
watchman watch-del-all
然后,重新启动 React Native 服务器。
adb 反向连接到 localhost:如果您的应用程序无法从 Android 模拟器内连接到 localhost,您可能需要运行:
adb reverse tcp:8081 tcp:8081
此命令将流量从模拟器路由到本地计算机。确保可以从您的终端访问 adb。
清除缓存并重建:如果您仍然遇到问题,请尝试清除缓存并重建项目:
npm start -- --reset-cache cd android && ./gradlew clean cd ios && xcodebuild clean
然后再次运行您的 React Native 应用程序。
直接使用react-native run-android 或 run-ios:如果模拟器无法从 IDE 或终端正确启动,请尝试直接使用以下命令运行应用程序:
npx react-native run-android npx react-native run-ios
调试这些问题可能会令人沮丧,但这些步骤帮助我解决了许多常见问题。
我也创建了一个易于使用的脚本
const {spawn, exec} = require('child_process'); const path = require('path'); // Define paths const projectPath = path.resolve(); // Define commands const watchDelCommand = `watchman watch-del '${projectPath}'`; const watchProjectCommand = `watchman watch-project '${projectPath}'`; // Function to execute commands const clearWatchman = () => { // Execute watch-del command exec(watchDelCommand, (error, stdout, stderr) => { if (error) { console.error(`Error executing watch-del command: ${error.message}`); return; } if (stderr) { console.error(`stderr: ${stderr}`); return; } console.log(`watch-del command executed successfully: ${stdout}`); // Execute watch-project command exec(watchProjectCommand, (error, stdout, stderr) => { if (error) { console.error( `Error executing watch-project command: ${error.message}`, ); return; } if (stderr) { console.error(`stderr: ${stderr}`); return; } console.log(`watch-project command executed successfully: ${stdout}`); }); }); }; async function reverseAdb() { console.log('Running... adb reverse tcp:8080 tcp:8080'); // After the emulator has started adb reverse tcp:8080 tcp:8080 const adbReverse = spawn('adb', ['reverse', 'tcp:8080', 'tcp:8080']); await new Promise((resolve, reject) => { let output = ''; adbReverse.stdout.on('data', data => { output = data.toString(); }); adbReverse.on('close', () => { resolve(); }); adbReverse.stderr.on('error', reject); }).catch(error => console.error('Error reversing ADB port to 8080:', error)); } async function runEmulator() { try { clearWatchman(); // Check for running emulator const adbDevices = spawn('adb', ['devices']); const devices = await new Promise((resolve, reject) => { let output = ''; adbDevices.stdout.on('data', data => { output = data.toString(); }); adbDevices.on('close', () => { resolve(output.includes('emulator')); }); adbDevices.stderr.on('error', reject); }); if (devices) { console.log('Emulator is already running'); reverseAdb(); return; } // Get list of available emulators const emulatorList = spawn('emulator', ['-list-avds']); const emulatorName = await new Promise((resolve, reject) => { let output = ''; emulatorList.stdout.on('data', data => { output = data.toString(); }); emulatorList.on('close', () => { const lines = output.split('\n').filter(Boolean); // Filter out empty lines if (lines.length === 0) { reject(new Error('No AVDs found')); } else { resolve(lines[lines.length - 1]); // Get the last non-empty line } }); emulatorList.stderr.on('error', reject); }); // Start the emulator in detached mode const emulatorProcess = spawn('emulator', ['-avd', emulatorName], { detached: true, stdio: 'ignore', }); emulatorProcess.unref(); // Allow the parent process to exit independently of the child reverseAdb(); console.log(`Starting emulator: ${emulatorName}`); } catch (error) { console.error('Error running emulator:', error); } } runEmulator(); // package.json "scripts": { ... "dev:android": "yarn android:emulator && npx react-native@latest start", "android:emulator": "node runEmulator.js", ... },
通过使用上面的脚本,您可以同时运行模拟器和React Native。
感谢您的阅读!
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3