0

Is there any way to get the android version (4 or 5 or 6 or 7) directly from python or pyjnius?

If so, could anyone make an example?

I tried kivy.platform but it only tells me if it is android or not and using platform.release from python gives me the linux version

Nick
  • 545
  • 12
  • 31

3 Answers3

1

You can use pyjnius to retrieve the version via the Java API, as described e.g. here.

inclement
  • 29,124
  • 4
  • 48
  • 60
0

You can use my module from here (On page click download > Only file) and put downloaded file into folder with your kivy project. Add to your .py file:

import PyAndroid

Now you can use:

print(PyAndroid.android_version())

To print android version or

print(PyAndroid.android_name())

To print android name

For all functions you can read documentation.

I hope I helped :D

olokelo
  • 135
  • 1
  • 1
  • 8
  • This project web-site is now dead. The code is still available on [PyPi](https://pypi.org/project/PyAndroid/), but there is no documentation and no support. Spoiler: It looks in a file called "/system/build.prop" for the entries for "ro.build.version.release" and "ro.build.version.sdk". Please consider reviewing and refactoring the code before including it in a project. – Oddthinking Jul 11 '20 at 15:57
  • 1
    @Oddthinking Yes, I know. I was working on this project back when I was 11 years old. Now it's obsolete (android permissions changed since then and now non-root user in some android versions doesn't even have rights to read file /system/build.prop). I will keep this "project" on PyPi because it may be useful for someone but for now it doesn't have any practical usage. BTW thanks for your attention ;) – olokelo Jul 14 '20 at 09:00
0

I had the same question. I wanted to know the version to decide whether to reference a particular class which didn't exist in versions earlier than SDK 23.

Then I realised there was a better way.

try:
    AudioFocusRequest = autoclass('android.media.AudioFocusRequest')
except jnius.JavaException:
    # Not available on earlier versions of Android.
    AudioFocusRequest = None

Now, I don't need to know what the version actually is, and there won't be any bugs where I get the threshold values wrong. I just check whether the AutoFocusRequest exists before I use it.

Oddthinking
  • 24,359
  • 19
  • 83
  • 121