Finding out package and activity name via adb for appium automation
Tags: android, appium, mobiletesting
Published: 2020-04-24 04:15:15

---
title: Finding out package and activity name via adb for appium automation
published: true
date: 2020-04-24 04:12:46 UTC
canonical_url: https://automationhacks.io/2020/04/24/finding-out-package-and-activity-name-via-adb-for-appium-automation/
---

![File:Android robot head.svg - Wikimedia Commons](https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Android_robot_head.svg/1280px-Android_robot_head.svg.png)<figcaption>Android head image by <a href="https://commons.wikimedia.org/wiki/File:Android_robot_head.svg" target="_blank" rel="noopener">Wikimedia commons</a></figcaption>

Hey there,

A common problem when starting with ![🤖](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f916.png) **android automation** with appium after you have learned basics is to find the **package name** and **activity name** to get started with Appium driver session

This is easy if you have access to the source code and friendly developers around but what if you want to know this information for any app from the Google play store? ![🕵🏻‍♂️](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f575-1f3fb-200d-2642-fe0f.png)

Turns out its possible to get this info entirely using `adb`.

Let’s see how.

## Ensure adb is set up and on path

- Ensure android SDK is installed and already in your path. It automatically gets installed if you [download Android studio](https://developer.android.com/studio)
- Add below entry in your `.zshrc` or `.bashrc` file followed by `source <file_name>` if you are on the mac otherwise add these in your windows system environment variables to allow all the common CLI to be available without having to type the fully qualified path every time

```
# Android specific env vars
export ANDROID\_HOME=$HOME/Library/Android/sdk
export PATH=$ANDROID\_HOME/tools:$PATH
export PATH=$ANDROID\_HOME/platform-tools:$PATH
export PATH=$ANDROID\_HOME/tools/bin:$PATH
```

## Get app package name

- Open the app on a real device connected via USB (with USB debugging on) or on an android emulator
- Execute below:

```
adb shell dumpsys window windows | grep -i "mCurrentFocus"
```

For example, Here is the output if you run above command with twitter app already open

```
mCurrentFocus=Window{6c0629c u0 com.twitter.android/com.twitter.android.onboarding.common.CtaSubtaskActivity}
```

You should see `packageName/currentActivity` in the output.

As you can rightly guess our package name is:

```
com.twitter.android
```

## Get start activity for use

While the above command gets you to name of the current activity, It might not be the activity that the app starts with and thus cause your appium test case to fail in driver initialization itself

How can we work around this?

We can use `adb` and `aapt` tools to figure this out. Let’s see how to do this.

### Get a list of installed packages and path to base apk

We can execute the below command to get a list of **all installed packages** in the device and filter to get only the app for which we want to find out this info

```
adb shell pm list packages -f | grep -i "<app\_name>"
```

Here is a sample output for twitter app

```
package:/data/app/com.twitter.android-eka4BzKX4Con18lwXr0yAQ==/base.apk=com.twitter.android
```

### Get the base.apk file for the app

Next, we need to get the base apk file into our local machine. We can easily do this via `adb pull`

> Also, notice, I have passed the output of the above command after removing the app package name

```
adb pull /data/app/com.twitter.android-eka4BzKX4Con18lwXr0yAQ==/base.apk
```

You should see a file getting downloaded on your current path. For example:

```
/data/app/com.twitter.android-eka4BzKX.... 80.2 MB/s (19076098 bytes in 0.227s)
```

### Get the activity name using `aapt`tool

Android SDK comes with [AAPT tools (Android asset packaging tool)](https://developer.android.com/studio/command-line/aapt2) which allows us to compile and build into binary format. We can use this for our specific purpose of finding out the start activity

If `aapt` is already on your path then you can remove directly use it else choose the version which you want to use (usually the latest) under `build-tools` and run below command with your `base.apk` files name

```
 ~/Library/Android/sdk/build-tools/28.0.3/aapt dump badging base.apk | grep -i "launchable-activity"
```

Voila! ![🙆🏻‍♂️](https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f646-1f3fb-200d-2642-fe0f.png)

You should output like below. This lets you know the activity which can be used to launch the app with which in this case is: **com.twitter.android.StartActivity**

```
launchable-activity: name='com.twitter.android.StartActivity' label='' icon=''
```

## Conclusion

ADB is a really powerful tool for android automation and with the use of `adb` and `aapt` we can very easily find out the package name and activity name to use to start with our appium automation.

If you found this tip useful, do share it with your friends or colleagues and until next time. Cheers! Happy testing.