Android - Launch main activity from APK


Sometimes you might find yourself wanting to execute an app programmatically or through adb shell but you have no idea which Activity from this app you should call. Wonder no more, AAPT is here for you.

aapt dump badging development/latest/CoreService-1.0.1.apk | grep launchable-activity
| sed -r "s/launchable-activity: name='([a-z0-9.]*)'.*/\1/i"

This will give you the main activity of the package. If you want to go a step ahead, you can actually write it so it will automatically also execute the activity:


adb shell am start -a android.intent.action.MAIN -n `aapt dump badging
development/latest/EmployeeManagement-latest.apk | grep launchable-activity
| sed -r "s/launchable-activity: name='([a-zA-Z0-9.]+)(\.[a-zA-z0-9]+)'.*/\1\/\2/i"


Quick explanation:


adb shell am start -a android.intent.action.MAIN -n 

This starts an Activity. We will be passing the activity name from our extracting command, so we surround it with ``.

aapt dump badging development/latest/EmployeeManagement-latest.apk | grep launchable-activity
| sed -r "s/launchable-activity: name='([a-zA-Z0-9.]+)(\.[a-zA-z0-9]+)'.*/\1\/\2/i"

This is basically same command chain we had at the beginning, except the sed regex: now it will extract and process the activity name by adding a slash (/) before the name of the activity. That's what am tool expects as a syntax. For example: com.example.android/.YourActivityName. Don't ask me why, though.

You're welcome ;)

Comments

Popular Posts