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