Skip to content

Commit 543c522

Browse files
Merge pull request #5207 from NativeScript/vladimirov/improve-socket-error
fix: improve message for Android socket connection error
2 parents 188bc75 + 626433d commit 543c522

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/services/livesync/android-livesync-tool.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
7575
abstractPort: `localabstract:${configuration.appIdentifier}-livesync`
7676
});
7777

78-
const connectionResult = await this.connectEventuallyUntilTimeout(this.createSocket.bind(this, port), connectTimeout);
78+
const connectionResult = await this.connectEventuallyUntilTimeout(this.createSocket.bind(this, port), connectTimeout, configuration);
7979
this.handleConnection(connectionResult);
8080
}
8181

@@ -299,19 +299,33 @@ export class AndroidLivesyncTool implements IAndroidLivesyncTool {
299299
});
300300
}
301301

302-
private connectEventuallyUntilTimeout(factory: () => ILiveSyncSocket, timeout: number): Promise<{ socket: ILiveSyncSocket, data: Buffer | string }> {
302+
private connectEventuallyUntilTimeout(factory: () => ILiveSyncSocket, timeout: number, configuration: IAndroidLivesyncToolConfiguration): Promise<{ socket: ILiveSyncSocket, data: Buffer | string }> {
303303
return new Promise((resolve, reject) => {
304304
let lastKnownError: Error | string,
305305
isConnected = false;
306306

307-
const connectionTimer = setTimeout(() => {
307+
const connectionTimer = setTimeout(async () => {
308308
if (!isConnected) {
309309
isConnected = true;
310310
if (this.pendingConnectionData && this.pendingConnectionData.socketTimer) {
311311
clearTimeout(this.pendingConnectionData.socketTimer);
312312
}
313313

314-
reject(lastKnownError || new Error(AndroidLivesyncTool.SOCKET_CONNECTION_TIMED_OUT_ERROR));
314+
const applicationPid = await this.$androidProcessService.getAppProcessId(configuration.deviceIdentifier, configuration.appIdentifier);
315+
if (!applicationPid) {
316+
this.$logger.trace("In Android LiveSync tool, lastKnownError is: ", lastKnownError);
317+
this.$logger.info(`Application ${configuration.appIdentifier} is not running on device ${configuration.deviceIdentifier}.`.yellow);
318+
this.$logger.info(
319+
`This issue may be caused by:
320+
* crash at startup (try \`tns debug android --debug-brk\` to check why it crashes)
321+
* different application identifier in your package.json and in your gradle files (check your identifier in \`package.json\` and in all *.gradle files in your App_Resources directory)
322+
* device is locked
323+
* manual closing of the application`.cyan);
324+
reject(new Error(`Application ${configuration.appIdentifier} is not running`));
325+
} else {
326+
reject(lastKnownError || new Error(AndroidLivesyncTool.SOCKET_CONNECTION_TIMED_OUT_ERROR));
327+
}
328+
315329
this.pendingConnectionData = null;
316330
}
317331
}, timeout);

test/services/livesync/android-livesync-tool.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ const createTestInjector = (socket: INetSocket, fileStreams: IDictionary<NodeJS.
7070
testInjector.register("injector", testInjector);
7171
testInjector.register("mobileHelper", MobileHelper);
7272
testInjector.register("androidProcessService", {
73-
forwardFreeTcpToAbstractPort: () => Promise.resolve("")
73+
forwardFreeTcpToAbstractPort: () => Promise.resolve(""),
74+
getAppProcessId: () => Promise.resolve("1234")
7475
});
7576
testInjector.register("LiveSyncSocket", () => socket);
7677
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);

0 commit comments

Comments
 (0)