Pythonista
Pythonista

Reputation: 37

How can I launch an Android app on a device through Python?

I have consulted several topics on the subject, but I didn't see any related to launching an app on a device directly using a ppadb command.

I managed to do this code:

import ppadb
import subprocess

from ppadb.client import Client as AdbClient

# Create the connect functiun

def connect():

   client = AdbClient(host='localhost', port=5037)
   devices = client.devices()
   for device in devices:
       print (device.serial)

   if len(devices) == 0:
       print('no device connected')
       quit()

   phone = devices[0]
   print (f'connected to {phone.serial}')

   return phone, client

if __name__ == '__main__':
    phone, client = connect()

    import time
    time.sleep(5)

    # How to print each app on the emulator
    list = phone.list_packages()
    for truc in list:
       print(truc)

# Launch the desired app through phone.shell using the package name
phone.shell(????????????????)

From there, I have access to each app package (com.package.name). I would like to launch it through a phone.shell() command but I can't access the correct syntax.

I can execute a tap or a keyevent and it's perfectly working, but I want to be sure my code won't be disturbed by any change in position.

Upvotes: 0

Views: 3516

Answers (3)

Diego Torres Milano
Diego Torres Milano

Reputation: 69318

Using AndroidViewClient/cluebra, you can launch the MAIN Activity of a package as follows:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from com.dtmilano.android.viewclient import ViewClient

ViewClient.connectToDeviceOrExit()[0].startActivity(package='com.example.package')

This connects to the device (waiting if necessary) and then invokes startActivity() just using the package name.

startActivity() can also receive a component which is used when you know the package and the activity.

Upvotes: 0

SamBob
SamBob

Reputation: 955

From How to start an application using Android ADB tools, the shell command to launch an app is

am start -n com.package.name/com.package.name.ActivityName

Hence you would call

phone.shell("am start -n com.package.name/com.package.name.ActivityName")

A given package may have multiple activities. To find out what they are, you can use dumpsys package as follows:

def parse_activities(package, connection, retval):
    out = ""
    while True:
        data = connection.read(1024)
        if not data: break
        out += data.decode('utf-8')
    retval.clear()
    retval += [l.split()[-1] for l in out.splitlines() if package in l and "Activity" in l]
    connection.close()

activities = []
phone.shell("dumpsys package", handler=lambda c: parse_activities("com.package.name", c, activities))
print(activities)

Upvotes: 1

Pythonista
Pythonista

Reputation: 37

Here is the correct and easiest answer:

phone.shell('monkey -p com.package.name 1')

This method will launch the app without needing to have acces to the ActivityName

Upvotes: 1

Related Questions