16

I am collecting some data from my android application. How to run a python script in my android application that uses the collected data as input and generates some output?

Shubhamhackz
  • 7,333
  • 7
  • 50
  • 71
Prateek Ratnaker
  • 817
  • 11
  • 35
  • 1
    Possible duplicate of https://stackoverflow.com/questions/101754/is-there-a-way-to-run-python-on-android (minus the implied 'and write my entire code'-part of course) – tevemadar Dec 28 '17 at 11:22
  • 1
    @tevemadar answer to your linked question suggests using kivy. However, it will limit me to just using kivy..what about other functionalities GUIs service broadcast receivers which are more easier to do using plain android...don't want to develop the entire app using just kivy and the bundle it into apk....hence this isn't a duplicate...what I was looking for was some api using which my java and python code can interact in android environment .Regarding "write my entire code part" , I can give a simple activity code and a simple python script which adds two numbers retrieved from java code – Prateek Ratnaker Dec 28 '17 at 12:13
  • 1
    Have had this doubt for a long time. Couldn't find anything appropriate though. Only dropped words like [SL4A](https://github.com/damonkohler/sl4a) or [Kivy](https://kivy.org) etc. Hope someone provides a clear cut answer with **example** here. – Kaushik NP Dec 28 '17 at 15:42
  • I would not dismiss the other posts in that question, like the ones about QPython, it has a call-python-from-android-app example: https://github.com/qpython-android/app-call-qpython-api, and while the example itself is more than a year old, the repo itself has recent activity even from today: https://github.com/qpython-android – tevemadar Dec 28 '17 at 18:35
  • Why not manipulating the data using Java or Kotlin since you are inside an Android app? – JP Ventura Dec 29 '17 at 06:18
  • 1
    that won't be feasible for me – Prateek Ratnaker Jan 01 '18 at 07:23
  • You can compile Python with the required packages for Android, and use them to achieve what you're looking for. It should be fairly simple, assuming you don't have to channel heavy data in between the app and the script. – Sakchham Jan 02 '18 at 18:25
  • 1
    @SakchhamSharma could you please provide a basic example for the same? – Prateek Ratnaker Jan 03 '18 at 11:08
  • @PrateekRatnaker Kivy is an example for that, it compiles the python interpreter for the target platform (arm, arm64...) and uses them to execute the scripts. You can get the python sources and compile them using the NDK. To interact with the executable you can have a look at this https://stackoverflow.com/a/5642593/6525469 – Sakchham Jan 04 '18 at 18:03

2 Answers2

2

Consider Jython, Jython is an implementation of the high-level, dynamic, object-oriented language Python seamlessly integrated with the Java platform. The predecessor to Jython, JPython, is certified as 100% Pure Java.

  1. mbedded scripting - Java programmers can add the Jython libraries to their system to allow end users to write simple or complicated scripts that add functionality to the application.
  2. Interactive experimentation - Jython provides an interactive interpreter that can be used to interact with Java packages or with running Java applications. This allows programmers to experiment and debug any Java system using Jython.
  3. Rapid application development - Python programs are typically 2-10X shorter than the equivalent Java program. This translates directly to increased programmer productivity. The seamless interaction between Python and Java allows developers to freely mix the two languages both during development and in shipping products.

The awesome features of Jython are as follows,

  • Dynamic compilation to Java bytecodes - leads to highest possible performance without sacrificing interactivity.
  • Ability to extend existing Java classes in Jython - allows effective use of abstract classes.

Jython doesn't compile to "pure java", it compiles to java bytecode subsequently to class files. To develop for Android, one compile java bytecode to Dalvik bytecode. To be honest, this path of development is not official , thus you will run into lots of problem with compatibility of course.

Remario
  • 3,813
  • 2
  • 18
  • 25
  • Jython looks cool, but its page https://wiki.python.org/jython/JythonFaq/GeneralInfo says that Jython is not feasible to run in Android - can you suggest or link to a toolchain or process article for running Jython on an Android device? – WillC Sep 16 '18 at 13:03
0

You may use qpython,

QPython is a script engine that runs Python on android devices. It lets your android device run Python scripts and projects. It contains the Python interpreter, console, editor, and the SL4A Library for Android. It’s Python on Android! It offers the development kit which lets you easily develop Python projects and scripts on your Android device.

If you need to add some specifics libraries, according to qpython documentation install-libraries :

  • You can install most pure python libraries through pip_console.py which included from the 0.9.8.2 version

You may add --no-use-wheel argument when using pip_console.py to avoid unknown archive format :whl issue when installing some package.

and / or

  • You can upload your libraries into the /sdcard/com.hipipal.qpyplus/lib/python2.7/site-packages or /sdcard/com.hipipal.qpyplus/lib/python3.2/site-packages (For QPython3)

Regards

A. STEFANI
  • 6,707
  • 1
  • 23
  • 48