File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff 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
2424function 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
Original file line number Diff line number Diff 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.
97103async function cleanupOldApks ( config : HtkConfig ) {
98104 const apks = await getAllLocalApks ( config ) ;
You can’t perform that action at this time.
0 commit comments