Python SL4A Development
Asked Answered
D

1

8

Ok - I'm feeling like a frustrated idiot. I want to state that upfront.

I'm trying to get a Python/Android setup going so I can develop Python applications for Android. I've got both SL4A and the Python interpreter installed on my Android device and can write an application on the phone that runs.

The issue is how can I continue with development on my PC and interact with my Android device. I've unzipped the extras package and put it in $PYTHONPATH/site-packages/. I've integrated Ecplise with PyDev and Android packages and set the AP_PORT and AP_HOST environment variables.

When I run the following code:

import android
droid = android.Android()
droid.makeToast("Hello")

I get errors in Eclipse that include "com.googlecode.android_scripting.rpc.RpcError: Unknown RPC." to bad magic numbers.

When I drop into Python via CLI and type

import android

I get the following output

com.googlecode.android_scripting.rpc.RpcError: Unknown RPC.

Any help or beginners tutorial would be greatly appreciate.

I know I must be doing something wrong because I see others getting set up with, what appears, relative ease.

Thanks in advance.

Duster answered 31/5, 2012 at 19:16 Comment(1)
just for clarification. You are wanting to develop python scripts within eclipse and have the ability to push the to the device similarly to native development with adb?Pasahow
I
8

1. Start Server

On Device

First, connect your device with USB. Then for remote debugging, you need to start a server on your device:

SL4A -> Interpreters -> Menu -> Start Server

The private one is preferable.

Then you can look up the port for the server in the Android notification area (with SL4A r5 you can specify a fixed port in settings).

From PC

Alternatively, you can start a server from the PC with adb using a specific port (r5 required for setting the port):

$ adb shell 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 51943


2. Specify connection

Now you need to forward your port with adb (assuming the looked up value is 51943):

$ adb forward tcp:51943 tcp:51943
$ export AP_PORT=51943


3. Start script

Afterwards you should be able to run your script locally:

$ python my_script.py


Wifi connection

If you want to go via wifi, you don't need to forward your port with adb. Instead you have to use a public server and specify your host additionally:

$ export AP_HOST=192.168.0.100 


References

For more help see the Wiki page addressing this topic.

Implausibility answered 5/6, 2012 at 8:37 Comment(2)
Hi, I have complete 1,2 procedure step, but i have run python code in my pc i will get this error ImportError: No module named android, I have try in my terminal wget 127.0.0.1:51943 i will get this msg Connecting to 127.0.0.1:51943... connected. HTTP request sent, awaiting response... No data received. Retrying.Ehrlich
download in android-scripting.googlecode.com/hg/python/ase/android.py android.py file in development dir and proceed the step it is work fineEhrlich

© 2022 - 2024 — McMap. All rights reserved.