This page gives details on accessing Android APIs and managing otherinteractions on Android.
Python Coding for Android Minecraft PE: The Raspberry PI version of Minecraft lets you script in Python, making fractal designs, drawings, turtle graphics, simple games, etc. The same thing can be done on a desktop version with my Raspberry Jam Mod (here is my Instructable on how to use i. 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! QPython - Python on Android. In most cases, script can get your jobs done as good as the native application. Now you can make it with QPython's help. QPython is a script engine which runs Python programs on android devices. It also can help developers develop android applications.
Storage paths¶
If you want to store and retrieve data, you shouldn’t just save tothe current directory, and not hardcode /sdcard/ or some otherpath either - it might differ per device.
Instead, the android module which you can add to your –requirementsallows you to query the most commonly required paths:
app_storage_path() gives you Android’s so-called “internal storage”which is specific to your app and cannot seen by others or the user.It compares best to the AppData directory on Windows.
primary_external_storage_path() returns Android’s so-called“primary external storage”, often found at /sdcard/ and potentiallyaccessible to any other app.It compares best to the Documents directory on Windows.Requires Permission.WRITE_EXTERNAL_STORAGE to read and write to.
secondary_external_storage_path() returns Android’s so-called“secondary external storage”, often found at /storage/External_SD/.It compares best to an external disk plugged to a Desktop PC, and canafter a device restart become inaccessible if removed.Requires Permission.WRITE_EXTERNAL_STORAGE to read and write to.
Warning
Even if secondary_external_storage_path returns a paththe external sd card may still not be present.Only non-empty contents or a successful write indicate that it is.
Read more on all the different storage types and what to use them forin the Android documentation:
A note on permissions¶
Only the internal storage is always accessible with no additionalpermissions. For both primary and secondary external storage, you needto obtain Permission.WRITE_EXTERNAL_STORAGEand the user may deny it.Also, if you get it, both forms of external storage may only allowyour app to write to the common pre-existing folders like “Music”,“Documents”, and so on. (see the Android Docs linked above for details)
Runtime permissions¶
With API level >= 21, you will need to request runtime permissionsto access the SD card, the camera, and other things.
This can be done through the android module which is available per defaultunless you blacklist it. Use it in your app like this:
The available permissions are listed here:
Other common tasks¶
Dismissing the splash screen¶
With the SDL2 bootstrap, the app’s splash screen may be visiblelonger than necessary (with your app already being loaded) due to alimitation with the way we check if the app has properly started.In this case, the splash screen overlaps the app gui for a short time.
To dismiss the loading screen explicitly in your code, use the androidmodule:
You can call it e.g. using kivy.clock.Clock.schedule_once
to run itin the first active frame of your app, or use the app build method.
Handling the back button¶
Android phones always have a back button, which users expect toperform an appropriate in-app function. If you do not handle it, Kivyapps will actually shut down and appear to have crashed.
In SDL2 bootstraps, the back button appears as the escape key (keycode27, codepoint 270). You can handle this key to perform actions when itis pressed.
For instance, in your App class in Kivy:
Pausing the App¶
When the user leaves an App, it is automatically paused by Android,although it gets a few seconds to store data etc. if necessary. Oncepaused, there is no guarantee that your app will run again.
With Kivy, add an on_pause
method to your App class, which returns True:
With the webview bootstrap, pausing should work automatically.
Under SDL2, you can handle the appropriate events (see SDL_APP_WILLENTERBACKGROUND etc.).
Advanced Android API use¶
android for Android API access¶
As mentioned above, the android
Python module provides a simplewrapper around many native Android APIS, and it is included by defaultunless you blacklist it.
The available functionality of this module is not separately documented.You can read the source onGithub.
Also please note you can replicate most functionality without it usingpyjnius. (see below)
Plyer - a more comprehensive API wrapper¶
Plyer provides a more thorough wrapper than android for a much largerarea of platform-specific APIs, supporting not only Android but alsoiOS and desktop operating systems.(Though plyer is a work in progress and not allplatforms support all Plyer calls yet)
Plyer does not support all APIs yet, but you can always use Pyjnius tocall anything that is currently missing.
You can include Plyer in your APKs by adding the Plyer recipe toyour build requirements, e.g. --requirements=plyer
.
You should check the Plyer documentation for details of all supportedfacades (platform APIs), but as an example the following is how youwould achieve vibration as described in the Pyjnius section above:
This is obviously much less verbose than with Pyjnius!
Pyjnius - raw lowlevel API access¶
Pyjnius lets you call the Android API directly from Python Pyjnius isworks by dynamically wrapping Java classes, so you don’t have to waitfor any particular feature to be pre-supported.
This is particularly useful when android and plyer don’t alreadyprovide a convenient access to the API, or you need more control.
You can include Pyjnius in your APKs by adding pyjnius to your buildrequirements, e.g. --requirements=flask,pyjnius
. It isautomatically included in any APK containing Kivy, in which case youdon’t need to specify it manually.
The basic mechanism of Pyjnius is the autoclass command, which wrapsa Java class. For instance, here is the code to vibrate your device:
Things to note here are:
- The class that must be wrapped depends on the bootstrap. This isbecause Pyjnius is using the bootstrap’s java source code to get areference to the current activity, which the bootstraps store in the
mActivity
static variable. This difference isn’t alwaysimportant, but it’s important to know about. - The code closely follows the Java API - this is exactly the same setof function calls that you’d use to achieve the same thing from Javacode.
- This is quite verbose - it’s a lot of lines to achieve a simplevibration!
These emphasise both the advantages and disadvantage of Pyjnius; youcan achieve just about any API call with it (though the syntax issometimes a little more involved, particularly if making Java classesfrom Python code), but it’s not Pythonic and it’s not short. These areproblems that Plyer, explained below, attempts to address.
Python Runner For Android Studio
You can check the Pyjnius documentation for further details.
Latest versionReleased:
Android APK packager for Python scripts and apps
Project description
python-for-android is a packaging tool for Python apps on Android. You cancreate your own Python distribution including the modules anddependencies you want, and bundle it in an APK along with your own code.
Features include:
- Different app backends including Kivy, PySDL2, and a WebView withPython webserver.
- Automatic support for most pure Python modules, and built in supportfor many others, including popular dependencies such as numpy andsqlalchemy.
- Multiple architecture targets, for APKs optimised on any givendevice.
For documentation and support, see:
- Website: http://python-for-android.readthedocs.io
- Mailing list: https://groups.google.com/forum/#!forum/kivy-users orhttps://groups.google.com/forum/#!forum/python-android.
Documentation
Follow the quickstartinstructionsto install and begin creating APKs.
Quick instructions: install python-for-android with:
(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 NDKas 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.:
For full instructions and parameter options, see thedocumentation.
Support
If you need assistance, you can ask for help on our mailing list:
- User Group: https://groups.google.com/group/kivy-users
- Email: kivy-users@googlegroups.com
We also have #support Discord channel.
Contributing
We love pull requests and discussing novel ideas. Check out the Kivyproject contribution guide andfeel free to improve python-for-android.
See ourdocumentationfor more information about the python-for-android development andrelease model, but don't worry about the details. You just need tomake a pull request, we'll take care of the rest.
The following mailing list and IRC channel are used exclusively fordiscussions about developing the Kivy framework and its sister projects:
- Dev Group: https://groups.google.com/group/kivy-dev
- Email: kivy-dev@googlegroups.com
We also have #dev Discord channel.
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 andeasier-to-extend interface. If you'd like to browse the old toolchain, itsstatus is recorded for posterity at athttps://github.com/kivy/python-for-android/tree/old_toolchain.
In the last quarter of 2018 the python recipes were changed. Thenew recipe for python3 (3.7.1) had a new build system which wasapplied to the ancient python recipe, allowing us to bump the python2version number to 2.7.15. This change unified the build process forboth python recipes, and probably solved various issues detected over theyears. These unified python recipes require a minimum target api level of 21,Android 5.0 - Lollipop. If you need to build targeting anapi level below 21, you should use an older version of python-for-android(<=0.7.1).
On March of 2020 we dropped support for creating apps that use Python 2. The latestpython-for-android release that supported building Python 2 was version 2019.10.6.
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]
Release historyRelease notifications | RSS feed
2021.9.5
2020.6.2
2020.4.29
2020.3.30
2019.10.6
2019.8.9
2019.7.8
2019.6.6.post0
2019.6.6
0.7.0
0.6.0
0.5.3
0.5.2
0.5.1
0.5
0.4
0.3
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size python_for_android-2021.9.5-py3-none-any.whl (516.5 kB) | File type Wheel | Python version py3 | Upload date | Hashes |
Filename, size python-for-android-2021.9.5.tar.gz (985.0 kB) | File type Source | Python version None | Upload date | Hashes |
Python Runner For Android Version
CloseHashes for python_for_android-2021.9.5-py3-none-any.whl
Algorithm | Hash digest |
---|---|
SHA256 | 383254500966739d42766318ed508e90d05c7907096bead48a1076c67797a2d3 |
MD5 | 0d575f463033f5f5285500191b31805f |
BLAKE2-256 | 08675395480ec4346434cc5a1c11ab1f19c7a6b742f42a6f2e45b4842ecfaac9 |
Python Interpreter
CloseHashes for python-for-android-2021.9.5.tar.gz
Algorithm | Hash digest |
---|---|
SHA256 | 46305120df040d964514a02b7a5cbc93048d1a69ef9ad67d14cd6cdac536337c |
MD5 | 1c2ad32a574109dd20a857774f77bae3 |
BLAKE2-256 | 8712d6825fbd0ee7e3da541cda4d71c3b8e6621b3926ac72e11c4c23e0c169c1 |