0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pythonista
  • 27
  • 8

3 Answers3

1

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SamBob
  • 849
  • 6
  • 17
  • the phone.shell ('am .......") isn't working unfortunately – Pythonista Oct 20 '21 at 11:58
  • 1
    "isn't working": what happens? Do you get an error message? What message? – SamBob Oct 20 '21 at 12:52
  • The method you show require to have acces to the package.name and the ActivityName. I still don't know how to acces the ActivityName So because my address was incomplete, no action or error happen. – Pythonista Oct 21 '21 at 13:00
  • 1
    Ah, I see. Its important to know which activity you want as a single app will usually have multiple. I've added a snippet to the answer that will help you list the activities belonging to a package on your phone with `ppadb` – SamBob Oct 21 '21 at 14:14
1

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

Pythonista
  • 27
  • 8
0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134