Android API level question - added in 31 but runs on Android 11

Viewed 302

I am trying to use LocationManager.FUSED_PROVIDER, which according to Android Dev documentation was added in API level 31 (Android 12)

My minSdkVersion is 26 for my app

When I put down LocationManager.FUSED_PROVIDER, Android Studio does NOT complain about my minSdkVersion. Also - my test phone on Android 11 (which I presume is API level 30) runs the app (inclusive of the location functionality) without issue

Can someone please help me understand why Android 11 can run a function making use of the FUSED_PROVIDER despite that being added on in API level 31? Does this imply that phones running down to my minSdkVersion of 26 should be able to handle as well?

Edit:

I just ran the following code on an Android 9 device (spare device I flash downgraded) and the device was able to retrieve the location without error -- not sure how this is happening since Android 9 is API v28

mLocationManager.requestLocationUpdates(LocationManager.FUSED_PROVIDER, 0, 0, mLocationListener);
1 Answers

When I put down LocationManager.FUSED_PROVIDER, Android Studio does NOT complain about my minSdkVersion.

Maybe you need to sync or rebuild or something. It does for me (min 19):

enter image description here

Can someone please help me understand why Android 11 can run a function making use of the FUSED_PROVIDER despite that being added on in API level 31?

My guess would be that it works because FUSED_PROVIDER is just a String. Once you compile, the system doesn't need or care about the name of the variable, it just cares about the string value, which obviously any version of Android handle. Prior version of Android won't understand what "fused" means, but they can certainly handle the string "fused" without exploding.

This is different from a function, like addTestProvider, which was also added in 31 which will crash your old version of Android quite decidedly.

Does this imply that phones running down to my minSdkVersion of 26 should be able to handle as well?

I would think so, yes. You can test this easily with the emulator.

Related