diff --git a/packages/flutter_tools/lib/src/android/android_device.dart b/packages/flutter_tools/lib/src/android/android_device.dart index bf9515d3cf..85035a9342 100644 --- a/packages/flutter_tools/lib/src/android/android_device.dart +++ b/packages/flutter_tools/lib/src/android/android_device.dart @@ -17,6 +17,7 @@ import '../build_configuration.dart'; import '../device.dart'; import '../flx.dart' as flx; import '../globals.dart'; +import '../service_protocol.dart'; import '../toolchain.dart'; import 'adb.dart'; import 'android.dart'; @@ -185,12 +186,12 @@ class AndroidDevice extends Device { return true; } - Future _forwardObservatoryPort(int port) async { - bool portWasZero = port == 0; + Future _forwardObservatoryPort(int devicePort, int port) async { + bool portWasZero = (port == null) || (port == 0); try { // Set up port forwarding for observatory. - port = await portForwarder.forward(observatoryDefaultPort, + port = await portForwarder.forward(devicePort, hostPort: port); if (portWasZero) printStatus('Observatory listening on http://127.0.0.1:$port'); @@ -214,13 +215,18 @@ class AndroidDevice extends Device { return false; } - await _forwardObservatoryPort(debugPort); - if (clearLogs) this.clearLogs(); runCheckedSync(adbCommandForDevice(['push', bundlePath, _deviceBundlePath])); + ServiceProtocolDiscovery serviceProtocolDiscovery = + new ServiceProtocolDiscovery(logReader); + + // We take this future here but do not wait for completion until *after* + // we start the bundle. + Future serviceProtocolPort = serviceProtocolDiscovery.nextPort(); + List cmd = adbCommandForDevice([ 'shell', 'am', 'start', '-a', 'android.intent.action.RUN', @@ -243,6 +249,13 @@ class AndroidDevice extends Device { printError(result.trim()); return false; } + + // Wait for the service protocol port here. This will complete once + // the device has printed "Observatory is listening on..." + int devicePort = await serviceProtocolPort; + printTrace('service protocol port = $devicePort'); + await _forwardObservatoryPort(devicePort, debugPort); + return true; } diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index c7f84d3b06..c618a5fbe9 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -199,7 +199,6 @@ class IOSDevice extends Device { return false; } - printTrace('Installation successful.'); return true; } diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index fe02d31f8f..567cdcd078 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -16,6 +16,7 @@ import '../build_configuration.dart'; import '../device.dart'; import '../flx.dart' as flx; import '../globals.dart'; +import '../service_protocol.dart'; import '../toolchain.dart'; import 'mac.dart'; @@ -462,6 +463,13 @@ class IOSSimulator extends Device { if (!(await _setupUpdatedApplicationBundle(app, toolchain))) return false; + ServiceProtocolDiscovery serviceProtocolDiscovery = + new ServiceProtocolDiscovery(logReader); + + // We take this future here but do not wait for completion until *after* + // we start the application. + Future serviceProtocolPort = serviceProtocolDiscovery.nextPort(); + // Prepare launch arguments. List args = [ "--flx=${path.absolute(path.join('build', 'app.flx'))}", @@ -486,7 +494,12 @@ class IOSSimulator extends Device { return false; } + // Wait for the service protocol port here. This will complete once + // the device has printed "Observatory is listening on..." + int devicePort = await serviceProtocolPort; + printTrace('service protocol port = $devicePort'); printTrace('Successfully started ${app.name} on $id.'); + printStatus('Observatory listening on http://127.0.0.1:$devicePort'); return true; } diff --git a/packages/flutter_tools/lib/src/service_protocol.dart b/packages/flutter_tools/lib/src/service_protocol.dart index db3bbbed20..70b6183204 100644 --- a/packages/flutter_tools/lib/src/service_protocol.dart +++ b/packages/flutter_tools/lib/src/service_protocol.dart @@ -29,7 +29,7 @@ class ServiceProtocolDiscovery { void _onLine(String line) { int portNumber = 0; - if (line.startsWith('Observatory listening on http://')) { + if (line.contains('Observatory listening on http://')) { try { RegExp portExp = new RegExp(r"\d+.\d+.\d+.\d+:(\d+)"); String port = portExp.firstMatch(line).group(1); diff --git a/packages/flutter_tools/test/service_protocol_test.dart b/packages/flutter_tools/test/service_protocol_test.dart index 5f5d5c6bdf..621266be0a 100644 --- a/packages/flutter_tools/test/service_protocol_test.dart +++ b/packages/flutter_tools/test/service_protocol_test.dart @@ -40,6 +40,11 @@ void main() { const Duration(milliseconds: 100), onTimeout: () => 77); // Expect the timeout port. expect(port, 77); + // Get next port future. + nextPort = discoverer.nextPort(); + logReader.addLine( + 'I/flutter : Observatory listening on http://127.0.0.1:52584'); + expect(await nextPort, 52584); }); }); }