How to get the main activity of the Android App for Appium
To automate the Android application, you need to specify the application package name and activity.
An Android application can have multiple activities. But we may not be able to launch all the activities. To find out the main activity, you can use below commands.
- adb shell pm list packages -f
- adb pull /system/app/Calculator/Calculator.apk
- aapt dump badging Calculator.apk
First command lists all the application paths and package names. Then second command copies the apk file from the emulator or device on the machine. Finally we have used aapt tool to find the main activity of the app. aapt tool is inside Android SDK directory
Here are the screenshots of above commands.
Note that above command prints the package details for all apps in the device. You can use grep command to filter only specific apps as shown in below image.
As shown in above image, we have listed the package details for only calculator. Then we have pulled the apk file to system.
Once the apk file is available, you can get the launchable activity as shown in above image.
Here are some of the main activities of apps on Android Emulator.
Here are the main activities of some of the popular Android apps.
Note that if you try to launch the invalid activity, you will get the error saying Permission to start activity denied.
Android app can have multiple activities. To mark the specific activity as the starting activity, you have to add the intent-filter as shown in below example. Usually developers need to add below XML section app’s manifest file to make the specific activity as launchable.
<activity android:name=".MainActivity" android:label="XYZ"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>