Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Import SL4A Libraries #34

Open
GoogleCodeExporter opened this issue Apr 5, 2015 · 20 comments
Open

How to Import SL4A Libraries #34

GoogleCodeExporter opened this issue Apr 5, 2015 · 20 comments

Comments

@GoogleCodeExporter
Copy link

Hi. I have an android application using PySide and QML. It uses Python27 for 
android. But I can't use this with SL4A. How can I import SL4A libraries into 
python2.7 ? Thanks

Original issue reported on code.google.com by edips...@gmail.com on 6 Feb 2014 at 8:00

@GoogleCodeExporter
Copy link
Author

SL4A libraries are packaged in the APK in libs folder (script.jar): 
http://code.google.com/p/android-python27/source/browse/apk/#apk%2Flibs [ see: 
http://code.google.com/p/android-python27/wiki/TutorialHowToUpdateWithLatestSL4A
 ]

On Python side you have android.py in res/raw/python_extras_27.zip which 
bridges your calls to SL4A, e.g.:

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

Original comment by anthony....@gmail.com on 6 Feb 2014 at 8:19

@GoogleCodeExporter
Copy link
Author

I tryed to import android using adb shell on desktop:
adb shell
su
chmod 755 /data/data/org.modrana.PySideExample/files/python/bin/python
LD_LIBRARY_PATH=/data/data/org.modrana.PySideExample/files/python/lib 
/data/data/org.modrana.PySideExample/files/python/bin/python

It can import android but It give error when I tryed your example:
                                     <
Python 2.7.2 (default, Jul 10 2012, 02:50:07) 
[GCC 4.4.3] on linux3
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySide
>>> import android
>>> droid=android.Android()        
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/data/org.modrana.PySideExample/files/python/lib/python2.7/android.py", line 34, in __init__
    self.conn = socket.create_connection(addr)
  File "/home/tsheasha/GUC/Bachelors/android-python27/python-build/build/lib/python2.7/socket.py", line 553, in create_connection
socket.gaierror: [Errno 8] hostname nor servname provided, or not known.

I also tryed this with android device terminal, but got same error. My 
application is based on modrana.org/trac/wiki/PySideForAndroid ,and android.py 
is located res/raw/python_27.zip. It is located 
/data/data/org.modrana.PySideExample/files/python/lib/python2.7/android.py on 
device.
Android/libs have script.jar, locale_platform.jar, libGoogleAnalytics.jar, 
guava-r06.jar in PySİde-android project. 

I also tryed to import PySide in adb shell,

import PySide didnt give any error but

>>> import PySide.QtGui
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen failed: could not load library "libQtCore.so" needed by 
"QtGui.so"; caused by library "libQtCore.so" not found


PySide library works good with device. Maybe I have mistake using adb shell. So 
maybe SL4A can work too. I didnt test it yet. If I test it, I will report you.

Thank you.

Original comment by edips...@gmail.com on 7 Feb 2014 at 7:23

@GoogleCodeExporter
Copy link
Author

I did nou use this tutorial for running 
Python(https://code.google.com/p/android-python27/wiki/TutorialHowToRunPythonfro
mShell), I used thp.io/2011/pyside-android/ for running it.

Regards

Original comment by edips...@gmail.com on 7 Feb 2014 at 7:36

@GoogleCodeExporter
Copy link
Author

>socket.gaierror: [Errno 8] hostname nor servname provided, or not known.
>My application is based on modrana.org/trac/wiki/PySideForAndroid

I don't know the modrana app but the error you hit means that the SL4A server 
is not running (the socket cannot connect to the rpc server).

With android-python27 installed, from the command line you run the server with:

{{{
export AP_HOST=localhost
export AP_PORT=45777
am start -a com.android.python27.LAUNCH_RPC_SERVER -n 
com.android.python27/.RpcServerLauncher --ei 
com.android.python27.RPC_SERVER_PORT 45777
}}}

Then droid=android.Android() will work.

When you are in the context of the APK, the server is launched for you with 
(see in 
http://code.google.com/p/android-python27/source/browse/apk/src/com/android/pyth
on27/ScriptService.java):

mProxy = new AndroidProxy(this, null, true);
mProxy.startLocal();

So you would need to replicate one of this in your context.

SL4A scheme is simple, there is a RPC server running, the scripting languages 
send request to this server via sockets (android.py), the server execute native 
Java code and send back the results.


Original comment by anthony....@gmail.com on 7 Feb 2014 at 8:07

@GoogleCodeExporter
Copy link
Author

I installed android-python27.apk on Android 4.04, ran this, It worked on 
device. But I couldn't run the app both adb shell and android terminal.
I tryed them:

export AP_HOST=localhost
export AP_PORT=45777

am start -a com.android.python27.LAUNCH_RPC_SERVER -n 
com.android.python27/.RpcServerLauncher --ei 
com.android.python27.RPC_SERVER_PORT 45777

I got this error:

