DEV Community

petercour
petercour

Posted on

9 1

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)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay