goocreations
goocreations

Reputation: 3066

Python: Detect Android

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?

Upvotes: 7

Views: 5310

Answers (7)

HackedX_Prototype
HackedX_Prototype

Reputation: 1

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')

Upvotes: 0

GoldenVadim
GoldenVadim

Reputation: 1

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

Upvotes: 0

rdb
rdb

Reputation: 1617

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

Upvotes: 4

Honey Pots
Honey Pots

Reputation: 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

Upvotes: -1

Hrithik Joseph
Hrithik Joseph

Reputation: 11

from os import environ
if 'ANDROID_BOOTLOGO' in environ:
   print("Android!")
else:
   print("!Android")

Upvotes: 1

Ronie Martinez
Ronie Martinez

Reputation: 1275

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'

Upvotes: 14

royalbhati
royalbhati

Reputation: 306

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.

Upvotes: 0

Related Questions