sofdigital / DroidPython

Python app development with SL4A in Android Studio
MIT License
55 stars 17 forks source link

Best practices #3

Closed viperfx closed 8 years ago

viperfx commented 8 years ago

My use case is quite simple, I would like to run a function that uses a library in python and then have the results of that (dictionary) available in Java.

What is the simplest way to achieve this? Should I build upon the custom facade example?

michaelrinderle commented 8 years ago

is there a specific need for dataset in java?

viperfx commented 8 years ago

Not sure what you mean. But there is a specific need for using Python.

On Tuesday, 19 April 2016, michael notifications@github.com wrote:

is there a specific need for dataset in java?

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/ainsophical/DROID_PYTHON/issues/3#issuecomment-212147842

Tharshan

michaelrinderle commented 8 years ago

you said you need to transfer the python dict into a java data type. If i knew why you need to do it i could probably point you in the right direction.

viperfx commented 8 years ago

The library I am using in the python script returns a python dictionary with its keys value pairs. I need to access these values in Java. So the data structure need to be converted to the appropriate Java data type.

On Tuesday, 19 April 2016, michael notifications@github.com wrote:

you said you need to transfer the python dict into a java data type. If i knew why you need to do it i could probably point you in the right direction.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/ainsophical/DROID_PYTHON/issues/3#issuecomment-212153282

Tharshan

michaelrinderle commented 8 years ago

i know don't how, why, or need to access a java data type with a python script but if you need to, then yes. creating a custom sl4a method with a facade class and registering it but easiest is adding it in the Acustomfacade.java file.

something like this...

@Rpc(description = "Send Python Dict to Java Code")
public hashmap <String, Integer> dict2hashmap(@RpcParameter(name = "dict") Hashmap dict) throws InterruptedException {

        *** Do stuff with dict *** 
}

import android
droid = android.Android()
a ={}
droid.dict2hashmap(a)

this is pseudo-code so take it with a grain of salt. i don't know exactly how youre going to have to map the dict to a hashmap nor if it's possible.

viperfx commented 8 years ago

I am currently porting a React Native application to Android and a core part of the application uses python. I am using some functionality in the youtube-dl (https://github.com/rg3/youtube-dl) library that I wish to not replicate in Java. Therefore, I am hoping that I can call this library in the script and output the results to Java so that I can use them in my React Native application.

So in short I need to be able to call the function from Java which runs in Pythons and then returns the results back to Java.

I hope that makes sense.

Having explained all this, does this change anything? Does your suggestion still go towards solving this?

I tried putting the youtube-dl package files into the python_extras zip, both in the root and also the site-packages. I modified the hello.py and only imported the youtube_dl package at the top and it seems after that the APK fails to run. However, I am not able to see what actually went wrong. Is there a way to see the error output from Python?

I tried running the commands you outlined in the wiki but when I run the python hello.py command in the adb shell, I get a permission denied. I have rooted my nexus 5 but I may need to put a custom bootloader on the device to get superuser access.

If you would like, I can send you an email with the files I have in my raw folder, so you can replicate the issue easier.

viperfx commented 8 years ago

Okay got su permission working and I found the issue through the adb shell.

Here is what happens when I try importing youtube_dl

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/data/com.android.python27/files/packages/python/youtube_dl/__init__.py", line 15, in <module>
    from .options import (
  File "/data/data/com.android.python27/files/packages/python/youtube_dl/options.py", line 7, in <module>
    from .downloader.external import list_external_downloaders
  File "/data/data/com.android.python27/files/packages/python/youtube_dl/downloader/__init__.py", line 3, in <module>
    from .common import FileDownloader
  File "/data/data/com.android.python27/files/packages/python/youtube_dl/downloader/common.py", line 9, in <module>
    from ..utils import (
  File "/data/data/com.android.python27/files/packages/python/youtube_dl/utils.py", line 27, in <module>
    import ssl
  File "/data/data/com.android.python27/files/packages/python/ssl.py", line 60, in <module>
    import _ssl             # if we can't import it, let the error propagate
ImportError: dlopen failed: cannot locate symbol "OPENSSL_add_all_algorithms_noconf" referenced by "/data/data/com.android.python27/files/python/lib/python2.7/lib-dynload/_ssl.so"...

Is this a known issue? Is there a way to ignore this - I know that I am not making any SSL requests.

Should I rebuild python with SSL support?

michaelrinderle commented 8 years ago

not sure. i have a commercial app that has sent hundreds of emails in the last 6 months over ssl with smtplib with no problem so i know it's not completely broken. if i have time i'll look into it.

viperfx commented 8 years ago

OK. Well from searching around it seems the libcrypto also needs to be linked. Is the google code the only location to get the python binaries and shared files? Do you have instructions on building python for this project?

michaelrinderle commented 8 years ago

no instructions but you have to take the python source code and compile it ( with -PIE flags, because android <= 5.0 needs position independent executables ) with android ndk.

viperfx commented 8 years ago

Is that something you can share? If its something you have done already then I would not have to replicate your work. I can take a look at the flags and adjust as needed.