microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8a67e140aee58da792b1e9c1c17358a98d5a3704

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/android/androidPlatform.ts

194lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import * as Q from "q";
5
6import {GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform";
7import {Packager} from "../packager";
8import {IRunOptions} from "../launchArgs";
9import {Log} from "../log/log";
10import {IAdb, Adb, AndroidAPILevel, IDevice, DeviceType} from "./adb";
11import {Package} from "../node/package";
12import {PromiseUtil} from "../node/promise";
13import {PackageNameResolver} from "./packageNameResolver";
14import {OutputVerifier, PatternToFailure} from "../outputVerifier";
15import {FileSystem} from "../node/fileSystem";
16import {IReactNative, ReactNative} from "../reactNative";
17import {TelemetryHelper} from "../telemetryHelper";
18
19export interface AndroidPlatformDeps extends MobilePlatformDeps {
20 adb?: IAdb;
21 reactNative?: IReactNative;
22 fileSystem?: FileSystem;
23}
24/**
25 * Android specific platform implementation for debugging RN applications.
26 */
27export class AndroidPlatform extends GeneralMobilePlatform {
28 private static MULTIPLE_DEVICES_ERROR = "error: more than one device/emulator";
29
30 // We should add the common Android build/run erros we find to this list
31 private static RUN_ANDROID_FAILURE_PATTERNS: PatternToFailure[] = [{
32 pattern: "Failed to install on any devices",
33 message: "Could not install the app on any available device. Make sure you have a correctly"
34 + " configured device or emulator running. See https://facebook.github.io/react-native/docs/android-setup.html",
35 }, {
36 pattern: "com.android.ddmlib.ShellCommandUnresponsiveException",
37 message: "An Android shell command timed-out. Please retry the operation.",
38 }, {
39 pattern: "Android project not found",
40 message: "Android project not found.",
41
42 }, {
43 pattern: "error: more than one device/emulator",
44 message: AndroidPlatform.MULTIPLE_DEVICES_ERROR,
45 }, {
46 pattern: /^Error: Activity class \{.*\} does not exist\.$/m,
47 message: "Failed to launch the specified activity. Try running application manually and "
48 + "start debugging using 'Attach to packager' launch configuration.",
49 }];
50
51 private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"];
52
53 private debugTarget: IDevice;
54 private devices: IDevice[];
55 private packageName: string;
56 private adb: IAdb;
57 private reactNative: IReactNative;
58 private fileSystem: FileSystem;
59
60 private needsToLaunchApps: boolean = false;
61
62 // 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.
63 constructor(runOptions: IRunOptions, {
64 remoteExtension,
65 adb = <IAdb>new Adb(),
66 reactNative = <IReactNative>new ReactNative(),
67 fileSystem = new FileSystem(),
68 }: AndroidPlatformDeps = {}) {
69 super(runOptions, { remoteExtension: remoteExtension });
70 this.adb = adb;
71 this.reactNative = reactNative;
72 this.fileSystem = fileSystem;
73 }
74
75 public runApp(shouldLaunchInAllDevices: boolean = false): Q.Promise<void> {
76 return TelemetryHelper.generate("AndroidPlatform.runApp", () => {
77 const runAndroidSpawn = this.reactNative.runAndroid(this.runOptions.projectRoot, this.runOptions.variant, this.runOptions.target);
78 const output = new OutputVerifier(
79 () =>
80 Q(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS),
81 () =>
82 Q(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS)).process(runAndroidSpawn);
83
84 return output
85 .finally(() => {
86 return this.initializeTargetDevicesAndPackageName();
87 }).then(() => [this.debugTarget], reason => {
88 if (reason.message === AndroidPlatform.MULTIPLE_DEVICES_ERROR && this.devices.length > 1 && this.debugTarget) {
89 /* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */
90 this.needsToLaunchApps = true;
91 return shouldLaunchInAllDevices
92 ? this.adb.getOnlineDevices()
93 : Q([this.debugTarget]);
94 } else {
95 return Q.reject<IDevice[]>(reason);
96 }
97 }).then(devices => {
98 return new PromiseUtil().forEach(devices, device => {
99 return this.launchAppWithADBReverseAndLogCat(device);
100 });
101 });
102 });
103 }
104
105 public enableJSDebuggingMode(): Q.Promise<void> {
106 return this.adb.reloadAppInDebugMode(this.runOptions.projectRoot, this.packageName, this.debugTarget.id);
107 }
108
109 public prewarmBundleCache(): Q.Promise<void> {
110 return this.remoteExtension.prewarmBundleCache(this.platformName);
111 }
112
113 private initializeTargetDevicesAndPackageName(): Q.Promise<void> {
114 return this.adb.getConnectedDevices().then(devices => {
115 this.devices = devices;
116 this.debugTarget = this.getTargetEmulator(devices);
117 return this.getPackageName().then(packageName => {
118 this.packageName = packageName;
119 });
120 });
121 }
122
123 private launchAppWithADBReverseAndLogCat(device: IDevice): Q.Promise<void> {
124 return Q({})
125 .then(() => {
126 return this.configureADBReverseWhenApplicable(device);
127 }).then(() => {
128 return this.needsToLaunchApps
129 ? this.adb.launchApp(this.runOptions.projectRoot, this.packageName, device.id)
130 : Q<void>(void 0);
131 }).then(() => {
132 return this.startMonitoringLogCat(device, this.runOptions.logCatArguments).catch(error => // The LogCatMonitor failing won't stop the debugging experience
133 Log.logWarning("Couldn't start LogCat monitor", error));
134 });
135 }
136
137 private configureADBReverseWhenApplicable(device: IDevice): Q.Promise<void> {
138 if (device.type !== DeviceType.AndroidSdkEmulator) {
139 return Q({}) // For other emulators and devices we try to enable adb reverse
140 .then(() => this.adb.apiVersion(device.id))
141 .then(apiVersion => {
142 if (apiVersion >= AndroidAPILevel.LOLLIPOP) { // If we support adb reverse
143 return this.adb.reverseAdd(device.id, Packager.DEFAULT_PORT.toString(), this.runOptions.packagerPort);
144 } else {
145 Log.logWarning(`Device ${device.id} supports only API Level ${apiVersion}. `
146 + `Level ${AndroidAPILevel.LOLLIPOP} is needed to support port forwarding via adb reverse. `
147 + "For debugging to work you'll need <Shake or press menu button> for the dev menu, "
148 + "go into <Dev Settings> and configure <Debug Server host & port for Device> to be "
149 + "an IP address of your computer that the Device can reach. More info at: "
150 + "https://facebook.github.io/react-native/docs/debugging.html#debugging-react-native-apps");
151 return void 0;
152 }
153 });
154 } else {
155 return Q<void>(void 0); // Android SDK emulators can connect directly to 10.0.0.2, so they don't need port forwarding
156 }
157 }
158
159 private getPackageName(): Q.Promise<string> {
160 return new Package(this.runOptions.projectRoot, { fileSystem: this.fileSystem }).name().then(appName =>
161 new PackageNameResolver(appName).resolvePackageName(this.runOptions.projectRoot));
162 }
163
164 /**
165 * Returns the target emulator, using the following logic:
166 * * If an emulator is specified and it is connected, use that one.
167 * * Otherwise, use the first one in the list.
168 */
169 private getTargetEmulator(devices: IDevice[]): IDevice {
170 let activeFilterFunction = (device: IDevice) => {
171 return device.isOnline;
172 };
173
174 let targetFilterFunction = (device: IDevice) => {
175 return device.id === this.runOptions.target && activeFilterFunction(device);
176 };
177
178 if (this.runOptions && this.runOptions.target && devices) {
179 /* check if the specified target is active */
180 const targetDevice = devices.find(targetFilterFunction);
181 if (targetDevice) {
182 return targetDevice;
183 }
184 }
185
186 /* return the first active device in the list */
187 let activeDevices = devices && devices.filter(activeFilterFunction);
188 return activeDevices && activeDevices[0];
189 }
190
191 private startMonitoringLogCat(device: IDevice, logCatArguments: string): Q.Promise<void> {
192 return this.remoteExtension.startMonitoringLogcat(device.id, logCatArguments);
193 }
194}
195