1

In my Java code, I can easily test the current API level at runtime with something like:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   doSomething();
}

How can I find the equivalent of Build.VERSION.SDK_INT in Delphi Rio?

I tried to look for int android_get_application_target_sdk_version() but I cannot find a Delphi unit containing that.

On the other hand, I found a function AConfiguration_getSdkVersion(Config: PAConfiguration) in the Androidapi.Configuration unit, but it needs a PAConfiguration as parameter, and I don't know how to get one.

EricC-59
  • 119
  • 2
  • 9

1 Answers1

2

It can be done this way:

uses
  Androidapi.JNI.Os;

...

  if TJBuild_VERSION.JavaClass.SDK_INT > SomeValue then

Where the value of SomeValue is the API level you want to check against.

Dave Nottage
  • 3,411
  • 1
  • 20
  • 57
  • 1
    Thank you. I tried if TJBuild_VERSION.JavaClass.SDK_INT >= TJBuild_VERSION_CODES.JavaClass.O then doSomething; but I had to replace TJBuild_VERSION_CODES.JavaClass.O with a hardcoded integer to avoid an exception. Could you complete your answer with TJBuild_VERSION_CODES.JavaClass.xxx please? – EricC-59 Aug 06 '21 at 09:23
  • Why would I add something that potentially causes an exception? As you've discovered, it's better to use an hardcoded integer. – Dave Nottage Aug 06 '21 at 21:14
  • I understand your point of view. You're right. I just don't like "magic numbers". Constants are better. – EricC-59 Aug 11 '21 at 14:28
  • The problem is that, as you've discovered, `TJBuild_VERSION_CODES.JavaClass.O` isn't defined in earlier SDK's which is somewhat akin to a catch-22. – Freddie Bell Aug 30 '21 at 18:44