terminateApp
End app processes on Android devices by specifying the package ID, streamlining automation and testing workflows with AutoMobile's mobile automation suite.
Instructions
Terminate an app by package name
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| appId | Yes | App package ID of the app |
Implementation Reference
- src/server/appTools.ts:102-107 (registration)Registers the 'terminateApp' tool with the ToolRegistry using packageNameSchema and terminateAppHandler.ToolRegistry.registerDeviceAware( "terminateApp", "Terminate an app by package name", packageNameSchema, terminateAppHandler );
- src/server/appTools.ts:63-77 (handler)The registered tool handler for 'terminateApp', which instantiates TerminateApp class and calls its execute method.// Terminate app handler const terminateAppHandler = async (device: BootedDevice, args: AppActionArgs) => { try { const terminateApp = new TerminateApp(device); const result = await terminateApp.execute(args.appId); // observe = true return createJSONToolResponse({ message: `Terminated app ${args.appId}`, observation: result.observation, ...result }); } catch (error) { throw new ActionableError(`Failed to terminate app: ${error}`); } };
- The TerminateApp class execute method containing the core logic: checks if app installed/running/foreground using ADB, then force-stops it.async execute( packageName: string, progress?: ProgressCallback ): Promise<TerminateAppResult> { return this.observedInteraction( async () => { // Check if app is installed const isInstalledCmd = `shell pm list packages -f ${packageName} | grep -c ${packageName}`; const isInstalledOutput = await this.adb.executeCommand(isInstalledCmd, undefined, undefined, true); const isInstalled = parseInt(isInstalledOutput.trim(), 10) > 0; if (!isInstalled) { return { success: true, packageName, wasInstalled: false, wasRunning: false, wasForeground: false }; } // Check if app is running const isRunning = true; if (!isRunning) { return { success: true, packageName, wasInstalled: true, wasRunning: false, wasForeground: false }; } // Check if app is in foreground const currentAppCmd = `shell "dumpsys window windows | grep '${packageName}'"`; const currentAppOutput = await this.adb.executeCommand(currentAppCmd); // App is in foreground if it's either the top app or an IME target const isTopApp = currentAppOutput.includes(`topApp=ActivityRecord{`) && currentAppOutput.includes(`${packageName}`); const isImeTarget = currentAppOutput.includes(`imeLayeringTarget`) && currentAppOutput.includes(`${packageName}`); const isForeground = isTopApp || isImeTarget; await this.adb.executeCommand(`shell am force-stop ${packageName}`); return { success: true, packageName, wasInstalled: true, wasRunning: true, wasForeground: isForeground, }; }, { changeExpected: false, // TODO: Can make this true if we progress } ); }
- src/server/appTools.ts:10-12 (schema)Zod schema for input arguments to terminateApp tool (appId).export const packageNameSchema = z.object({ appId: z.string().describe("App package ID of the app"), });
- TypeScript interface defining the output/result structure of terminateApp operation.export interface TerminateAppResult { success: boolean; packageName: string; wasInstalled: boolean; wasRunning: boolean; wasForeground: boolean; observation?: ObserveResult; error?: string; }