microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/androidPlatform.ts
363lines · modeblame
52f3873ddigeff10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
df8c800dArtem Egorov8 years ago | 4 | import * as semver from "semver"; |
52f3873ddigeff10 years ago | 5 | |
8df5011eYuri Skorokhodov5 years ago | 6 | import { GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform"; |
| 7 | import { IAndroidRunOptions, PlatformType } from "../launchArgs"; | |
| 8 | import { AdbHelper, AndroidAPILevel, IDevice } from "./adb"; | |
| 9 | import { Package } from "../../common/node/package"; | |
| 10 | import { PackageNameResolver } from "./packageNameResolver"; | |
| 11 | import { OutputVerifier, PatternToFailure } from "../../common/outputVerifier"; | |
| 12 | import { TelemetryHelper } from "../../common/telemetryHelper"; | |
| 13 | import { CommandExecutor } from "../../common/commandExecutor"; | |
| 14 | import { LogCatMonitor } from "./logCatMonitor"; | |
d7d405aeYuri Skorokhodov7 years ago | 15 | import * as nls from "vscode-nls"; |
| 16 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; | |
| 17 | import { ErrorHelper } from "../../common/error/errorHelper"; | |
78c2b4deRedMickey6 years ago | 18 | import { isNullOrUndefined } from "util"; |
ce5e88eeYuri Skorokhodov5 years ago | 19 | import { PromiseUtil } from "../../common/node/promise"; |
68a5b8d5JiglioNero5 years ago | 20 | import { AndroidEmulatorManager, IAndroidEmulator } from "./androidEmulatorManager"; |
8df5011eYuri Skorokhodov5 years ago | 21 | import { LogCatMonitorManager } from "./logCatMonitorManager"; |
34472878RedMickey5 years ago | 22 | nls.config({ |
| 23 | messageFormat: nls.MessageFormat.bundle, | |
| 24 | bundleFormat: nls.BundleFormat.standalone, | |
| 25 | })(); | |
d7d405aeYuri Skorokhodov7 years ago | 26 | const localize = nls.loadMessageBundle(); |
5c8365a6Artem Egorov8 years ago | 27 | |
52f3873ddigeff10 years ago | 28 | /** |
| 29 | * Android specific platform implementation for debugging RN applications. | |
| 30 | */ | |
299b0557Patricio Beltran10 years ago | 31 | export class AndroidPlatform extends GeneralMobilePlatform { |
0a68f8dbArtem Egorov8 years ago | 32 | // We should add the common Android build/run errors we find to this list |
34472878RedMickey5 years ago | 33 | private static RUN_ANDROID_FAILURE_PATTERNS: PatternToFailure[] = [ |
| 34 | { | |
| 35 | pattern: "Failed to install on any devices", | |
| 36 | errorCode: InternalErrorCode.AndroidCouldNotInstallTheAppOnAnyAvailibleDevice, | |
| 37 | }, | |
| 38 | { | |
| 39 | pattern: "com.android.ddmlib.ShellCommandUnresponsiveException", | |
| 40 | errorCode: InternalErrorCode.AndroidShellCommandTimedOut, | |
| 41 | }, | |
| 42 | { | |
| 43 | pattern: "Android project not found", | |
| 44 | errorCode: InternalErrorCode.AndroidProjectNotFound, | |
| 45 | }, | |
| 46 | { | |
| 47 | pattern: "error: more than one device/emulator", | |
| 48 | errorCode: InternalErrorCode.AndroidMoreThanOneDeviceOrEmulator, | |
| 49 | }, | |
| 50 | { | |
| 51 | pattern: /^Error: Activity class \{.*\} does not exist\.$/m, | |
| 52 | errorCode: InternalErrorCode.AndroidFailedToLaunchTheSpecifiedActivity, | |
| 53 | }, | |
| 54 | ]; | |
| 55 | | |
| 56 | private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = [ | |
| 57 | "BUILD SUCCESSFUL", | |
| 58 | "Starting the app", | |
| 59 | "Starting: Intent", | |
| 60 | ]; | |
52f3873ddigeff10 years ago | 61 | |
| 62 | private debugTarget: IDevice; | |
| 63 | private devices: IDevice[]; | |
| 64 | private packageName: string; | |
db6fd42aRuslan Bikkinin7 years ago | 65 | private adbHelper: AdbHelper; |
68a5b8d5JiglioNero5 years ago | 66 | private emulatorManager: AndroidEmulatorManager; |
8df5011eYuri Skorokhodov5 years ago | 67 | private logCatMonitor: LogCatMonitor | null = null; |
52f3873ddigeff10 years ago | 68 | |
| 69 | private needsToLaunchApps: boolean = false; | |
db6fd42aRuslan Bikkinin7 years ago | 70 | |
ce5e88eeYuri Skorokhodov5 years ago | 71 | public showDevMenu(deviceId?: string): Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 72 | return this.adbHelper.showDevMenu(deviceId); |
7daed3fcArtem Egorov8 years ago | 73 | } |
db6fd42aRuslan Bikkinin7 years ago | 74 | |
ce5e88eeYuri Skorokhodov5 years ago | 75 | public reloadApp(deviceId?: string): Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 76 | return this.adbHelper.reloadApp(deviceId); |
7daed3fcArtem Egorov8 years ago | 77 | } |
52f3873ddigeff10 years ago | 78 | |
299b0557Patricio Beltran10 years ago | 79 | // We set remoteExtension = null so that if there is an instance of androidPlatform that wants to have it's custom remoteExtension it can. This is specifically useful for tests. |
7daed3fcArtem Egorov8 years ago | 80 | constructor(protected runOptions: IAndroidRunOptions, platformDeps: MobilePlatformDeps = {}) { |
0a68f8dbArtem Egorov8 years ago | 81 | super(runOptions, platformDeps); |
db6fd42aRuslan Bikkinin7 years ago | 82 | this.adbHelper = new AdbHelper(this.runOptions.projectRoot, this.logger); |
68a5b8d5JiglioNero5 years ago | 83 | this.emulatorManager = new AndroidEmulatorManager(this.adbHelper); |
db6fd42aRuslan Bikkinin7 years ago | 84 | } |
43e1a996Ruslan Bikkinin7 years ago | 85 | |
db6fd42aRuslan Bikkinin7 years ago | 86 | // TODO: remove this method when sinon will be updated to upper version. Now it is used for tests only. |
8df5011eYuri Skorokhodov5 years ago | 87 | public setAdbHelper(adbHelper: AdbHelper): void { |
db6fd42aRuslan Bikkinin7 years ago | 88 | this.adbHelper = adbHelper; |
52f3873ddigeff10 years ago | 89 | } |
| 90 | | |
68a5b8d5JiglioNero5 years ago | 91 | public resolveVirtualDevice(target: string): Promise<IAndroidEmulator | null> { |
| 92 | if (!target.includes("device")) { | |
34472878RedMickey5 years ago | 93 | return this.emulatorManager |
| 94 | .startEmulator(target) | |
8df5011eYuri Skorokhodov5 years ago | 95 | .then((emulator: IAndroidEmulator | null) => { |
| 96 | if (emulator) { | |
34472878RedMickey5 years ago | 97 | GeneralMobilePlatform.setRunArgument( |
| 98 | this.runArguments, | |
| 99 | "--deviceId", | |
| 100 | emulator.id, | |
| 101 | ); | |
8df5011eYuri Skorokhodov5 years ago | 102 | } |
| 103 | return emulator; | |
| 104 | }); | |
34472878RedMickey5 years ago | 105 | } else { |
68a5b8d5JiglioNero5 years ago | 106 | return Promise.resolve(null); |
| 107 | } | |
| 108 | } | |
| 109 | | |
ce5e88eeYuri Skorokhodov5 years ago | 110 | public runApp(shouldLaunchInAllDevices: boolean = false): Promise<void> { |
549baae2RedMickey6 years ago | 111 | let extProps: any = { |
031832ffArtem Egorov8 years ago | 112 | platform: { |
259c018fYuri Skorokhodov5 years ago | 113 | value: PlatformType.Android, |
031832ffArtem Egorov8 years ago | 114 | isPii: false, |
| 115 | }, | |
| 116 | }; | |
| 117 | | |
549baae2RedMickey6 years ago | 118 | if (this.runOptions.isDirect) { |
| 119 | extProps.isDirect = { | |
| 120 | value: true, | |
| 121 | isPii: false, | |
| 122 | }; | |
| 123 | } | |
| 124 | | |
34472878RedMickey5 years ago | 125 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties( |
| 126 | this.runOptions, | |
| 127 | this.runOptions.reactNativeVersions, | |
| 128 | extProps, | |
| 129 | ); | |
ba953e9fRedMickey6 years ago | 130 | |
031832ffArtem Egorov8 years ago | 131 | return TelemetryHelper.generate("AndroidPlatform.runApp", extProps, () => { |
34472878RedMickey5 years ago | 132 | const env = GeneralMobilePlatform.getEnvArgument( |
| 133 | process.env, | |
| 134 | this.runOptions.env, | |
| 135 | this.runOptions.envFile, | |
| 136 | ); | |
78c2b4deRedMickey6 years ago | 137 | |
e3706a1cRedMickey6 years ago | 138 | if ( |
34472878RedMickey5 years ago | 139 | !semver.valid( |
| 140 | this.runOptions.reactNativeVersions.reactNativeVersion, | |
| 141 | ) /*Custom RN implementations should support this flag*/ || | |
| 142 | semver.gte( | |
| 143 | this.runOptions.reactNativeVersions.reactNativeVersion, | |
| 144 | AndroidPlatform.NO_PACKAGER_VERSION, | |
| 145 | ) | |
e3706a1cRedMickey6 years ago | 146 | ) { |
| 147 | this.runArguments.push("--no-packager"); | |
| 148 | } | |
| 149 | | |
34472878RedMickey5 years ago | 150 | let mainActivity = GeneralMobilePlatform.getOptFromRunArgs( |
| 151 | this.runArguments, | |
| 152 | "--main-activity", | |
| 153 | ); | |
e3706a1cRedMickey6 years ago | 154 | |
| 155 | if (mainActivity) { | |
| 156 | this.adbHelper.setLaunchActivity(mainActivity); | |
| 157 | } else if (!isNullOrUndefined(this.runOptions.debugLaunchActivity)) { | |
| 158 | this.runArguments.push("--main-activity", this.runOptions.debugLaunchActivity); | |
| 159 | this.adbHelper.setLaunchActivity(this.runOptions.debugLaunchActivity); | |
| 160 | } | |
| 161 | | |
34472878RedMickey5 years ago | 162 | const runAndroidSpawn = new CommandExecutor( |
| 163 | this.projectPath, | |
| 164 | this.logger, | |
| 165 | ).spawnReactCommand("run-android", this.runArguments, { env }); | |
e3706a1cRedMickey6 years ago | 166 | const output = new OutputVerifier( |
34472878RedMickey5 years ago | 167 | () => Promise.resolve(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS), |
| 168 | () => Promise.resolve(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS), | |
| 169 | PlatformType.Android, | |
| 170 | ).process(runAndroidSpawn); | |
e3706a1cRedMickey6 years ago | 171 | |
| 172 | return output | |
| 173 | .finally(() => { | |
| 174 | return this.initializeTargetDevicesAndPackageName(); | |
34472878RedMickey5 years ago | 175 | }) |
| 176 | .then( | |
| 177 | () => [this.debugTarget], | |
| 178 | reason => { | |
| 179 | if ( | |
| 180 | reason.message === | |
| 181 | ErrorHelper.getInternalError( | |
| 182 | InternalErrorCode.AndroidMoreThanOneDeviceOrEmulator, | |
| 183 | ).message && | |
| 184 | this.devices.length > 1 && | |
| 185 | this.debugTarget | |
| 186 | ) { | |
| 187 | /* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */ | |
| 188 | this.needsToLaunchApps = true; | |
| 189 | return shouldLaunchInAllDevices | |
| 190 | ? this.adbHelper.getOnlineDevices() | |
| 191 | : Promise.resolve([this.debugTarget]); | |
| 192 | } else { | |
| 193 | return Promise.reject<IDevice[]>(reason); | |
| 194 | } | |
| 195 | }, | |
| 196 | ) | |
| 197 | .then(devices => { | |
e3706a1cRedMickey6 years ago | 198 | return new PromiseUtil().forEach(devices, device => { |
| 199 | return this.launchAppWithADBReverseAndLogCat(device); | |
| 200 | }); | |
52f3873ddigeff10 years ago | 201 | }); |
| 202 | }); | |
| 203 | } | |
| 204 | | |
ce5e88eeYuri Skorokhodov5 years ago | 205 | public enableJSDebuggingMode(): Promise<void> { |
34472878RedMickey5 years ago | 206 | return this.adbHelper.switchDebugMode( |
| 207 | this.runOptions.projectRoot, | |
| 208 | this.packageName, | |
| 209 | true, | |
| 210 | this.debugTarget.id, | |
| 211 | ); | |
b57ea017Artem Egorov8 years ago | 212 | } |
| 213 | | |
ce5e88eeYuri Skorokhodov5 years ago | 214 | public disableJSDebuggingMode(): Promise<void> { |
34472878RedMickey5 years ago | 215 | return this.adbHelper.switchDebugMode( |
| 216 | this.runOptions.projectRoot, | |
| 217 | this.packageName, | |
| 218 | false, | |
| 219 | this.debugTarget.id, | |
| 220 | ); | |
52f3873ddigeff10 years ago | 221 | } |
| 222 | | |
ce5e88eeYuri Skorokhodov5 years ago | 223 | public prewarmBundleCache(): Promise<void> { |
259c018fYuri Skorokhodov5 years ago | 224 | return this.packager.prewarmBundleCache(PlatformType.Android); |
299b0557Patricio Beltran10 years ago | 225 | } |
| 226 | | |
cbc7ac5bArtem Egorov7 years ago | 227 | public getRunArguments(): string[] { |
8022afdfVladimir Kotikov8 years ago | 228 | let runArguments: string[] = []; |
| 229 | | |
8df5011eYuri Skorokhodov5 years ago | 230 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
8022afdfVladimir Kotikov8 years ago | 231 | runArguments = this.runOptions.runArguments; |
| 232 | } else { | |
| 233 | if (this.runOptions.variant) { | |
| 234 | runArguments.push("--variant", this.runOptions.variant); | |
| 235 | } | |
| 236 | if (this.runOptions.target) { | |
34472878RedMickey5 years ago | 237 | if ( |
| 238 | this.runOptions.target === AndroidPlatform.simulatorString || | |
| 239 | this.runOptions.target === AndroidPlatform.deviceString | |
| 240 | ) { | |
| 241 | const message = localize( | |
| 242 | "TargetIsNotSupportedForAndroid", | |
8df5011eYuri Skorokhodov5 years ago | 243 | "Target {0} is not supported for Android platform. \n If you want to use particular device or simulator for launching Android app,\n please specify device id (as in 'adb devices' output) instead.", |
34472878RedMickey5 years ago | 244 | this.runOptions.target, |
| 245 | ); | |
cbc7ac5bArtem Egorov7 years ago | 246 | this.logger.warning(message); |
| 247 | } else { | |
| 248 | runArguments.push("--deviceId", this.runOptions.target); | |
| 249 | } | |
8022afdfVladimir Kotikov8 years ago | 250 | } |
| 251 | } | |
| 252 | | |
| 253 | return runArguments; | |
| 254 | } | |
| 255 | | |
34472878RedMickey5 years ago | 256 | public dispose(): void { |
8df5011eYuri Skorokhodov5 years ago | 257 | if (this.logCatMonitor) { |
| 258 | LogCatMonitorManager.delMonitor(this.logCatMonitor.deviceId); | |
| 259 | this.logCatMonitor = null; | |
| 260 | } | |
| 261 | } | |
| 262 | | |
ce5e88eeYuri Skorokhodov5 years ago | 263 | private initializeTargetDevicesAndPackageName(): Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 264 | return this.adbHelper.getConnectedDevices().then(devices => { |
52f3873ddigeff10 years ago | 265 | this.devices = devices; |
| 266 | this.debugTarget = this.getTargetEmulator(devices); | |
| 267 | return this.getPackageName().then(packageName => { | |
| 268 | this.packageName = packageName; | |
| 269 | }); | |
| 270 | }); | |
| 271 | } | |
| 272 | | |
ce5e88eeYuri Skorokhodov5 years ago | 273 | private launchAppWithADBReverseAndLogCat(device: IDevice): Promise<void> { |
8df5011eYuri Skorokhodov5 years ago | 274 | return this.configureADBReverseWhenApplicable(device) |
52f3873ddigeff10 years ago | 275 | .then(() => { |
| 276 | return this.needsToLaunchApps | |
34472878RedMickey5 years ago | 277 | ? this.adbHelper.launchApp( |
| 278 | this.runOptions.projectRoot, | |
| 279 | this.packageName, | |
| 280 | device.id, | |
| 281 | ) | |
ce5e88eeYuri Skorokhodov5 years ago | 282 | : Promise.resolve(); |
| 283 | }) | |
| 284 | .then(() => { | |
0a68f8dbArtem Egorov8 years ago | 285 | return this.startMonitoringLogCat(device, this.runOptions.logCatArguments); |
52f3873ddigeff10 years ago | 286 | }); |
| 287 | } | |
| 288 | | |
ce5e88eeYuri Skorokhodov5 years ago | 289 | private configureADBReverseWhenApplicable(device: IDevice): Promise<void> { |
34472878RedMickey5 years ago | 290 | return Promise.resolve() // For other emulators and devices we try to enable adb reverse |
db6fd42aRuslan Bikkinin7 years ago | 291 | .then(() => this.adbHelper.apiVersion(device.id)) |
b57ea017Artem Egorov8 years ago | 292 | .then(apiVersion => { |
34472878RedMickey5 years ago | 293 | if (apiVersion >= AndroidAPILevel.LOLLIPOP) { |
| 294 | // If we support adb reverse | |
| 295 | return this.adbHelper.reverseAdb( | |
| 296 | device.id, | |
| 297 | Number(this.runOptions.packagerPort), | |
| 298 | ); | |
b57ea017Artem Egorov8 years ago | 299 | } else { |
34472878RedMickey5 years ago | 300 | const message = localize( |
| 301 | "DeviceSupportsOnlyAPILevel", | |
8df5011eYuri Skorokhodov5 years ago | 302 | "Device {0} supports only API Level {1}. \n Level {2} is needed to support port forwarding via adb reverse. \n For debugging to work you'll need <Shake or press menu button> for the dev menu, \n go into <Dev Settings> and configure <Debug Server host & port for Device> to be \n an IP address of your computer that the Device can reach. More info at: \n https://facebook.github.io/react-native/docs/debugging.html#debugging-react-native-apps", |
34472878RedMickey5 years ago | 303 | device.id, |
| 304 | apiVersion, | |
| 305 | AndroidAPILevel.LOLLIPOP, | |
| 306 | ); | |
d7d405aeYuri Skorokhodov7 years ago | 307 | this.logger.warning(message); |
b57ea017Artem Egorov8 years ago | 308 | return void 0; |
| 309 | } | |
| 310 | }); | |
52f3873ddigeff10 years ago | 311 | } |
| 312 | | |
ce5e88eeYuri Skorokhodov5 years ago | 313 | private getPackageName(): Promise<string> { |
34472878RedMickey5 years ago | 314 | return new Package(this.runOptions.projectRoot) |
| 315 | .name() | |
| 316 | .then(appName => | |
| 317 | new PackageNameResolver(appName).resolvePackageName(this.runOptions.projectRoot), | |
| 318 | ); | |
52f3873ddigeff10 years ago | 319 | } |
| 320 | | |
| 321 | /** | |
| 322 | * Returns the target emulator, using the following logic: | |
| 323 | * * If an emulator is specified and it is connected, use that one. | |
| 324 | * * Otherwise, use the first one in the list. | |
| 325 | */ | |
| 326 | private getTargetEmulator(devices: IDevice[]): IDevice { | |
| 327 | let activeFilterFunction = (device: IDevice) => { | |
| 328 | return device.isOnline; | |
| 329 | }; | |
| 330 | | |
| 331 | let targetFilterFunction = (device: IDevice) => { | |
| 332 | return device.id === this.runOptions.target && activeFilterFunction(device); | |
| 333 | }; | |
| 334 | | |
| 335 | if (this.runOptions && this.runOptions.target && devices) { | |
| 336 | /* check if the specified target is active */ | |
| 337 | const targetDevice = devices.find(targetFilterFunction); | |
| 338 | if (targetDevice) { | |
| 339 | return targetDevice; | |
| 340 | } | |
| 341 | } | |
| 342 | | |
| 343 | /* return the first active device in the list */ | |
| 344 | let activeDevices = devices && devices.filter(activeFilterFunction); | |
| 345 | return activeDevices && activeDevices[0]; | |
| 346 | } | |
| 347 | | |
8df5011eYuri Skorokhodov5 years ago | 348 | private startMonitoringLogCat(device: IDevice, logCatArguments: string[]): void { |
| 349 | LogCatMonitorManager.delMonitor(device.id); // Stop previous logcat monitor if it's running | |
0a68f8dbArtem Egorov8 years ago | 350 | |
| 351 | // this.logCatMonitor can be mutated, so we store it locally too | |
8df5011eYuri Skorokhodov5 years ago | 352 | this.logCatMonitor = new LogCatMonitor(device.id, this.adbHelper, logCatArguments); |
| 353 | LogCatMonitorManager.addMonitor(this.logCatMonitor); | |
34472878RedMickey5 years ago | 354 | this.logCatMonitor |
| 355 | .start() // The LogCat will continue running forever, so we don't wait for it | |
| 356 | .catch(error => { | |
| 357 | this.logger.warning(error); | |
| 358 | this.logger.warning( | |
| 359 | localize("ErrorWhileMonitoringLogCat", "Error while monitoring LogCat"), | |
| 360 | ); | |
| 361 | }); // The LogCatMonitor failing won't stop the debugging experience | |
0a68f8dbArtem Egorov8 years ago | 362 | } |
ef902673Vladimir Kotikov9 years ago | 363 | } |