Python to android compiler

python-for-android

python-for-android is a packaging tool for Python apps on Android. You can create your own Python distribution including the modules and dependencies you want, and bundle it in an APK or AAB along with your own code.

  • Different app backends including Kivy, PySDL2, and a WebView with Python webserver.
  • Automatic support for most pure Python modules, and built in support for many others, including popular dependencies such as numpy and sqlalchemy.
  • Multiple architecture targets, for APKs optimised on any given device.
  • AAB: Android App Bundle support.

For documentation and support, see:

Documentation

Follow the quickstart instructions to install and begin creating APKs and AABs.

Quick instructions: install python-for-android with:

pip install python-for-android 

(for the develop branch: pip install git+https://github.com/kivy/python-for-android.git )

Test that the install works with:

To build any actual apps, set up the Android SDK and NDK as described in the quickstart. Use the SDK/NDK API level & NDK version as in the quickstart, other API levels may not work.

With everything installed, build an APK with SDL2 with e.g.:

p4a apk --requirements=kivy --private /home/username/devel/planewave_frozen/ --package=net.inclem.planewavessdl2 --name="planewavessdl2" --version=0.5 --bootstrap=sdl2 

If you need to deploy your app on Google Play, Android App Bundle (aab) is required since 1 August 2021:

For full instructions and parameter options, see the documentation.

Support

If you need assistance, you can ask for help on our mailing list:

Contributing

We love pull requests and discussing novel ideas. Check out the Kivy project contribution guide and feel free to improve python-for-android.

See our documentation for more information about the python-for-android development and release model, but don’t worry about the details. You just need to make a pull request, we’ll take care of the rest.

The following mailing list and IRC channel are used exclusively for discussions about developing the Kivy framework and its sister projects:

License

python-for-android is released under the terms of the MIT License. Please refer to the LICENSE file.

History

In 2015 these tools were rewritten to provide a new, easier-to-use and easier-to-extend interface. If you’d like to browse the old toolchain, its status is recorded for posterity at at https://github.com/kivy/python-for-android/tree/old_toolchain.

In the last quarter of 2018 the python recipes were changed. The new recipe for python3 (3.7.1) had a new build system which was applied to the ancient python recipe, allowing us to bump the python2 version number to 2.7.15. This change unified the build process for both python recipes, and probably solved various issues detected over the years. These unified python recipes require a minimum target api level of 21, Android 5.0 — Lollipop. If you need to build targeting an api level below 21, you should use an older version of python-for-android (<=0.7.1).

Читайте также:  Настройки css для браузеров

On March of 2020 we dropped support for creating apps that use Python 2. The latest python-for-android release that supported building Python 2 was version 2019.10.6.

On August of 2021, we added support for Android App Bundle (aab). As a collateral, now We support multi-arch apk.

Contributors

This project exists thanks to all the people who contribute. [Contribute].

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Script for cross-compiling Python to Android

sfalexrog/python-xcompile

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

A script to build Python for Android, based on the recipe by wandering_logic: https://github.com/wandering-logic/android_x86_python-3.4

It compiles cpython from source, then uses it and the NDK toolchains to compile cpython for target platforms.

Note that even though with some patches compilation succeeds, there are still major parts of Python standard library that fail. In my case, even some basic modules (like cmath) fail tests on my Android device. You have been warned.

Note also that this script will change your cpython source tree.

In order to use this script, you need:

  • A recent version of Python 3 on your Build machine,
  • cpython sources,
  • wandering_logic’s patches,
  • Standalone toolchains for target platforms.

Note that you can create a standalone toolchain for a given platform by executing

$ $/build/tools/make-standalone-toolchain.sh --toolchain=TOOLCHAIN_NAME \ --platform=TARGET_PLATFORM --install-dir=$/toolchain-target-triplet 

So, for example, to create a toolchain for x86 that uses gcc-4.9 and targets android-19, you want to run:

$ $/build/tools/make-standalone-toolchain.sh --toolchain=x86-4.9 \ --platform=android-19 --install-dir=/tmp/android-toolchains/i686-linux-android 

(Yes, toolchains in the $/toolchains don’t always use the right target triplets)

In order to compile Python for Android you have to:

  • edit the paths in the script, or
  • provide the paths as environment variables, or
  • provide the paths as command-line parameters.

You have to provide the paths for:

  • A folder that contains all your NDK toolchains (variable NDK_TOOLCHAINS_PATH , option —ndk-toolchains-dir )
  • A folder that contains your cpython sources (variable PYTHON_SOURCE_PATH , option —python-source )
  • A folder that will contain intermediate build files (variable BUILD_DIR_PATH , option —build-dir )
  • A folder that will contain compilation results (variable OUTPUT_PATH , option —output-dir )
Читайте также:  Convert string to byte and byte to string in java

Additionally, if your toolchains contain version suffices, you have to provide them with NDK_TOOLCHAIN_SUFFIX variable or —ndk-version-suffix option.

By default, the script will try to build binaries for all supported platforms. To override that, you should specify a comma-separated list of platforms as a —platforms (variable TARGET_PLATFORMS ) option.

So, to compile Python for arm and x86, with sources in /tmp/cpython-hg , intermediate builds in /tmp/pybuild , toolchains in /opt/android-toolchains , with a suffix of 4.9 , and results in /opt/andpython, you want to run: $ python3 compile.py —ndk-toolchains-dir /opt/android-toolchains —ndk-toolchain-suffix 4.9
—python-source /tmp/cpython-hg —build-dir /tmp/pybuild —output-dir /opt/andpython
—platforms arm,x86

For now, Python for Android can’t be built without applying patches first. Moreover, due to Android quirkiness, configuration script actually gets some values «wrong» (notably, the HAVE_GETHOSTBYNAME_R macro: there are actually prototypes for gethostbyname_r and gethostbyaddr_r in Android headers, but there’s no gethostbyaddr_r in bionic), there are patches that should be applied after configuring build.

Patches that should be applied to the source tree should be put in the prepatch folder and will be run before the configure script. Patches that should be applied to the build tree should be put in the postpatch folder and will be run after the configure script.

By default, there should be patches from wandering_logic and a couple of patches to build some modules statically and use pgen from build machine. Feel free to add more patches!

For now, Python executable is not built as a position-independent executable. This means that you can’t run the built binary on Android 5.0 and up.

By default, your /sdcard on your device is mounted with the noexec flag that prevents you from running the binary directly from your device. If you really want to run the interpreter on your device, you should copy the python3 executable to some place in your device ( /data/python3 , for exampe) that you’re allowed to run executables from. You can place the shared library ( libpython3.4.so.1.0 ) anywhere, but make sure to copy Python modules (that reside in lib/python3.4 ) and to provide the path in sys.path for your scripts!

This way, though, you should provide an LD_PRELOAD variable that points to the Python shared library:

$ LD_PRELOAD=/path/to/libpython3.4.so.1.0 /path/to/python3.4 

This should give you a running interpreter (or, at least, a helpful error message).

The correct way to use Python on Android would probably be to use only the shared library embedded in an application (with some glue code to make Python and Java interact).

About

Script for cross-compiling Python to Android

Источник

3 Ways to Convert Python App into APK

In August 2020, I started a medium blog series called Building Android App in Python, where I explained the usage of Kivy and Kivymd. These libraries allow you to create cross-platform apps using Python. In that series, I described how android apps are configured in Python, it’s limitations, and various key elements that build up an android app. At the end of the series (3rd part), I mentioned that I will convert an app into APK and deploy it on a cloud platform. Here is the article about the same! Here I will discuss three ways in which this conversion can be performed. If you haven’t followed this series or you are unaware of this library then I highly recommend you that check out the first part of the series:

Читайте также:  Шаблоны php социальной сети

Building Android Apps With Python: Part -1

Step by Step Guide to Build Android Apps using Python

The Challenge

The Python apps build with Kivy cannot be directly transferred to android phones as these devices only support APK (Android Application Package) and we need to package them properly. This conversion process is only possible on a Linux system (for now) as the main components of this conversion, buildozer and python-for-android are currently supported on Linux based systems only. This adds up a challenge for budding developers who generally who use a windows machine for all the coding purpose. Other challenges include failed app conversions, app crashing on the start, or not able to connect to the internet. While some issues need extra attention from your side, I will provide you 3 different ways to successfully convert the Python app to APK.

Before moving ahead, let’s look at the flow of the conversion:

  1. Making sure that the app entry point file is named as main.py
  2. Installing the dependencies.
  3. Initialize the buildozer
  4. Edit the specs file
  5. Start the process

We will see 1,2 and 4 steps in detail in upcoming sections as they don’t require any explicit change but it is necessary to understand how to configure the buildozer specs properly.

The buildozer spec file is automatically generated while initializing the buildozer. The file contains the whole configuration and the app is built following this only. There are a few lines that need to be modified in that file before proceeding with the next steps (There are a total of 339 lines in the actual file):

The source code for both the apps (Python file, APK, and buildozer spec) can be found at this GitHub repository.

If you don’t want to use the APK generated into your devices then you simply host them online using a free service called Appetize.io which allows you to run android apps in browsers!

Conclusion

In this article, we saw how to configure the deployment file needed to convert the Python file into APK. Then we saw 3 different ways to do this conversion. A local machine is the best option but if don’t want to mess with your system then I would suggest Google Colab as it has great processing power. If you are looking for CI/CD type solution then GitHub actions will suit your requirements. With this, we come to an end of this article as well as the development series.

If you don’t want to miss such articles then make sure to follow me on medium to receive all the notifications. With that said, Sayonara!

You can connect with me here:

Источник

Оцените статью