WARNING: This library is incompatible with Python 3.11 or newer due to breaking changes on inspect
module. Please use this library along with Python 3.7 - 3.10. Contributions of fixing compatibility with Python 3.11 or newer is appreciated.
This is a modified version for specific project usage - for example, service permission granting program for all Nokia phones.
Note: This is not an official Google project. It is maintained by ex-Google engineers. For a better maintained option, look at adb_shell.
This repository contains a pure-python implementation of the ADB and Fastboot protocols, using libusb1 for USB communications.
This is a complete replacement and rearchitecture of the Android project's ADB and fastboot code
This code is mainly targeted to users that need to communicate with Android devices in an automated fashion, such as in automated testing. It does not have a daemon between the client and the device, and therefore does not support multiple simultaneous commands to the same device. It does support any number of devices and never communicates with a device that it wasn't intended to, unlike the Android project's ADB.
Once installed, two new binaries should be available: pyadb
and pyfastboot
.
pyadb devices
pyadb shell ls /sdcard
Running ./make_tools.py
creates two files: adb.zip
and fastboot.zip
. They
can be run similar to native adb
and fastboot
via the python interpreter:
python adb.zip devices
python adb.zip shell ls /sdcard
FIHSW model: See this file as reference
HMDSW model get permission code (note the dk_calculation module is unavailable yet).
from adb import fastboot
from io import StringIO
import dk_calculation
import sys
def _InfoCb(message):
if not message.message:
return
print(message.message.decode('utf-8'))
def getsecver(device):
tmp = sys.stdout
result = StringIO()
sys.stdout = result
device.Oem('getsecurityversion', info_cb=_InfoCb)
sys.stdout = tmp
del tmp
return result.getvalue().splitlines()
def getperm(device):
tmp = sys.stdout
result = StringIO()
sys.stdout = result
device.Oem('getpermissions', info_cb=_InfoCb)
sys.stdout = tmp
del tmp
return result.getvalue().splitlines()
def authcode(device):
return device.HmdAuthStart().decode('utf-8')
ThisDevice = fastboot.FastbootCommands()
ThisDevice.ConnectDevice()
# Essential Informations will be stored at these 3 variables.
product = ThisDevice.Getvar('product').decode('utf-8')
secver = getsecver(ThisDevice)[0]
psn = ThisDevice.Getvar('serialno').decode('utf-8')
# 1st permission type is flash
ThisDevice.HmdEnableAuth(1, dk_calculation.getresult(prjcode=product, serialnumber=psn, securityversion=secver, auth_code=authcode(ThisDevice))
# Check Enabled Permissions
print(getperm(ThisDevice))
# 3rd permission type is repair
ThisDevice.HmdEnableAuth(3, dk_calculation.getresult(prjcode=product, serialnumber=psn, securityversion=secver, auth_code=authcode(ThisDevice))
# Check Enabled Permissions
print(getperm(ThisDevice))
- Simpler code due to use of libusb1 and Python.
- API can be used by other Python code easily.
- Errors are propagated with tracebacks, helping debug connectivity issues.
- No daemon outliving the command.
- Can be packaged as standalone zips that can be run independent of the CPU architecture (e.g. x86 vs ARM).
- Technically slower due to Python, mitigated by no daemon.
- Only one command per device at a time.
- More dependencies than Android's ADB.
- libusb1 (1.0.16+)
- python-libusb1 (1.2.0+)
adb.zip
: one of:- py-cryptography
- python-rsa (3.2+)
fastboot.zip
(optional):- python-progressbar (2.3+)
- Initial version
- Added TcpHandle (jameyhicks)
- Various timing and other changes (alusco)
- Update to libusb1 1.6+ (bytearray output)
- Add support for Python 3.6
- Create adb.zip and fastboot.zip as executable tools.
- Add Travis CI integration
- Support multiple crypto libraries (M2Crypto + python-rsa)
- Push directories
adb_commands.AdbCommands()
is now a normal class rather than a collection of staticmethods. Using the following example code to get started:
device = adb_commands.AdbCommands()
device.ConnectDevice(rsa_keys=[signer])
Many changes since 1.2.0!
- New entrypoints exposed by pip: pyadb and pyfastboot
- Lots of Python 2/3 compatibility fixes
- Windows compatibility fixes
- Transfer progress available (
Push
,Pull
,Install
) - Handle some misbehaving devices (double CLSE bug)
- New options for
Push
andInstall
(st_mode
andgrant_permissions
)