File size: 2,939 Bytes
d97b8f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// 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);
}