Starting: Intent { act=com.android.python27.LAUNCH_RPC_SERVER 
cmp=com.android.python27/.RpcServerLauncher (has extras) }
Error type 3
Error: Activity class 
{com.android.python27/com.android.python27.RpcServerLauncher} does not exist.

I also applied 
https://code.google.com/p/android-python27/wiki/TutorialHowToRunPythonfromShell
in adb shell:

export PYTHONHOME=/data/data/com.android.python27/files/python
export 
PYTHONPATH=/sdcard/com.android.python27/extras/python:/data/data/com.android.pyt
hon27/files/python/lib/python2.7/lib-dynload:/data/data/com.android.python27/fil
es/python/lib/python2.7
export PATH=$PYTHONHOME/bin:$PATH
export 
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/data/com.android.python27/files/python/li
b:/data/data/com.android.python27/files/python/lib/python2.7/lib-dynload

python can work in adb shell, But hello.py didn't work.

cd /data/data/com.android.python27/files/
python hello.py

gave error:

Traceback (most recent call last):
  File "hello.py", line 3, in <module>
    droid = android.Android()
  File "/sdcard/com.android.python27/extras/python/android.py", line 34, in __init__
    self.conn = socket.create_connection(addr)
  File "/home/tony/Desktop/xcross/android-python27/python-build/build/lib/python2.7/socket.py", line 571, in create_connection
socket.error: [Errno 111] Connection refused

Then for testing SL4A API:

export AP_HOST=localhost
export AP_PORT=45777
am start -a com.android.python27.LAUNCH_RPC_SERVER -n 
com.android.python27/.RpcServerLauncher --ei 
com.android.python27.RPC_SERVER_PORT 45777

gave error:

Starting: Intent { act=com.android.python27.LAUNCH_RPC_SERVER 
cmp=com.android.python27/.RpcServerLauncher (has extras) }
Error type 3
Error: Activity class 
{com.android.python27/com.android.python27.RpcServerLauncher} does not exist.

I think problem is connecting SL4A server and RPC server. I installed SL4A and 
Python for android (2.6) and I connected SL4A server so SL4A scripts can work 
with adb shell. (with SL4A wiki)

Original comment by edips...@gmail.com on 15 Feb 2014 at 10:36

@GoogleCodeExporter
Copy link
Author

I don't see why this error popup:

"Error: Activity class 
{com.android.python27/com.android.python27.RpcServerLauncher} does not exist."

Can you send me the apk you are using ?

Original comment by anthony....@gmail.com on 15 Feb 2014 at 11:35

@GoogleCodeExporter
Copy link
Author

Sorry, not android-python27.apk, I installed PythonAPK.apk:

https://code.google.com/p/android-python27/downloads/detail?name=PythonAPK.apk&c
an=2&q=

Original comment by edips...@gmail.com on 15 Feb 2014 at 1:34

@GoogleCodeExporter
Copy link
Author

First I try to learn connection between SL4A server and PythonAPK example. Then 
I will apply to connect this with PySide example.

Pyside apk : http://speedy.sh/MtHmA/PySideExample-1.0.apk

Python source : http://speedy.sh/RWSKU/android-pyside-example-project-master.zip

Regards

Original comment by edips...@gmail.com on 15 Feb 2014 at 2:12

@GoogleCodeExporter
Copy link
Author

Also My goal is to use SL4A API in PySide GUI with Python27. Such as GPS 
sensor, bluetooth, etc

Original comment by edips...@gmail.com on 15 Feb 2014 at 2:31

@GoogleCodeExporter
Copy link
Author

>Sorry, not android-python27.apk, I installed PythonAPK.apk:

Understood then, this is an old APK, I added RpcServerLauncher later on so you 
just need to build the android-python27.apk from HEAD and use it, then all will 
work as expected.

Original comment by anthony....@gmail.com on 15 Feb 2014 at 2:32

@GoogleCodeExporter
Copy link
Author

As I told at 2. comment, I tested SL4A hello world example in PySide Example 
from source, but It didnt worked.

>I added RpcServerLauncher later on so you just need to build the 
android-python27.apk from HEAD and use it, then all will work as expected.

Sorry HEAD means from source? I just know to test PySide projects sources from 
Necessitas or PyDroid. 

Original comment by edips...@gmail.com on 15 Feb 2014 at 2:43

@GoogleCodeExporter
Copy link
Author

Try attached new PythonAPK. Don't forget to uninstall previous one before 
installing this one.

Original comment by anthony....@gmail.com on 15 Feb 2014 at 2:46

Attachments:

@GoogleCodeExporter
Copy link
Author

Thank you, I will try it.

Original comment by edips...@gmail.com on 15 Feb 2014 at 2:48

@GoogleCodeExporter
Copy link
Author

Hi, I tested new apk, but gave error again.
First:

