How to Start an Application Using Android ADB Tools

How to Start an Application Using Android ADB Tools

30 November 2024 Stephan Petzl Leave a comment Tech-Help

Android Debug Bridge (ADB) is a versatile command-line tool that facilitates communication with an emulator instance or connected Android device. One of its key functionalities is the ability to send commands to start applications directly from the command line. This guide will walk you through several methods to achieve this using ADB tools.

Method 1: Using Intent Commands

To start an application using the intent command, you can use the following syntax:

adb shell am start -n com.package.name/com.package.name.ActivityName

This command directly starts an activity within the specified package. You can also specify actions to filter by your intent-filters:

adb shell am start -a com.example.ACTION_NAME -n com.package.name/com.package.name.ActivityName

This method is straightforward and allows for the execution of specific activities within an application.

Method 2: Using the Monkey Tool

An alternative approach involves using the Monkey tool, which is primarily used for testing but can also be employed to start applications:

adb shell monkey -p your.app.package.name -c android.intent.category.LAUNCHER 1

This command launches the application as if it were clicked from the launcher. The integer ‘1’ signifies that only one random input is generated, effectively just starting the app.

Method 3: Automating with Scripts

For Linux and Mac users, creating a script can simplify the process, especially when the package or activity names are unknown. Consider creating a script file, adb-run.sh, with the following lines:


pkg=$(aapt dump badging $1|awk -F" " '/package/ {print $2}'|awk -F"'" '/name=/ {print $2}')
act=$(aapt dump badging $1|awk -F" " '/launchable-activity/ {print $2}'|awk -F"'" '/name=/ {print $2}')
adb shell am start -n $pkg/$act
  

Make the script executable with chmod +x adb-run.sh and run it using adb-run.sh myapp.apk.

Considerations

When starting applications using ADB shell commands, remember that it automatically adds the FLAG_ACTIVITY_NEW_TASK flag. This flag may alter the behavior of the back button, as it can bring the current task to the forefront rather than starting a new activity.

Enhancing Your Testing Workflow

While ADB commands are powerful, they can become cumbersome, especially when dealing with multiple tests or devices. This is where tools like Repeato can significantly enhance your workflow. Repeato allows you to create, run, and maintain automated tests for iOS and Android applications without writing code. It utilizes computer vision and AI to streamline the testing process, making it much faster to edit and execute tests.

Repeato also integrates ADB, allowing you to execute ADB commands through “script steps”, ensuring that your ADB commands are well-timed and executed in sequence.

For more information on utilizing ADB commands efficiently, consider reading our blog articles on granting app permissions using ADB and querying device state via ADB shell command.

Like this article? there’s more where that came from!