microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/androidPlatform.ts
282lines · 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 | |
5c8365a6Artem Egorov8 years ago | 6 | import {GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform"; |
259c018fYuri Skorokhodov5 years ago | 7 | import {IAndroidRunOptions, PlatformType} from "../launchArgs"; |
7daed3fcArtem Egorov8 years ago | 8 | import {AdbHelper, AndroidAPILevel, IDevice} from "./adb"; |
0a68f8dbArtem Egorov8 years ago | 9 | import {Package} from "../../common/node/package"; |
5c8365a6Artem Egorov8 years ago | 10 | import {PackageNameResolver} from "./packageNameResolver"; |
0a68f8dbArtem Egorov8 years ago | 11 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
| 12 | import {TelemetryHelper} from "../../common/telemetryHelper"; | |
8022afdfVladimir Kotikov8 years ago | 13 | import {CommandExecutor} from "../../common/commandExecutor"; |
0a68f8dbArtem Egorov8 years ago | 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"; |
2d8af448Yuri Skorokhodov6 years ago | 21 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
d7d405aeYuri Skorokhodov7 years ago | 22 | const localize = nls.loadMessageBundle(); |
5c8365a6Artem Egorov8 years ago | 23 | |
52f3873ddigeff10 years ago | 24 | /** |
| 25 | * Android specific platform implementation for debugging RN applications. | |
| 26 | */ | |
299b0557Patricio Beltran10 years ago | 27 | export class AndroidPlatform extends GeneralMobilePlatform { |
52f3873ddigeff10 years ago | 28 | |
0a68f8dbArtem Egorov8 years ago | 29 | // We should add the common Android build/run errors we find to this list |
ef902673Vladimir Kotikov9 years ago | 30 | private static RUN_ANDROID_FAILURE_PATTERNS: PatternToFailure[] = [{ |
| 31 | pattern: "Failed to install on any devices", | |
d7d405aeYuri Skorokhodov7 years ago | 32 | errorCode: InternalErrorCode.AndroidCouldNotInstallTheAppOnAnyAvailibleDevice, |
ef902673Vladimir Kotikov9 years ago | 33 | }, { |
| 34 | pattern: "com.android.ddmlib.ShellCommandUnresponsiveException", | |
d7d405aeYuri Skorokhodov7 years ago | 35 | errorCode: InternalErrorCode.AndroidShellCommandTimedOut, |
ef902673Vladimir Kotikov9 years ago | 36 | }, { |
| 37 | pattern: "Android project not found", | |
d7d405aeYuri Skorokhodov7 years ago | 38 | errorCode: InternalErrorCode.AndroidProjectNotFound, |
ef902673Vladimir Kotikov9 years ago | 39 | |
| 40 | }, { | |
| 41 | pattern: "error: more than one device/emulator", | |
d7d405aeYuri Skorokhodov7 years ago | 42 | errorCode: InternalErrorCode.AndroidMoreThanOneDeviceOrEmulator, |
ef902673Vladimir Kotikov9 years ago | 43 | }, { |
| 44 | pattern: /^Error: Activity class \{.*\} does not exist\.$/m, | |
d7d405aeYuri Skorokhodov7 years ago | 45 | errorCode: InternalErrorCode.AndroidFailedToLaunchTheSpecifiedActivity, |
ef902673Vladimir Kotikov9 years ago | 46 | }]; |
52f3873ddigeff10 years ago | 47 | |
| 48 | private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"]; | |
| 49 | | |
| 50 | private debugTarget: IDevice; | |
| 51 | private devices: IDevice[]; | |
| 52 | private packageName: string; | |
0a68f8dbArtem Egorov8 years ago | 53 | private logCatMonitor: LogCatMonitor | null = null; |
db6fd42aRuslan Bikkinin7 years ago | 54 | private adbHelper: AdbHelper; |
68a5b8d5JiglioNero5 years ago | 55 | private emulatorManager: AndroidEmulatorManager; |
52f3873ddigeff10 years ago | 56 | |
| 57 | private needsToLaunchApps: boolean = false; | |
db6fd42aRuslan Bikkinin7 years ago | 58 | |
ce5e88eeYuri Skorokhodov5 years ago | 59 | public showDevMenu(deviceId?: string): Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 60 | return this.adbHelper.showDevMenu(deviceId); |
7daed3fcArtem Egorov8 years ago | 61 | } |
db6fd42aRuslan Bikkinin7 years ago | 62 | |
ce5e88eeYuri Skorokhodov5 years ago | 63 | public reloadApp(deviceId?: string): Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 64 | return this.adbHelper.reloadApp(deviceId); |
7daed3fcArtem Egorov8 years ago | 65 | } |
52f3873ddigeff10 years ago | 66 | |
299b0557Patricio Beltran10 years ago | 67 | // 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 | 68 | constructor(protected runOptions: IAndroidRunOptions, platformDeps: MobilePlatformDeps = {}) { |
0a68f8dbArtem Egorov8 years ago | 69 | super(runOptions, platformDeps); |
db6fd42aRuslan Bikkinin7 years ago | 70 | this.adbHelper = new AdbHelper(this.runOptions.projectRoot, this.logger); |
68a5b8d5JiglioNero5 years ago | 71 | this.emulatorManager = new AndroidEmulatorManager(this.adbHelper); |
db6fd42aRuslan Bikkinin7 years ago | 72 | } |
43e1a996Ruslan Bikkinin7 years ago | 73 | |
db6fd42aRuslan Bikkinin7 years ago | 74 | // TODO: remove this method when sinon will be updated to upper version. Now it is used for tests only. |
| 75 | public setAdbHelper(adbHelper: AdbHelper) { | |
| 76 | this.adbHelper = adbHelper; | |
52f3873ddigeff10 years ago | 77 | } |
| 78 | | |
68a5b8d5JiglioNero5 years ago | 79 | public resolveVirtualDevice(target: string): Promise<IAndroidEmulator | null> { |
| 80 | if (!target.includes("device")) { | |
| 81 | return this.emulatorManager.startEmulator(target) | |
| 82 | .then((emulator: IAndroidEmulator | null) => { | |
| 83 | if (emulator) { | |
| 84 | GeneralMobilePlatform.setRunArgument(this.runArguments, "--deviceId", emulator.id); | |
| 85 | } | |
| 86 | return emulator; | |
| 87 | }); | |
| 88 | } | |
| 89 | else { | |
| 90 | return Promise.resolve(null); | |
| 91 | } | |
| 92 | } | |
| 93 | | |
ce5e88eeYuri Skorokhodov5 years ago | 94 | public runApp(shouldLaunchInAllDevices: boolean = false): Promise<void> { |
549baae2RedMickey6 years ago | 95 | let extProps: any = { |
031832ffArtem Egorov8 years ago | 96 | platform: { |
259c018fYuri Skorokhodov5 years ago | 97 | value: PlatformType.Android, |
031832ffArtem Egorov8 years ago | 98 | isPii: false, |
| 99 | }, | |
| 100 | }; | |
| 101 | | |
549baae2RedMickey6 years ago | 102 | if (this.runOptions.isDirect) { |
| 103 | extProps.isDirect = { | |
| 104 | value: true, | |
| 105 | isPii: false, | |
| 106 | }; | |
| 107 | } | |
| 108 | | |
7fa90b3bRedMickey6 years ago | 109 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps); |
ba953e9fRedMickey6 years ago | 110 | |
031832ffArtem Egorov8 years ago | 111 | return TelemetryHelper.generate("AndroidPlatform.runApp", extProps, () => { |
de838bbfJiglioNero6 years ago | 112 | const env = GeneralMobilePlatform.getEnvArgument(process.env, this.runOptions.env, this.runOptions.envFile); |
78c2b4deRedMickey6 years ago | 113 | |
e3706a1cRedMickey6 years ago | 114 | if ( |
| 115 | !semver.valid(this.runOptions.reactNativeVersions.reactNativeVersion) /*Custom RN implementations should support this flag*/ || | |
| 116 | semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, AndroidPlatform.NO_PACKAGER_VERSION) | |
| 117 | ) { | |
| 118 | this.runArguments.push("--no-packager"); | |
| 119 | } | |
| 120 | | |
| 121 | let mainActivity = GeneralMobilePlatform.getOptFromRunArgs(this.runArguments, "--main-activity"); | |
| 122 | | |
| 123 | if (mainActivity) { | |
| 124 | this.adbHelper.setLaunchActivity(mainActivity); | |
| 125 | } else if (!isNullOrUndefined(this.runOptions.debugLaunchActivity)) { | |
| 126 | this.runArguments.push("--main-activity", this.runOptions.debugLaunchActivity); | |
| 127 | this.adbHelper.setLaunchActivity(this.runOptions.debugLaunchActivity); | |
| 128 | } | |
| 129 | | |
| 130 | const runAndroidSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-android", this.runArguments, {env}); | |
| 131 | const output = new OutputVerifier( | |
| 132 | () => | |
ce5e88eeYuri Skorokhodov5 years ago | 133 | Promise.resolve(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS), |
e3706a1cRedMickey6 years ago | 134 | () => |
ce5e88eeYuri Skorokhodov5 years ago | 135 | Promise.resolve(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS), |
259c018fYuri Skorokhodov5 years ago | 136 | PlatformType.Android).process(runAndroidSpawn); |
e3706a1cRedMickey6 years ago | 137 | |
| 138 | return output | |
| 139 | .finally(() => { | |
| 140 | return this.initializeTargetDevicesAndPackageName(); | |
| 141 | }).then(() => [this.debugTarget], reason => { | |
| 142 | if (reason.message === ErrorHelper.getInternalError(InternalErrorCode.AndroidMoreThanOneDeviceOrEmulator).message && this.devices.length > 1 && this.debugTarget) { | |
| 143 | /* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */ | |
| 144 | this.needsToLaunchApps = true; | |
| 145 | return shouldLaunchInAllDevices | |
| 146 | ? this.adbHelper.getOnlineDevices() | |
ce5e88eeYuri Skorokhodov5 years ago | 147 | : Promise.resolve([this.debugTarget]); |
e3706a1cRedMickey6 years ago | 148 | } else { |
ce5e88eeYuri Skorokhodov5 years ago | 149 | return Promise.reject<IDevice[]>(reason); |
78c2b4deRedMickey6 years ago | 150 | } |
e3706a1cRedMickey6 years ago | 151 | }).then(devices => { |
| 152 | return new PromiseUtil().forEach(devices, device => { | |
| 153 | return this.launchAppWithADBReverseAndLogCat(device); | |
| 154 | }); | |
52f3873ddigeff10 years ago | 155 | }); |
| 156 | }); | |
| 157 | } | |
| 158 | | |
ce5e88eeYuri Skorokhodov5 years ago | 159 | public enableJSDebuggingMode(): Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 160 | return this.adbHelper.switchDebugMode(this.runOptions.projectRoot, this.packageName, true, this.debugTarget.id); |
b57ea017Artem Egorov8 years ago | 161 | } |
| 162 | | |
ce5e88eeYuri Skorokhodov5 years ago | 163 | public disableJSDebuggingMode(): Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 164 | return this.adbHelper.switchDebugMode(this.runOptions.projectRoot, this.packageName, false, this.debugTarget.id); |
52f3873ddigeff10 years ago | 165 | } |
| 166 | | |
ce5e88eeYuri Skorokhodov5 years ago | 167 | public prewarmBundleCache(): Promise<void> { |
259c018fYuri Skorokhodov5 years ago | 168 | return this.packager.prewarmBundleCache(PlatformType.Android); |
299b0557Patricio Beltran10 years ago | 169 | } |
| 170 | | |
cbc7ac5bArtem Egorov7 years ago | 171 | public getRunArguments(): string[] { |
8022afdfVladimir Kotikov8 years ago | 172 | let runArguments: string[] = []; |
| 173 | | |
| 174 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { | |
| 175 | runArguments = this.runOptions.runArguments; | |
| 176 | } else { | |
| 177 | if (this.runOptions.variant) { | |
| 178 | runArguments.push("--variant", this.runOptions.variant); | |
| 179 | } | |
| 180 | if (this.runOptions.target) { | |
cbc7ac5bArtem Egorov7 years ago | 181 | if (this.runOptions.target === AndroidPlatform.simulatorString || |
| 182 | this.runOptions.target === AndroidPlatform.deviceString) { | |
| 183 | | |
d7d405aeYuri Skorokhodov7 years ago | 184 | const message = localize("TargetIsNotSupportedForAndroid", |
de838bbfJiglioNero6 years ago | 185 | "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.", |
d7d405aeYuri Skorokhodov7 years ago | 186 | this.runOptions.target); |
cbc7ac5bArtem Egorov7 years ago | 187 | this.logger.warning(message); |
| 188 | } else { | |
| 189 | runArguments.push("--deviceId", this.runOptions.target); | |
| 190 | } | |
8022afdfVladimir Kotikov8 years ago | 191 | } |
| 192 | } | |
| 193 | | |
| 194 | return runArguments; | |
| 195 | } | |
| 196 | | |
ce5e88eeYuri Skorokhodov5 years ago | 197 | private initializeTargetDevicesAndPackageName(): Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 198 | return this.adbHelper.getConnectedDevices().then(devices => { |
52f3873ddigeff10 years ago | 199 | this.devices = devices; |
| 200 | this.debugTarget = this.getTargetEmulator(devices); | |
| 201 | return this.getPackageName().then(packageName => { | |
| 202 | this.packageName = packageName; | |
| 203 | }); | |
| 204 | }); | |
| 205 | } | |
| 206 | | |
ce5e88eeYuri Skorokhodov5 years ago | 207 | private launchAppWithADBReverseAndLogCat(device: IDevice): Promise<void> { |
| 208 | return this.configureADBReverseWhenApplicable(device) | |
52f3873ddigeff10 years ago | 209 | .then(() => { |
| 210 | return this.needsToLaunchApps | |
db6fd42aRuslan Bikkinin7 years ago | 211 | ? this.adbHelper.launchApp(this.runOptions.projectRoot, this.packageName, device.id) |
ce5e88eeYuri Skorokhodov5 years ago | 212 | : Promise.resolve(); |
| 213 | }) | |
| 214 | .then(() => { | |
0a68f8dbArtem Egorov8 years ago | 215 | return this.startMonitoringLogCat(device, this.runOptions.logCatArguments); |
52f3873ddigeff10 years ago | 216 | }); |
| 217 | } | |
| 218 | | |
ce5e88eeYuri Skorokhodov5 years ago | 219 | private configureADBReverseWhenApplicable(device: IDevice): Promise<void> { |
| 220 | return Promise.resolve()// For other emulators and devices we try to enable adb reverse | |
db6fd42aRuslan Bikkinin7 years ago | 221 | .then(() => this.adbHelper.apiVersion(device.id)) |
b57ea017Artem Egorov8 years ago | 222 | .then(apiVersion => { |
| 223 | if (apiVersion >= AndroidAPILevel.LOLLIPOP) { // If we support adb reverse | |
db6fd42aRuslan Bikkinin7 years ago | 224 | return this.adbHelper.reverseAdb(device.id, Number(this.runOptions.packagerPort)); |
b57ea017Artem Egorov8 years ago | 225 | } else { |
d7d405aeYuri Skorokhodov7 years ago | 226 | const message = localize("DeviceSupportsOnlyAPILevel", |
| 227 | "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", | |
| 228 | device.id, apiVersion, AndroidAPILevel.LOLLIPOP); | |
| 229 | this.logger.warning(message); | |
b57ea017Artem Egorov8 years ago | 230 | return void 0; |
| 231 | } | |
| 232 | }); | |
52f3873ddigeff10 years ago | 233 | } |
| 234 | | |
ce5e88eeYuri Skorokhodov5 years ago | 235 | private getPackageName(): Promise<string> { |
8022afdfVladimir Kotikov8 years ago | 236 | return new Package(this.runOptions.projectRoot).name().then(appName => |
52f3873ddigeff10 years ago | 237 | new PackageNameResolver(appName).resolvePackageName(this.runOptions.projectRoot)); |
| 238 | } | |
| 239 | | |
| 240 | /** | |
| 241 | * Returns the target emulator, using the following logic: | |
| 242 | * * If an emulator is specified and it is connected, use that one. | |
| 243 | * * Otherwise, use the first one in the list. | |
| 244 | */ | |
| 245 | private getTargetEmulator(devices: IDevice[]): IDevice { | |
| 246 | let activeFilterFunction = (device: IDevice) => { | |
| 247 | return device.isOnline; | |
| 248 | }; | |
| 249 | | |
| 250 | let targetFilterFunction = (device: IDevice) => { | |
| 251 | return device.id === this.runOptions.target && activeFilterFunction(device); | |
| 252 | }; | |
| 253 | | |
| 254 | if (this.runOptions && this.runOptions.target && devices) { | |
| 255 | /* check if the specified target is active */ | |
| 256 | const targetDevice = devices.find(targetFilterFunction); | |
| 257 | if (targetDevice) { | |
| 258 | return targetDevice; | |
| 259 | } | |
| 260 | } | |
| 261 | | |
| 262 | /* return the first active device in the list */ | |
| 263 | let activeDevices = devices && devices.filter(activeFilterFunction); | |
| 264 | return activeDevices && activeDevices[0]; | |
| 265 | } | |
| 266 | | |
0a68f8dbArtem Egorov8 years ago | 267 | private startMonitoringLogCat(device: IDevice, logCatArguments: string): void { |
| 268 | this.stopMonitoringLogCat(); // Stop previous logcat monitor if it's running | |
| 269 | | |
| 270 | // this.logCatMonitor can be mutated, so we store it locally too | |
db6fd42aRuslan Bikkinin7 years ago | 271 | this.logCatMonitor = new LogCatMonitor(device.id, logCatArguments, this.adbHelper); |
0a68f8dbArtem Egorov8 years ago | 272 | this.logCatMonitor.start() // The LogCat will continue running forever, so we don't wait for it |
ce5e88eeYuri Skorokhodov5 years ago | 273 | .catch(error => this.logger.warning(localize("ErrorWhileMonitoringLogCat", "Error while monitoring LogCat"), error)); // The LogCatMonitor failing won't stop the debugging experience |
0a68f8dbArtem Egorov8 years ago | 274 | } |
| 275 | | |
| 276 | private stopMonitoringLogCat(): void { | |
| 277 | if (this.logCatMonitor) { | |
| 278 | this.logCatMonitor.dispose(); | |
| 279 | this.logCatMonitor = null; | |
| 280 | } | |
52f3873ddigeff10 years ago | 281 | } |
ef902673Vladimir Kotikov9 years ago | 282 | } |