0

I am trying to understand how Android Studio determines if a code is available in a certain API. When using MediaStore.setRequireOriginal, Android Studio warns me that "this call requires API level 29". Does Android Studio check that this code is available in previous Android version sources?

photoContentUri = MediaStore.setRequireOriginal(photoContentUri)

I am trying to understand how it knows this.Android Studio screenshot showing "Call requires API level 29"

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167

3 Answers3

2

The linter just knows all the APIs in all the versions. You don't need to download all the previous Android version sources (I was wondering how Android Studio's Linter knew about older versions when I only had API level 29 and 30 sources downloaded on my machine).

As you can see, lint now has a database of the full Android API such that it knows precisely which version each API call was introduced in.

Lint API Check page

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
1

The Short Answer:

It's set by the developer, And Android Studio just compares your minSdkVersion set in build.gradle file with the required api level.

The Longer Answer:

When you get this warning on a method, just CTRL+click on it to go to the source class, and there you will find it annotated @RequiresApi or/and @TargetApi, for example :

class MediaStore{
    @RequiresApi(api = 29)
    @TargetApi(29)
    void setRequiredOriginal(...){}
}

Your build.gradle file:

defaultConfig {
    minSdkVersion 23
    ...
}

Android Studio compares minSdkVersion to @RequiresApi or/and @TargetApi at the time you call the method MediaStore.setRequiredOriginal(...); and warn you if minSdkVersion is less that the recommended api.

Please note that there are differences between @RequiresApi and @TargetApi, sometimes you find them used along with each other but sometimes just one of them. For more about difference between the two see: https://stackoverflow.com/a/50578783/10005752

khalid3e
  • 399
  • 3
  • 14
0

There is something in build.gradle of application module like:

defaultConfig {
    minSdkVersion 23
    targetSdkVersion 30
}

So you can change the "minSdkVersion" to 29, and the warning message disappear ...

And if not:

  • With android OS version >= 29: your code works normally
  • With android OS version <29: might be an exception occurs
mtdot
  • 312
  • 2
  • 10
  • Thanks, but I am not looking to remove the warning, as I can do that already. I am actually supporting lower API levels too with a different code path, and am just using conditionals and the `@RequiresApi(Build.VERSION_CODES.Q)` annotation. **I am asking 'how' Android Studio does it.** – Ben Butterworth Sep 24 '20 at 13:27
  • The prerequisties of executing a code for Android Studio means, the code block needs to be annotated with either `@RequiresApi` or `@TargetApi`, or if the code block is surrounded within a condition where only specific devices with API levels can enter. If you call `if (Build.VERSION.SDK_INT >= 29)` and you open two brackets, the code inside will definitely be executed for API 29 devices and above. That's how Android Studio determines it, and that's how the annotations work. – Furkan Yurdakul Sep 24 '20 at 13:33
  • `I am asking 'how' Android Studio does it` I thinks it's a lint rule, you could refer [this repo](https://github.com/googlesamples/android-custom-lint-rules) to know how to implement that. – mtdot Sep 24 '20 at 13:37