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'
}
Related
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.
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
I am developing app in API level 23 (Marshmallow) but some methods and libraries are deprecated in API 23 i need to downgrade project to API 21 or API 22. But when i change my API level and build my project it gives error
I have tried all methods all things that are mentioned on this forum but none could helped me..
is give error for some value-23.xml file.. i have changed the name of that file from value-23.xml to value-22.xml but android studio said "FILE usder the build folder are generated and should not be edited"
You have changed your compileSdkVersion below 23, but you are still trying to use a version-23 edition of appcompat-v7. Change your appcompat-v7 statement in the dependencies of your build.gradle file, choosing a version that matches your compileSdkVersion. Or, move your compileSdkVersion back to 23 and address your deprecation issues in some other fashion.
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
Having issues with appcompat-v7 and compileSdkVersion, the app build target is API 10 so I set compileSdkVersion 10 to compile the code safely, as expected it works well with support-v4: the app compiles and runs on API 10 devices.
Then I want to add appcompat-v7 to dependencies (or replace v4 with it) and perform clean re-build of the app without any changes at the code or resources, build fails at the R generation stage unless the compileSdkVersion is set to a higher value.
I understand it as the v7 library is using some values unavailable at API 10. It raises the question of how someone can continue to write safe code and use v7 without need to manually check API level of each variable and method. Is there a way to keep using v7 (that is claimed to be "designed to be used with Android 2.1 (API level 7)") and compileSdkVersion 10 ?
Apparently at the newest Intellij version Lint produces errors if the methods form the API above minSdkVersion is used (can be enabled/disabled at Preferences-Inspections, expand Android Lint at the list, look for Calling new methods on older versions or use "NewApi" annotation to suppress the error if needed).
That'll have to do until some kind of dynamic resources compilation is introduced. I'm going to leave the question here for a future reference.