// Script for building and deploying to physical devices import { execSync } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; import { fileURLToPath } from 'url'; // Get the directory name in ES modules const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Make sure the dev server is running locally console.log('⚠️ Make sure your dev server is running on http://172.16.16.22:8080'); console.log('⚠️ And that your mobile device is on the same WiFi network as your computer'); // Copy physical device config to the main config location const physicalConfig = fs.readFileSync(path.join(__dirname, 'capacitor.config.physical.json'), 'utf8'); fs.writeFileSync(path.join(__dirname, 'android/app/src/main/assets/capacitor.config.json'), physicalConfig); console.log('✅ Physical device config applied'); try { // Run npm build console.log('🏗️ Building the project...'); execSync('npm run build', { stdio: 'inherit' }); // Run cap sync console.log('📱 Synchronizing Capacitor project...'); execSync('npx cap sync android', { stdio: 'inherit' }); // Build the APK console.log('📦 Building APK...'); execSync('cd android && .\\gradlew.bat assembleDebug', { stdio: 'inherit' }); // Check for connected devices const adbDevices = execSync('adb devices').toString(); console.log('📱 Available devices:'); console.log(adbDevices); // If no device is connected, stop if (!adbDevices.includes('\tdevice')) { console.log('❌ No physical device connected! Please connect a device and try again.'); process.exit(1); } // Get device ID (find the line that has "device" in it) const deviceLines = adbDevices.split('\n').filter(line => line.includes('\tdevice')); if (deviceLines.length === 0) { throw new Error('No connected devices found'); } const deviceId = deviceLines[0].split('\t')[0].trim(); console.log(`Found device: ${deviceId}`); // Deploy to connected device console.log(`📲 Deploying to device ${deviceId}...`); execSync(`adb -s ${deviceId} install -r android/app/build/outputs/apk/debug/app-debug.apk`, { stdio: 'inherit' }); // Launch the app console.log('🚀 Launching app...'); execSync(`adb -s ${deviceId} shell am start -n com.sspl.app/com.sspl.app.MainActivity`, { stdio: 'inherit' }); console.log('✨ App is running on your physical device!'); console.log('📝 Notes for physical devices:'); console.log('1. Make sure your device can reach your computer at 172.16.16.22:8080'); console.log('2. Check your computer firewall settings to allow incoming connections on port 8080'); console.log('3. If still having connection issues, try setting up a local server accessible to both devices'); } catch (error) { console.error('❌ Error:', error.message); }