Android

When Python runs inside an Android app (for example one built with python-for-android, Kivy or BeeWare), PlatformDirs is platformdirs.android.Android. App directories live in the app's private package folder, which Android removes when the app is uninstalled, and media directories point to the user's shared storage. This page explains how platformdirs detects Android and finds the package folder, and lists every directory.

How Android Is Detected

platformdirs uses the Android backend only when all of these hold:

  1. The process looks like Android: sys.getandroidapilevel() exists (official Android builds of CPython provide it), or the environment has ANDROID_DATA=/data and ANDROID_ROOT=/system.
  2. It is not a shell environment: neither SHELL nor PREFIX is set (see below).
  3. The app's package folder can be found.

If any check fails, the Unix backend is used instead.

Finding the Package Folder

The package folder is something like /data/user/0/com.example.app. platformdirs looks for it in this order and caches the result for the life of the process:

  1. pyjnius, if importable: it asks the python-for-android activity (org.kivy.android.PythonActivity.mActivity) for its files directory, or, in a python-for-android service process where the activity is null, the service (org.kivy.android.PythonService.mService). The parent of the files directory is the package folder.
  2. sys.path: the first entry matching <package folder>/files, where the package folder is under /data/data/<pkg>, /data/user/<user id>/<pkg>, or the device-protected /data/user_de/<user id>/<pkg>.
  3. Adopted storage: an entry under /mnt/expand/<volume uuid>/... with the same structure, for apps moved to an SD card formatted as internal storage.

If you construct platformdirs.android.Android yourself and no folder is found, app directory properties raise RuntimeError: cannot find the Android app folder: python4android and pyjnius failed and no app folder is on sys.path.

App Directories

Examples use appname="SuperApp" and <pkg> for the package folder (for example /data/user/0/com.example.app). appauthor and roaming are ignored. Every site_* directory returns its user_* counterpart, since an app has no system-wide storage.

Property Path
user_data_dir <pkg>/files/SuperApp
user_config_dir <pkg>/shared_prefs/SuperApp
user_preference_dir same as user_config_dir
user_cache_dir <pkg>/cache/SuperApp
user_state_dir same as user_data_dir
user_log_dir <pkg>/cache/SuperApp/log (without /log if opinion=False)
user_runtime_dir <pkg>/cache/SuperApp/tmp (without /tmp if opinion=False)
user_bin_dir <pkg>/files/bin
user_applications_dir same as user_data_dir

With version="1.0", the version comes right after the app name: <pkg>/cache/SuperApp/1.0/log.

Note: Logs and runtime files live inside the cache directory, which Android may clear when storage runs low. Write logs elsewhere if you must keep them.

Media Directories

Media directories point to the shared storage of the Android user the app runs as, /storage/emulated/<user id>. The user ID is taken from the package folder path (/data/user/<id>/... or /data/user_de/<id>/...), defaulting to 0.

Property Path
user_documents_dir /storage/emulated/0/Documents
user_downloads_dir /storage/emulated/0/Download
user_pictures_dir /storage/emulated/0/Pictures
user_videos_dir /storage/emulated/0/Movies
user_music_dir /storage/emulated/0/Music
user_desktop_dir /storage/emulated/0/Documents/Desktop
user_projects_dir /storage/emulated/0/Documents/Projects
user_publicshare_dir /storage/emulated/0/Documents/Public
user_templates_dir /storage/emulated/0/Documents/Templates
user_fonts_dir /storage/emulated/0/Documents/fonts

For the first five, platformdirs asks Android (Environment.getExternalStoragePublicDirectory) through pyjnius when available, and caches the answer. Android lets an app without "All files access" create top-level shared folders only from its standard list, which is why Desktop, Projects, Public, Templates and fonts are placed under Documents.

App-private directories need no permission. Reading or writing shared storage requires the appropriate Android storage permissions, which platformdirs does not request.

Creating Directories

With ensure_exists=True, app directories are created with mode 0700, as on other POSIX systems. Media, fonts and bin directories are never created; user_applications_dir is created because it is the data directory. Android's system-owned parent paths are accepted by the ownership checks.

Shell Environments (Termux, Pydroid)

Apps such as Termux and Pydroid give you a Linux shell on Android. They set SHELL or PREFIX, and platformdirs then uses the Unix backend: paths follow the XDG layout under the shell's HOME, and all XDG_* variables work.

See Also