DEV Community

petercour
petercour

Posted on

Automate Android with Python

Android phones can be controlled through Python scripts. To do so, adb must be installed and the phone connected.

Then you can use adb shell commands to control the phone like

#!/usr/bin/python3
import os

...

os.system("adb shell input tap 350 715")

which will click on the screen. You can also type text

#!/usr/bin/python3
os.system('adb shell input text "insert%syour%stext%shere"')

combined with time.sleep(seconds) you can automate any android app. Keeping in mind you don't have to use the phone at the same time.

Script

You can wrap it around functions to make it easy. Connect your device with a usb cable, type

adb usb

Then run the script below in your favorite chat program

#!/usr/bin/python3
import os

def android_tap(x,y):
    os.system("adb shell input tap " + x + " " + y)

def android_type(text):
    os.system('adb shell input text "' + text + '"')

android_type("great")

The program adb can do a lot more, a wrapper like this makes your android phone scriptable

Learn Python:

Top comments (0)