adb shell
root@android:/ # su
export PYTHONHOME=/data/data/com.android.python27/files/python
export 
PYTHONPATH=/sdcard/com.android.python27/extras/python:/data/data/com.android.pyt
hon27/files/python/lib/python2.7/lib-dynload:/data/data/com.android.python27/fil
es/python/lib/python2.7
export PATH=$PYTHONHOME/bin:$PATH
export 
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/data/data/com.android.python27/files/python/li
b:/data/data/com.android.python27/files/python/lib/python2.7/lib-dynload
root@android:/ # cd /data/data/com.android.python27/files
root@android:/data/data/com.android.python27/files # python hello.py
Traceback (most recent call last):
  File "hello.py", line 3, in <module>
    droid = android.Android()
  File "/sdcard/com.android.python27/extras/python/android.py", line 34, in __init__
    self.conn = socket.create_connection(addr)
  File "/sdcard/com.android.python27/extras/python/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 8] hostname nor servname provided, or not known

Second:

root@android:/ # export AP_HOST=localhost
root@android:/ # export AP_PORT=45777
on27/.RpcServerLauncher --ei com.android.python27.RPC_SERVER_PORT 45777       <
Starting: Intent { act=com.android.python27.LAUNCH_RPC_SERVER 
cmp=com.android.python27/.RpcServerLauncher (has extras) }
root@android:/ # python
Python 2.7.2 (default, Nov 16 2012, 19:13:11) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> droid=android.Android()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/sdcard/com.android.python27/extras/python/android.py", line 34, in __init__
    self.conn = socket.create_connection(addr)
  File "/sdcard/com.android.python27/extras/python/socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 111] Connection refused

This time RPC server started however droid=android.Android() didn't work. I 
don't know what I missed. Should I try this with an android emulator too? (My 
OS is Ubuntu 13.10.)
Regards

Original comment by edips...@gmail.com on 15 Feb 2014 at 11:07

@GoogleCodeExporter
Copy link
Author

Could be the port was in use by the first failed attempt (failed because 
RpcServerLauncher has not been started before droid=android.Android()), try to 
reboot and check with netstat -a (also ps | grep python). Then try again, start 
RpcServerLauncher, start python, droid=android.Android().

If fail again try in an emulator.

Original comment by anthony....@gmail.com on 15 Feb 2014 at 11:46

@GoogleCodeExporter
Copy link
Author

Yes droid=android.Android() worked after I did your instruction. But 
droid.makeToast("Hello from PC") gave error.

>>> import android                         
>>> droid=android.Android()
>>> droid.makeToast("Hello from PC")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/sdcard/com.android.python27/extras/python/android.py", line 58, in rpc_call
    return self._rpc(name, *args)
  File "/sdcard/com.android.python27/extras/python/android.py", line 49, in _rpc
    result = json.loads(response)
  File "/sdcard/com.android.python27/extras/python/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/sdcard/com.android.python27/extras/python/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/sdcard/com.android.python27/extras/python/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Original comment by edips...@gmail.com on 16 Feb 2014 at 12:30

@GoogleCodeExporter
Copy link
Author

Hi, fortunatelly It worked today. I applied this tutorial again : 
https://code.google.com/p/android-python27/wiki/TutorialHowToRunPythonfromShell

I tryed this tutorial many times and this always didn't work and gave error. 
But I don't know why It worked today. Now I will try to export paths to the 
PySİde example in adb shell in accordance with this tutorial for connecting 
SL4A.

Thanks

Original comment by edips...@gmail.com on 16 Feb 2014 at 12:37

@GoogleCodeExporter
Copy link
Author

I edited my_python_project.zip's main.py in PyDroid example 
(https://code.google.com/p/android-python27/source/browse/#hg%2Fpython-build-wit
h-qt%2FPyDroid%2Fandroid%2Fres%2Fraw)


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

After that, app did not work. But when this code is deleted, app can work. Is 
it possible to use SL4A with PyQt GUI in this example?

Regards

Original comment by edips...@gmail.com on 18 Feb 2014 at 12:55

@GoogleCodeExporter
Copy link
Author

PyDroid example's error is pyqt-error.txt:
Traceback (most recent call last):
  File "/data/data/org.kde.necessitas.example.PyDroid/files/main.py", line 35, in <module>
    main()
  File "/data/data/org.kde.necessitas.example.PyDroid/files/main.py", line 20, in main
    droid=android.Android()
  File "/data/data/org.kde.necessitas.example.PyDroid/files/python/lib/python2.7/android.py", line 34, in __init__
    self.conn = socket.create_connection(addr)
  File "/home/tsheasha/GUC/Bachelors/android-python27/python-build/build/lib/python2.7/socket.py", line 553, in create_connection
gaierror: [Errno 8] hostname nor servname provided, or not known.

Original comment by edips...@gmail.com on 18 Feb 2014 at 5:07

@GoogleCodeExporter
Copy link
Author

PyDroid / SL4A issue moved to 
http://code.google.com/p/android-python27/issues/detail?id=35

Original comment by anthony....@gmail.com on 25 Feb 2014 at 10:49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant