Python: Detect Android
Asked Answered
C

7

6

I'm surprised to find very little info on this topic. I want to detect if a user is running Android. I'm using:

platform.dist()

This perfectly detects all all OSs and different Linux distros. However, when running this on Android, it system name is always returned as "Linux". Is there any way to detect Android? I do not want to include any 3rd party modules, since this has to be as portable as possible. Is there maybe some specific Android function that I can call in a try-catch?

Chansoo answered 29/12, 2017 at 7:25 Comment(0)
S
15

You can do this with Kivy

from kivy.utils import platform

if platform == 'android':
    # do something

EDIT:

As I said, I cannot use any 3rd party libs

Take a look at how Kivy implemented it.

def _get_platform():
    # On Android sys.platform returns 'linux2', so prefer to check the
    # presence of python-for-android environment variables (ANDROID_ARGUMENT
    # or ANDROID_PRIVATE).
    if 'ANDROID_ARGUMENT' in environ:
        return 'android'
Shockproof answered 29/12, 2017 at 9:13 Comment(2)
As I said, I cannot use any 3rd party libs. The program has to be portable and can only use the standard python modules.Chansoo
There be an implied from os import environ... to those that want the whole answer without another clicked link.Circosta
R
3

As of Python 3.7, you can check for the existence of sys.getandroidapilevel():

import sys

if hasattr(sys, 'getandroidapilevel'):
    # Yes, this is Android
else:
    # No, some other OS
Ronaronal answered 14/1, 2022 at 12:24 Comment(0)
D
1
from os import environ
if 'ANDROID_BOOTLOGO' in environ:
   print("Android!")
else:
   print("!Android")
Diannadianne answered 12/2, 2021 at 15:40 Comment(1)
While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive-feedback/upvotes from users, when the code is explained.Sandpaper
W
0

System name is returned 'Linux' because Android is also based on the Linux Kernel.Android is the name of the distro but deep down its linux.

Webb answered 29/12, 2017 at 7:39 Comment(1)
Yes, I'm aware of that. But I still want to know if the device is Android or not, so that I can distinguish between Android and other distros. All other distro's names are returned (eg: Ubuntu, Fedora, etc), but not for AndroidChansoo
P
0

This is a very easy you can do it without kivy or with kivy module using hpcomt module

import hpcomt
os_name = hpcomt.Name()
print(os_name)

# Output
# Android

Perdure answered 4/4, 2021 at 7:45 Comment(0)
M
0

To detect if a user is running Android use this code below

from platform import platform

def is_android() -> bool:
    if 'android' in platform():return True
    return False
Metacenter answered 20/8, 2023 at 15:7 Comment(1)
On my Android platform.platform() is Linux-5.4.134-qgki-g544c77a8a651-aarch64-with-libc. No android in the string.Muhammadan
B
0

You can use an if statement to check if the paths of '/system/app' and '/system/priv-app' exist, if they both do then it is android. See below:

import os

if os.path.exists('/system/app') and os.path.exists('/system/priv-app'):
    print('Is Android!')
else:
    print('Is not Android')
Bine answered 14/5 at 19:46 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Elwandaelwee

© 2022 - 2024 — McMap. All rights reserved.