Interact with Python on Android from PC via adb

With SL4A, you can use Python REPL on Android. However, if you have a PC in front of you, it would be convenient if you could REPL it with a keyboard and monitor.

Start SL4A service

First, start the RPC service. How to operate the app and the shell There is a way to start from.

Launch from the app

Open the list of interpreters in the SL4A app and select "Start Server" from the menu.

server.png

Select "Public" to open the port to the outside, and "Private" to open the port only to the inside.

When the server starts, it opens a port for RPC. You can check the port number from the items that appear in the notification bar.

The port number is usually decided randomly, but you can fix the port with "Server Port" in the setting item of the application.

Launch from shell

Use the ʻam` command in the shell as follows:

python


$ am start \
 -a com.googlecode.android_scripting.action.LAUNCH_SERVER \
 -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher

Server Port settings have no effect, but intent parameters (https://github.com/damonkohler/sl4a/blob/master/android/ScriptingLayerForAndroid/src/com/googlecode/android_scripting/activity/ScriptingLayerService.java) You can Specify with # L215).

python


$ am start \
 -a com.googlecode.android_scripting.action.LAUNCH_SERVER \
 -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher \
 --ei com.googlecode.android_scripting.extra.USE_SERVICE_PORT 12345

By default it opens as private, but you can make it public with a parameter.

python


$ am start \
 -a com.googlecode.android_scripting.action.LAUNCH_SERVER \
 -n com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher \
 --ez om.googlecode.android_scripting.extra.USE_PUBLIC_IP true

Start python

Then start Python.

There are two methods, one is to use Python on Android and the other is to use Python on PC.

Use Python on Android itself

Launch the Python interpreter body directly on the adb shell. At that time, specify the port of the started server in the ʻAP_PORT` environment variable.

python


#I don't know if it works in your environment because the environment settings are through trial and error.
#12345 is the port number of the RPC server
$ AP_PORT=12345 \
  LD_LIBRARY_PATH=/data/data/com.googlecode.pythonforandroid/files/python/lib:$LD_LIBRARY_PATH \
  PYTHONPATH=/sdcard/com.googlecode.pythonforandroid/extras/python \
  PYTHONHOME=/data/data/com.googlecode.pythonforandroid/files/python \
  /data/data/com.googlecode.pythonforandroid/files/python/bin/python

If python starts, you can interact as it is.

Use Python on your PC

RPC can be done if the port is connected from Python on the personal computer side. Use android.py in python_extras.zip as ʻimport`.

If you started the server privately, use ʻadb` port forwarding.

python


$ adb forward tcp:12345 tcp:12345
$ AP_PORT=12345 python

If you started the server in Public, specify the Android IP address in the ʻAP_HOST` environment variable.

python


$ AP_HOST=192.168.1.123 AP_PORT=12345 python

In either case, specify the port number of the RPC server with the ʻAP_PORTenvironment variable. Instead of an environment variable, you can also specify the address as an argument, such asdroid = android.Android (('127.0.0.1', 12345))`.

Hopefully you can use the same functions as the SL4A main unit.

python


>>> import android
>>> droid = android.Android()
>>> droid.makeToast("hello, world!")
Result(id=0, result=None, error=None)

(Reference: Stack Overflow --Unable to connect to SL4A server)

Recommended Posts

Interact with Python on Android from PC via adb
Accurately correct Android clock with adb and python
Get data from database via ODBC with Python (Access)
With skype, notify with skype from python!
OpenJTalk on Windows10 (Speak Japanese with Python from environment construction)
Notes on importing data from MySQL or CSV with Python
Call C from Python with DragonFFI
Using Rstan from Python with PypeR
Install Python from source with Ansible
Create folders from '01' to '12' with python
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Run Aprili from Python with Orange
Notes on using rstrip with python.
Notes on accessing dashDB from python
Call python from nim with Nimpy
Getting started with Python 3.8 on Windows
Read fbx from python with cinema4d
Call C / C ++ from Python on Mac
[Memo] Tweet on twitter with python
Send email via gmail with Python 3.4.3.
Update Python on Mac from 2 to 3
Data integration from Python app on Linux to Amazon Redshift with ODBC
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
Send and receive binary data via serial communication with python3 (on mac)
Data integration from Python app on Windows to Amazon Redshift with ODBC
Collecting information from Twitter with Python (Twitter API)
Receive textual data from mysql with python
Get html from element with Python selenium
[Note] Get data from PostgreSQL with Python
Run servo with Python on ESP32 (Windows)
Play audio files from Python with interrupts
Tweet from python with Twitter Developer + Tweepy
A memo with Python2.7 and Python3 on CentOS
Follow active applications on Mac with Python
[C] [python] Read with AquesTalk on Linux
Notes on doing Japanese OCR with Python
Business efficiency starting from scratch with Python
Push notification from Python server to Android
Decrypt files encrypted with openssl from python with openssl
Working with Azure CosmosDB from Python Part.2
Connecting from python to MySQL on CentOS 6.4
scipy stumbles with pip install on python 2.7.8
Image acquisition from camera with Python + OpenCV
Download files on the web with Python
Build Python environment with Anaconda on Mac
Getting started with Dynamo from Python boto
Try calling Python from Ruby with thrift
Scraping from an authenticated site with python
Use C ++ functions from python with pybind11
L-Chika TWE Lite from Python via ToCoStick
Share VLC playlists on PC and Android
Installing PIL with Python 3.x on macOS
Working with GPS on Raspberry Pi 3 Python
Getting started with Python with 100 knocks on language processing
Collecting information from Twitter with Python (Environment construction)
Microservices with GCP on RoR starting from scratch
Csv output from Google search with [Python]! 【Easy】
Extract text from PowerPoint with Python! (Compatible with tables)
Strategy on how to monetize with Python Java
Image processing from scratch with python (5) Fourier transform
Build a python environment with ansible on centos6