Skip to content

Commit edd2208

Browse files
committed
Handle ADB app install failures, reset APKs, and retry
1 parent e36fabb commit edd2208

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/interceptors/android/android-adb-interceptor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
hasCertInstalled,
2020
bringToFront
2121
} from './adb-commands';
22-
import { streamLatestApk } from './fetch-apk';
22+
import { streamLatestApk, clearAllApks } from './fetch-apk';
2323

2424
function urlSafeBase64(content: string) {
2525
return Buffer.from(content, 'utf8').toString('base64')
@@ -78,8 +78,15 @@ export class AndroidAdbInterceptor implements Interceptor {
7878

7979
if (!(await this.adbClient.isInstalled(options.deviceId, 'tech.httptoolkit.android.v1'))) {
8080
console.log("App not installed, installing...");
81-
let stream = await streamLatestApk(this.config);
82-
await this.adbClient.install(options.deviceId, stream);
81+
try {
82+
await this.adbClient.install(options.deviceId, await streamLatestApk(this.config));
83+
} catch (e) {
84+
console.log("Resetting & retrying APK install, after initial failure:", e);
85+
// This can fail due to connection issues (with the device or while downloading
86+
// the APK) due to a corrupted APK. Reset the APKs and try again, just in case.
87+
await clearAllApks(this.config);
88+
await this.adbClient.install(options.deviceId, await streamLatestApk(this.config));
89+
}
8390
console.log("App installed successfully");
8491
}
8592

src/interceptors/android/fetch-apk.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ async function updateLocalApk(
9393
await cleanupOldApks(config);
9494
}
9595

96+
export async function clearAllApks(config: HtkConfig) {
97+
const apks = await getAllLocalApks(config);
98+
console.log(`Deleting all APKs: ${apks.map(apk => apk.path).join(', ')}`);
99+
return Promise.all(apks.map(apk => deleteFile(apk.path)));
100+
}
101+
96102
// Delete all but the most recent APK version in the config directory.
97103
async function cleanupOldApks(config: HtkConfig) {
98104
const apks = await getAllLocalApks(config);

0 commit comments

Comments
 (0)