How does Android Studio know about backwards compatibility issues? - android

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.

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

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

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

Related

Android studio doesn't warning newapi. Why?

My Android Studio version:
version: 4.2.2
Settings->Inspections-> Calling new methods on older versions: DID CHECK (severity: error)
My project:
compileSdkVersion 30
minSdkVersion 21
targetSdkVersion 30
My snippet code:
myActivity.getWindowManager().getCurrentWindowMetrics();
Method getCurrentWindowMetrics() of class WindowManager is ONLY available on Android 11 (see here: https://developer.android.com/reference/android/view/WindowManager#getCurrentWindowMetrics())
My question:
why don't Android Studio warn me about using new method:getCurrentWindowMetrics on older OS?
This bug happens when using navigation-fragment-ktx library version 2.4.x. It's fixed in recent versions, see a bug report.
They mention in the top right corner that they added it in the new Api level 30. So indirectly they say it is available since that version. But I'm with you, that it is confusing because of the missing backward compatibility.

Can't update the project to Nougat (api 25)

I have an Android Studio project. The target- and compileSdkVersion for it - is 21. I want to change it to 25.
When I do this, some of the classes of the Android standard library become unavailable. For example org.apache.http.client.HttpClient.
here are some screenshots
Android classes before the rise of SDK version. You can see how many classes in org.apache.http.*
And Android classes after the rise of SDK version:
I also looked into the android reference, and saw that there is as little classes for 25 api, and (I was confused) for 21 api also. And I noticed that they are the same as I have after upgrade
Why in the case of 21 api, I have more classes? And how to fix it? Waiting for help. Thanks.
Since Api 23 (Marshmallow - Android 6.0), the Apache HTTP Client has been removed.
To continue using the Apache HTTP APIs, you must first declare the
following compile-time dependency in your build.gradle file:
android {
useLibrary 'org.apache.http.legacy'
}

Find higher api 23 calls than minSdk 21

Hello there are some answer on this topic, but none which worked for me so far.
My build.gradle looks like this
compileSdkVersion 23
buildToolsVersion "23.0.3"
minSdkVersion 21
targetSdkVersion 23
Somewhere in my Fragments I called the method getContext() and that crashed the app on a Lollipop 5.0 device. It works fine on Marshmallow 6.0.
The Fragment is imported from the non support library package.
import android.app.Fragment;
and since I have compileSDK on 23 I can call the method getContext() withing the Fragment to get the Context.
This will lead to a crash on Lollipop 5.0 and 5.1 since that method was added with API 23 and not API 21,22.
My Question is, how can I find such high level calls in Android Studio when the min SDK is below that?
how can I find such high level calls in Android Studio when the min
SDK is below that?
AFAIK Android studio normally warn the developer whenever they are using any methods which is not completely backward compatible till minSDKVersion defined by the application. So, At that point of time, you can check out current version of device and call relevant other method accordingly.
However, for some reason its not showing any lint warning while calling getContext() method. So, It seems we have to deal with it now.
Go to
-> Analyze -> Inspect Code -> run code inspection
Then in the result view there is
"Project Name"
- Android > Lint > Correctness
- calling new methods in older versions
Under (calling new methods in older versions) all unavailable calls should be listed
Now that we're in 2022, I found a more appropriate solution.
Run the gradlew lint command in Terminal and waiting for it to finish will generate a report file.
Wrote HTML report to file:///C:/Users/Administrator/path/to/project/module/build/reports/lint-results-$flavorName$buildType.html
open this file in browser, find InlinedApi and NewApi and you will find all of it.

Android studio check for compatibility with API < 19

I have an application project with this settings:
android {
compileSdkVersion 21
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
buildTypes {
release {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
And I have a block with try-with resources which not use checking if Build.SDK is less than API 19.
I don't get any errors from IDE about it.
I ran a program on emulator with target SDK Android 4.1.2 and everything is fine, also checked on the device with Android 4.2.2. The program is invoke this code, checked with the debugger.
Is everything ok? I expect that there is might be compatibility errors from IDE but there's not.
If I try to create a new project in IDE with same minVersionSdk 10, I've got a error from IDE about the compatibility.
But in my working project I don't get it.
I don't know, is there any other settings for compatibility, not in build.gradle and AndroidManifest.xml? Why is it working on API < 19 ?
UPDATE:
you need to check Lint settings in Android Studio.
Editor - Inspections
Android Lint, Calling new methods on older versions
Why is it working on API < 19 ?
According to this: https://code.google.com/p/android/issues/detail?id=73483 It "mostly works" from API 15, and you tested on an API 16 emulator (4.1.2).
Issue raiser states:
Since it was unhidden in API level 19, try-with-resources is backwards compatible down to API level 15.
Google member replies (edited down):
AutoCloseable was in ics. and it's just an interface; it's javac that emits code to actually do the closing. iirc there are fewer classes that are AutoCloseable in ics than we actually unhid
so "backwards compatible" is a bit misleading. "mostly works" is closer to the truth.
Because it's not fully compatible, the warning is from API 19.
As to why you are not seeing the warning, I think that is down to your very old buildToolsVersion which dates back to December 2013. https://developer.android.com/tools/revisions/build-tools.html
You should always keep your Build Tools component updated by downloading the latest version using the Android SDK Manager
If you only use methods which were created before API 10 (You can check here) everything is ok.
If you are not sure, you can run lint (by right clicking on your root folder) and then you can check if lint warn you about deprecated method usage.
If you want more explanation don't hesitate to comment

What does the language level setting in android studio 0.3.2 Do?

In the latest release of Android Studio (0.4.2), a new setting has been added when creating a new project, namely Language Level
I want to know what that refers to, and the consequence of selecting any of the provided options.
The doc doesn't give me a clear picture of this and i don't want to select an option when i don't know what that will result in?
Thanks
It's about what Java language level you want to use. KitKat supports full Java 7, Gingerbread and up support Java 6 and older versions are Java 5. At least when it comes to the core Java APIs. Roughly the same setting is mentioned here.
You can often use language features that were added in one version in an older version if the compiler knows how to do it. For example the Diamond Operator was added in Java 7 but you can still use that feature in Java 6 since
List<String> list = new ArrayList<>();
actually compiles into the same thing as
List<String> list = new ArrayList<String>();
did.
The "try with resource" construct on the other hand can't be compiled into legal Java 6 compatible code and is therefore exclusive to Apps that require KitKat and up.
The answer can be phrased better, IMHO. The docs are clear about Java 7 and below.
This setting affects which Java language features you can use in your source code.
All Java language features that you use will be compiled so that the code will work in all Android versions, except for the "try with resources" language feature.
From the docs: "If you want to use try with resources, you will need to also use a minSdkVersion of 19. You also need to make sure that Gradle is using version 1.7 or later of the JDK. (And version 0.6.1 or later of the Android Gradle plugin.)"
Java 7 support was added at build tools 19. You can use now features like the diamond operator, multi-catch, try-with-resources, strings in switches, etc. Add the folowing to your build.gradle.
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
Gradle 1.7+, Android gradle plugin 0.6.+ are required.
Note, that only try with resources require minSdkVersion 19. Other features works on previous platforms.

Categories

Resources