1

I'm trying to get Python 3 to run natively on android. By "native" I mean native Linux, just Androids variation of it. I'd like to be able to simply open a terminal and type "python /path/to/script.py" so Kivy and Qslide will not work (to my knowledge, feel free to prove me wrong :) ). Normally Termux would do this but I don't like the terminal app just the Linux packages it supplies, prefer to use another called Terminal Emulator by Jackpal(typo or maybe just wrong?).

I've tried booting into TWRP (I don't have root otherwise) and copying the necessary Python binaries and lib folder n the Termux data directory to /system/bin and /system/lib but Python fails saying it can't find "libandroid-support.so". I realize Termux's Python is probably modified in some way to account for the driectory structure requred by an android app and therefore couldn't find the .so even after I copied it to /system/lib.

So my question(s) are: How might I make the solution I tried work? or Are there any other ways to get Python installed as a native Linux app*?

*like I said I want it to be just like on a desktopn LInux where I can open ANY terminal app and use python just like any other command.
  • Why don't you simply run a Linux distro on Android device? It's fun to have power. But that needs root and in some cases a custom built kernel for chroot, namespaces etc. No apps need to be installed other than a simple terminal emulator. Termux packages won't work that way because paths of linked libraries are hard coded. Also, most of the Termux packages or other Android specific solutions are limited one way or the other. – Irfan Latif Dec 8 '18 at 20:22
1

Fantastic, I believe the solution for you is this awesome project called Python-for-Android (Py4A) . not only does it allow you to run scripts on your android device. You can go one step further because the project include simple draft instructions on creating a standalone, downloadable Android APK package.

First, I would start by going to the readme section of Py4A. There is a recipes (script) that will fit your needs. The python3 recipe that allows Python3 to run as a "Stand alone" on Android.

This excerpt from Linuxjournal explains script installation on your android device:

"Running on a Smartphone

To run a Python script on your physical Android device, install SL4A together with Python for Android on your handset, then transfer your script.

To install SL4A on your physical Android device, enable the Unknown Sources option in your device's Application settings. This setting is required to enable the installation of non-Market apps on your phone. With this done, you can follow the same steps you used when installing SL4A and Python on your emulator. To speed things up a little, install Barcode Scanner from the Android Market and use it to “read” the QR Codes from your desktop screen.

Transferring Your Script to Your Handset There are a number of ways to get your script onto a real phone. I've found the success of using something like Bluetooth connectivity or USB cabling arrangements can very much depend on the hardware on which you're running. What works on one handset, doesn't on another, and so on. Your mileage may vary depending on your actual device. When I need to transfer a file, I've come to rely on a solution that works no matter which handset I use (as long as the handset can talk to a local Wi-Fi network). What I do is switch on the OpenSSH server on my development PC running Linux, then use the AndFTP file transfer app on the handset to scp files from the desktop to the phone. AndFTP is available from the Android Market as a free download and installs in minutes. Once I connect to my desktop with AndFTP, I can navigate to a directory of my choice, mark the files that I want, then download them to my SD card on the handset.

AndFTP works well, and I've come to depend on it for all my Android file transfers (see Resources). Just be sure to transfer your scripts to /sdcard/sl4a/scripts on the phone to ensure that your script names appear within the SL4A list of scripts.

With your script file transferred to your physical device, start SL4A as before, tap your app's name and tap the run wheel. As expected, your app runs just as it did on the emulator, only faster! I haven't included a screenshot of the app running on a real phone for two reasons. First, it looks exactly the same as it did in the emulator, and second, it's running on your device, so you can take a look at it there!

Creating an APK There's one further kink to SL4A that might interest you. The project includes draft instructions on creating a standalone, downloadable Android APK package (see Resources). Once created, the APK file bundles your custom Python script with information that allows other Android users to install Python for Android automatically onto their handsets and then run your app from the smartphone's main menu of apps. Describing the process of creating the APK likely would take another article, so I leave it to the brave among you to try out the instructions on the SL4A Wiki"

Python-for-android website.

  • Not all devices allow runnng Linux within android and I've hard bricked a phone by deleting the Linux directory and having symlinks to Androids own system files followed so I really don't like that option. – Myersj281 Dec 10 '18 at 0:52
  • Would it be possible to compile Python 3 from github.com/python/cpython/tree/master/Python on Android (hopefully with little to no modification to fix path variables) and (lacking a package manager for Linux apps) move everything into place from TWRP? – Myersj281 Dec 10 '18 at 1:01
  • @Myersj281 vendors never want you run anything on the device other than their officially provided ROM. It's in their great benefit and also in user's benefit if he/she doesn't like playing with technicalities. Unlocking bootloaders, rooting, custom kernels, custom ROMs, everything has some degree of risk with it. But it gives you freedom. I've been using a lot of native code on my Android phone for last 2 years, including multiple Linux distros (Ubuntu, Debian, Void, ArchLinux) without any loss. My use is compiling programs natively rather than using cross compilers on PC. But it's a choice :) – Irfan Latif Dec 10 '18 at 16:48
  • True symlink can make things weird. Is this the symlink you mentioned: ln -sf "${TOOL_PREFIX}/sysroot/usr/include/"{linux,sys}"/soundcard.h" or ln -sf $(NDK_SYSROOT)/usr/include/linux/soundcard.h \ if so look at the path variable it includes sysroot. The sysroot is actually a merer copy of the file system of your target platform. Which is a directory which is considered to be the root directory for the purpose of locating headers and libraries. I suppose you can replace the sysroot option with another option. sysroot makes things less compilcated or change the crosscompiler. – Bo Lawson Dec 10 '18 at 21:07
  • @Myersj281 forgot to tag you first comment. Anyway, is it possible? most likely yes but difficult make it work. In my experience crosscompiling can be difficult but its easier than after its compiled figuring out, researching, and fixing thing to get it to work and be usable. It can get very frustratingat least for me. I quickly scanned through source code for sl4a and python3. I did not see any rm (delete) of a linux directory. Could you give a link the source file where the the linux directory is deleted please?? – Bo Lawson Dec 10 '18 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.