83

Is there a way to get the launchable activity for a package from using adb? For an unroot phone (i.e. without having the pull the apk from /data/app directory and inspect with appt).

I tried dumpsys, but it does not include information on default launchable activity.

Thanks

Prags
  • 2,457
  • 2
  • 21
  • 38
fruitJuice
  • 1,281
  • 2
  • 11
  • 22

9 Answers9

100

You don't need root to pull the apk files from /data/app. Sure, you might not have permissions to list the contents of that directory, but you can find the file locations of APKs with:

adb shell pm list packages -f

Then you can use adb pull:

adb pull <APK path from previous command>

and then aapt to get the information you want:

aapt dump badging <pulledfile.apk>
kabuko
  • 36,028
  • 10
  • 80
  • 93
  • 12
    and aapt usage can be found here on StackOverflow: http://stackoverflow.com/a/7502519/26510 – Brad Parks Jul 05 '13 at 21:09
  • This is great, direct and killer, including the first comment – Oxi Aug 08 '13 at 13:22
  • 3
    1, Do as kabuko said 2, adb pull /data/app/ ~/ 3, aapt dump badging Thats it – Oxi Aug 08 '13 at 13:41
  • 1
    I've just added the `aapt` and `adb pull` usage to the answer as well as it seems to be useful for a lot of people. – kabuko Aug 22 '13 at 20:33
  • 1
    @Vera No. It's a tool in ADT for all platforms. Make sure you have the build tools installed and look under the build-tools folder. It should be under another subfolder corresponding to your SDK. – kabuko Aug 08 '14 at 23:22
  • Unfortunately, using `aapt` does not work if the launchable activity is an `activity-alias`. It simply lists no launchable activity in that case, – sschuberth Jun 26 '15 at 07:01
  • I'm trying this on a system app that contains the hidden menus shown by special dialer codes. Via logcat, I found what several of the activities are called, but when I run aapt dump badging it doesn't even show any of these activities. Is there something more specific that can be run? – bobpaul Feb 24 '16 at 21:37
  • 4
    I needed "aapt dump xmltree HiddenMenuLight.apk AndroidManifest.xml" – bobpaul Feb 24 '16 at 21:58
60
$ adb shell pm dump PACKAGE_NAME | grep -A 1 MAIN
friederbluemle
  • 33,549
  • 14
  • 108
  • 109
  • 3
    `$ adb shell pm dump PACKAGE_NAME | grep -A 1 'filter' | head -n 1 | cut -d ' ' -f 12` There's probably a smarter way but I ain't no *NIX guru. – Kristopher Dec 01 '16 at 18:32
  • 4
    `pm dump` dumps many unnecessary services. and `pm` itself is a java command so it takes some time to start. The relevant part of the output comes from `PackageManager.Dump()` anyway - so `adb shell dumpsys package PACKAGE_NAME` would be much more efficient for producing the same result. But there is even better way for Android 7.0+ see http://stackoverflow.com/a/41325792/1778421 – Alex P. Dec 26 '16 at 19:29
  • 3
    @Kristopher, the problem with both yours and @friederbluemle's filters is that there could be multiple resolvers for the `android.intent.action.MAIN`. The proper way is to find one of `android.intent.category.LAUNCHER` category first. Something like this would work `grep -B 10 category\.LAUNCHER | grep -o '[^ ]*/[^ ]*' | tail -n 1` – Alex P. Dec 26 '16 at 20:10
  • @m0skit0 Which emulator/device are you using? Check which framework version you're using and the content of `/system/bin/pm` – friederbluemle Feb 19 '18 at 10:07
  • 1
    @friederbluemle TY! short and useful. Works fine on Android 12. – 0x8BADF00D May 10 '23 at 20:06
30

Since Android 7.0 you can use adb shell cmd package resolve-activity command to get the default activity of an installed app like this:

adb shell "cmd package resolve-activity --brief com.google.android.calculator | tail -n 1"
com.google.android.calculator/com.android.calculator2.Calculator
Alex P.
  • 30,437
  • 17
  • 118
  • 169
14
#!/bin/bash
#file getActivity.sh
package_name=$1
#launch app by package name
adb shell monkey -p ${package_name} -c android.intent.category.LAUNCHER 1;
sleep 1;
#get Activity name
adb shell logcat -d | grep 'START u0' | tail -n 1 | sed 's/.*cmp=\(.*\)} .*/\1/g'

sample:

getActivity.sh com.tencent.mm
com.tencent.mm/.ui.LauncherUI
zfu
  • 161
  • 1
  • 4
11

I didn't find it listed so updating the list.

You need to have the apk installed and running in front on your phone for this solution:

Windows CMD line:

adb shell dumpsys window windows | findstr <any unique string from your pkg Name>

Linux Terminal:

adb shell dumpsys window windows | grep -i <any unique string from your Pkg Name>

OUTPUT for Calculator package would be:

Window #7 Window{39ced4b1 u0 com.android.calculator2/com.android.calculator2.Calculator}:

    mOwnerUid=10036 mShowToOwnerOnly=true package=com.android.calculator2 appop=NONE

    mToken=AppWindowToken{29a4bed4 token=Token{2f850b1a ActivityRecord{eefe5c5 u0 com.android.calculator2/.Calculator t322}}}

    mRootToken=AppWindowToken{29a4bed4 token=Token{2f850b1a ActivityRecord{eefe5c5 u0 com.android.calculator2/.Calculator t322}}}

    mAppToken=AppWindowToken{29a4bed4 token=Token{2f850b1a ActivityRecord{eefe5c5 u0 com.android.calculator2/.Calculator t322}}}

    WindowStateAnimator{3e160d22 com.android.calculator2/com.android.calculator2.Calculator}:

      mSurface=Surface(name=com.android.calculator2/com.android.calculator2.Calculator)

  mCurrentFocus=Window{39ced4b1 u0 com.android.calculator2/com.android.calculator2.Calculator}

  mFocusedApp=AppWindowToken{29a4bed4 token=Token{2f850b1a ActivityRecord{eefe5c5 u0 com.android.calculator2/.Calculator t322}}}

Main part is, First Line:

Window #7 Window{39ced4b1 u0 com.android.calculator2/com.android.calculator2.Calculator}:

First part of the output is package name:

com.android.calculator2

Second Part of output (which is after /) can be two things, in our case its:

com.android.calculator2.Calculator

  1. <PKg name>.<activity name> = <com.android.calculator2>.<Calculator>

    so .Calculator is our activity

  2. If second part is entirely different from Package name and doesn't seem to contain pkg name which was before / in out output, then entire second part can be used as main activity.

PradyJord
  • 2,136
  • 12
  • 19
4

Here is another way to find out apps package name and launcher activity.

Step1: Start "adb logcat" in command prompt.

Step2: Open the app (either in emulator or real device) enter image description here

Rameshwar
  • 541
  • 6
  • 22
1

You can also use ddms for logcat logs where just giving search of the app name you will all info but you have to select Info instead of verbose or other options. check this below image.

enter image description here

Mark
  • 6,762
  • 1
  • 33
  • 50
0

Launch your app and keep it in foreground.

Run the below command:

adb shell dumpsys window windows | find "mcurrentfocus"

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
krrish
  • 1
  • 1
0

mCurrentFocus doesn't work for me on Android 12 device.

Here is the right step to go:

  1. Connect the device and open the app.
  2. adb shell dumpsys window windows | grep -E mObscuringWindow
  3. mObscuringWindow=Window{bc78a3 u0 com.yds.demo/com.test.activity.AppActivity}

com.test.activity.AppActivity is the activity.

William Hu
  • 15,423
  • 11
  • 100
  • 121