1

I'm doing some work on an internal, legacy Android app. The target SDK version is set to 28 in the Gradle build file, and the compile SDK version is set to 33.

The issue I face is that Android Studio lint is showing me an error regarding a missing permission, even though the indicated permission is not actually required at the API level that I am targeting. The permission that I'm being warned about is the BLUETOOTH_CONNECT permission, but the docs indicate that this permission is only required if an app targets API level 31 or above. (At my target API level, I am requesting the BLUETOOTH permission instead, as the documentation prescribes.)

The app compiles and runs fine. Bluetooth functionality is available to the app, even when it is run on a device that is running a version of Android >= API level 31.

Being an app internal to our organization, the app runs on a limited and controlled set of mostly older devices, and I'd just as soon not add the BLUETOOTH_CONNECT permission if I don't have to, particularly since it is a runtime permission and adds more hassle. My question is whether the lint warning is actually as spurious as it seems to me, and is therefore safe to ignore, or if there is something I am overlooking in this situation that could come back to bite me--not in future, if and when I bump up the target SDK level, but as things stand today.

I realize it's best practice to target the latest SDK. But it still seems to me that the lint error is misplaced as written, and I'd like to confirm that my understanding here is correct.


Edit: As far as I can tell, the lint error is in fact a false positive under the circumstances, for the reasons I have tried to lay out above.

I believe the proximate cause of the error's showing up in my project is that the SDK I'm compiling against annotates several of the Bluetooth-related methods that I am calling with @RequiresPermission (example) and the lint tool is simply basing its errors on this, without in any way taking into account the role played by the target SDK level.

(This, notwithstanding that a @RequiresLegacyBluetoothPermission annotation is also present at the source level, which speaks to my situation.)

For the time being, I've decided to suppress Bluetooth-related missing permission warnings by adding the following to my lint.xml file:

<issue id="MissingPermission">
   <ignore regexp="BLUETOOTH_SCAN|BLUETOOTH_CONNECT"/>
</issue>

The use of the regular expression (which is matched against the detailed error message per the documentation) should prevent suppression of missing permission lint errors that are unrelated to the situation I have described here.

E_Cross
  • 21
  • 1
  • 4

1 Answers1

0

When you are setting android target SDK version to 28 and compile SDK to 33, the compiler expects that your app should work fine on all the devices that are in that range, from API 28 until 33.

To sum up:

  • compileSdkVersion The compileSdkVersion is the version of the API the app is compiled against.

  • targetSdkVersion: The targetSdkVersion is supposed to indicate that you have tested your app on (presumably up to and including) the version you specify.

Therefore, eventhough I would not recommend to target only older devices. You can put this settings:

compileSDKVersion = 30
minSDKVersion = 24
targetSDKVersion = 30

If you need help with that permission, you can look online for it, or create another StackOverflow question regarding that issue.

  • 1
    But the thing is, the documentation for Bluetooth permissions (linked in my question above) is clear that whether the older or newer permissions model is used depends on which version of the SDK is _targeted_ by the app, not which version of Android the device is actually running. This is borne out by my testing, where my code without the newer permissions works just fine on a device that is at API level 33. Newer versions of Android sometimes adjust their behavior for apps that target a lower SDK version, and that's what seems to be happening here. So the question remains, why the lint error? – E_Cross Jun 15 '23 at 04:38
  • I have editted the answer, please check – Ignacio E. Loyola Jun 15 '23 at 10:07
  • Changing the settings as you suggest might make the lint warning go away (although I can't do it because I need to compile against v33 for other reasons). In any case, my question still stands, as it seems to me there was never a cause for the lint error to begin with as long as my target SDK level was set to 28: per the documentation, and as confirmed by my testing on a real device running v33. I know I'm kicking this tech debt down the road, and I should probably just break down and get the target SDK level up to match the compile level, but that's another issue. – E_Cross Jun 15 '23 at 20:12