11import { type AndroidAppLaunchOptions } from '@react-native-harness/platforms' ;
22import { spawn , SubprocessError } from '@react-native-harness/tools' ;
3+ import { withAndroidProcessEnv } from './environment.js' ;
34
45const wait = async ( ms : number ) : Promise < void > => {
56 await new Promise ( ( resolve ) => {
@@ -65,15 +66,11 @@ export const isAppInstalled = async (
6566 adbId : string ,
6667 bundleId : string
6768) : Promise < boolean > => {
68- const { stdout } = await spawn ( 'adb' , [
69- '-s' ,
70- adbId ,
71- 'shell' ,
72- 'pm' ,
73- 'list' ,
74- 'packages' ,
75- bundleId ,
76- ] ) ;
69+ const { stdout } = await spawn (
70+ 'adb' ,
71+ [ '-s' , adbId , 'shell' , 'pm' , 'list' , 'packages' , bundleId ] ,
72+ withAndroidProcessEnv ( )
73+ ) ;
7774 return stdout . trim ( ) !== '' ;
7875} ;
7976
@@ -82,20 +79,22 @@ export const reversePort = async (
8279 port : number ,
8380 hostPort : number = port
8481) : Promise < void > => {
85- await spawn ( 'adb' , [
86- '-s' ,
87- adbId ,
88- 'reverse' ,
89- `tcp:${ port } ` ,
90- `tcp:${ hostPort } ` ,
91- ] ) ;
82+ await spawn (
83+ 'adb' ,
84+ [ '-s' , adbId , 'reverse' , `tcp:${ port } ` , `tcp:${ hostPort } ` ] ,
85+ withAndroidProcessEnv ( )
86+ ) ;
9287} ;
9388
9489export const stopApp = async (
9590 adbId : string ,
9691 bundleId : string
9792) : Promise < void > => {
98- await spawn ( 'adb' , [ '-s' , adbId , 'shell' , 'am' , 'force-stop' , bundleId ] ) ;
93+ await spawn (
94+ 'adb' ,
95+ [ '-s' , adbId , 'shell' , 'am' , 'force-stop' , bundleId ] ,
96+ withAndroidProcessEnv ( )
97+ ) ;
9998} ;
10099
101100export const startApp = async (
@@ -104,15 +103,15 @@ export const startApp = async (
104103 activityName : string ,
105104 options ?: AndroidAppLaunchOptions
106105) : Promise < void > => {
107- await spawn ( 'adb' , [
108- '-s ' ,
109- adbId ,
110- ... getStartAppArgs ( bundleId , activityName , options ) ,
111- ] ) ;
106+ await spawn (
107+ 'adb ' ,
108+ [ '-s' , adbId , ... getStartAppArgs ( bundleId , activityName , options ) ] ,
109+ withAndroidProcessEnv ( )
110+ ) ;
112111} ;
113112
114113export const getDeviceIds = async ( ) : Promise < string [ ] > => {
115- const { stdout } = await spawn ( 'adb' , [ 'devices' ] ) ;
114+ const { stdout } = await spawn ( 'adb' , [ 'devices' ] , withAndroidProcessEnv ( ) ) ;
116115 return stdout
117116 . split ( '\n' )
118117 . slice ( 1 ) // Skip header
@@ -123,21 +122,23 @@ export const getDeviceIds = async (): Promise<string[]> => {
123122export const getEmulatorName = async (
124123 adbId : string
125124) : Promise < string | null > => {
126- const { stdout } = await spawn ( 'adb' , [ '-s' , adbId , 'emu' , 'avd' , 'name' ] ) ;
125+ const { stdout } = await spawn (
126+ 'adb' ,
127+ [ '-s' , adbId , 'emu' , 'avd' , 'name' ] ,
128+ withAndroidProcessEnv ( )
129+ ) ;
127130 return stdout . split ( '\n' ) [ 0 ] . trim ( ) || null ;
128131} ;
129132
130133export const getShellProperty = async (
131134 adbId : string ,
132135 property : string
133136) : Promise < string | null > => {
134- const { stdout } = await spawn ( 'adb' , [
135- '-s' ,
136- adbId ,
137- 'shell' ,
138- 'getprop' ,
139- property ,
140- ] ) ;
137+ const { stdout } = await spawn (
138+ 'adb' ,
139+ [ '-s' , adbId , 'shell' , 'getprop' , property ] ,
140+ withAndroidProcessEnv ( )
141+ ) ;
141142 return stdout . trim ( ) || null ;
142143} ;
143144
@@ -160,7 +161,7 @@ export const isBootCompleted = async (adbId: string): Promise<boolean> => {
160161} ;
161162
162163export const stopEmulator = async ( adbId : string ) : Promise < void > => {
163- await spawn ( 'adb' , [ '-s' , adbId , 'emu' , 'kill' ] ) ;
164+ await spawn ( 'adb' , [ '-s' , adbId , 'emu' , 'kill' ] , withAndroidProcessEnv ( ) ) ;
164165} ;
165166
166167export const installApp = async (
@@ -266,13 +267,11 @@ export const isAppRunning = async (
266267 bundleId : string
267268) : Promise < boolean > => {
268269 try {
269- const { stdout } = await spawn ( 'adb' , [
270- '-s' ,
271- adbId ,
272- 'shell' ,
273- 'pidof' ,
274- bundleId ,
275- ] ) ;
270+ const { stdout } = await spawn (
271+ 'adb' ,
272+ [ '-s' , adbId , 'shell' , 'pidof' , bundleId ] ,
273+ withAndroidProcessEnv ( )
274+ ) ;
276275 return stdout . trim ( ) !== '' ;
277276 } catch ( error ) {
278277 if ( error instanceof SubprocessError && error . exitCode === 1 ) {
@@ -287,15 +286,11 @@ export const getAppUid = async (
287286 adbId : string ,
288287 bundleId : string
289288) : Promise < number > => {
290- const { stdout } = await spawn ( 'adb' , [
291- '-s' ,
292- adbId ,
293- 'shell' ,
294- 'pm' ,
295- 'list' ,
296- 'packages' ,
297- '-U' ,
298- ] ) ;
289+ const { stdout } = await spawn (
290+ 'adb' ,
291+ [ '-s' , adbId , 'shell' , 'pm' , 'list' , 'packages' , '-U' ] ,
292+ withAndroidProcessEnv ( )
293+ ) ;
299294 const line = stdout
300295 . split ( '\n' )
301296 . find ( ( entry ) => entry . includes ( `package:${ bundleId } ` ) ) ;
@@ -312,33 +307,39 @@ export const setHideErrorDialogs = async (
312307 adbId : string ,
313308 hide : boolean
314309) : Promise < void > => {
315- await spawn ( 'adb' , [
316- '-s' ,
317- adbId ,
318- 'shell' ,
319- 'settings' ,
320- 'put' ,
321- 'global' ,
322- 'hide_error_dialogs' ,
323- hide ? '1' : '0' ,
324- ] ) ;
310+ await spawn (
311+ 'adb' ,
312+ [
313+ '-s' ,
314+ adbId ,
315+ 'shell' ,
316+ 'settings' ,
317+ 'put' ,
318+ 'global' ,
319+ 'hide_error_dialogs' ,
320+ hide ? '1' : '0' ,
321+ ] ,
322+ withAndroidProcessEnv ( )
323+ ) ;
325324} ;
326325
327326export const getLogcatTimestamp = async ( adbId : string ) : Promise < string > => {
328- const { stdout } = await spawn ( 'adb' , [
329- '-s' ,
330- adbId ,
331- 'shell' ,
332- 'date' ,
333- "+'%m-%d %H:%M:%S.000'" ,
334- ] ) ;
327+ const { stdout } = await spawn (
328+ 'adb' ,
329+ [ '-s' , adbId , 'shell' , 'date' , "+'%m-%d %H:%M:%S.000'" ] ,
330+ withAndroidProcessEnv ( )
331+ ) ;
335332
336333 return stdout . trim ( ) . replace ( / ^ ' + | ' + $ / g, '' ) ;
337334} ;
338335
339336export const getAvds = async ( ) : Promise < string [ ] > => {
340337 try {
341- const { stdout } = await spawn ( 'emulator' , [ '-list-avds' ] ) ;
338+ const { stdout } = await spawn (
339+ 'emulator' ,
340+ [ '-list-avds' ] ,
341+ withAndroidProcessEnv ( )
342+ ) ;
342343 return stdout
343344 . split ( '\n' )
344345 . map ( ( line ) => line . trim ( ) )
@@ -355,7 +356,11 @@ export type AdbDevice = {
355356} ;
356357
357358export const getConnectedDevices = async ( ) : Promise < AdbDevice [ ] > => {
358- const { stdout } = await spawn ( 'adb' , [ 'devices' , '-l' ] ) ;
359+ const { stdout } = await spawn (
360+ 'adb' ,
361+ [ 'devices' , '-l' ] ,
362+ withAndroidProcessEnv ( )
363+ ) ;
359364 const lines = stdout . split ( '\n' ) . slice ( 1 ) ;
360365 const devices : AdbDevice [ ] = [ ] ;
361366
0 commit comments