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.
First, start the RPC service. How to operate the app and the shell There is a way to start from.
Open the list of interpreters in the SL4A app and select "Start Server" from the menu.
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.
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
Then start Python.
There are two methods, one is to use Python on Android and the other is to use Python on PC.
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.
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 as
droid = 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