7

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?

8 Answers 8

14

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'
Sign up to request clarification or add additional context in comments.

3 Comments

As I said, I cannot use any 3rd party libs. The program has to be portable and can only use the standard python modules.
There be an implied from os import environ... to those that want the whole answer without another clicked link.
This is specific to Kivy and may not be portable (e.g. consider Python on Termux). Checking for sys.getandroidapilevel() as in @rdb's answer is better.
4

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

1 Comment

Thanks, finally a clean stdlib solution. This should be the accepted answer.
1
from os import environ
if 'ANDROID_BOOTLOGO' in environ:
   print("Android!")
else:
   print("!Android")

1 Comment

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.
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.

1 Comment

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 Android
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

1 Comment

On my Android platform.platform() is Linux-5.4.134-qgki-g544c77a8a651-aarch64-with-libc. No android in the string.
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')

1 Comment

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?
0

Get environment variables from OS and define a boolean function.

from os import environ

def is_android():
    return True if 'ANDROID_ROOT' in environ else False

if is_android():
    print('Is android!')
else:
    print('Is another OS')

# show all environment variables: bash> printenv
for i in environ:
    print(f'{i}: {environ[i]}')

Comments

-1

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

1 Comment

hpcompt is a third-party package, and it seems to be specific to termux, so not portable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.