Starting an Android Activity from the command line

Kelvin Watson
2 min readNov 9, 2020

During feature development, you don’t always have access to an entry point to your feature. Perhaps that entry point is still under discussion, or perhaps the Activity you want to launch can only be accessed via a push notification, or perhaps the launching Activity to your Activity has not yet been built.

Whatever the reason, it is helpful to be able to launch an Activity from the command line.

Starting an Android Activity from adb

As a simple example, suppose you’re building an Activity (e.g. SecondPageActivity) within a large app, and you don’t have access to an entry point to this Activity within the launching Activity (e.g. First page activity), so we want to launch cold launch SecondPageActivity.

Within the Android sdk, we have access to the debug bridge, or adb. It is located in your /Library/Android/sdk/platform-tools folder.

Use this command to launch SecondPageActivity:

./adb shell am start -n com.example.my.app/. SecondPageActivity --es "Message" "hello!"

The above means start the SecondPageActivity with a strong intent extra.

If you’re using a device or emulator running API 23 or under, this should work as expected, i.e. SecondPageActivity will be launched.

However, if you try to do this with an emulator or device running API above 23, you may encounter this error:

Security exception: Permission Denial: starting Intent { act=android.intent.action.RUN flg=0x30000000 cmp=com.example.my.app/SecondPageActivity (has extras) } from null (pid=4729, uid=2000) not exported from uid 10079

To remedy this, create a debug package in your module and add an AndroidManifest.xml file. This AndroidManifest.xml should be identical to the one in your main package, except that the activity tag includes the exported="true" attribute.

This debug manifest will be used when building the debug app variant so that you can use the above adb command to launch your Activity in devices running API higher than 23.

Please consider supporting me so I can bring you more content:

Please consider buying me a coffee!

--

--