Android studio check for compatibility with API < 19 - android

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

Related

How does Android Studio know about backwards compatibility issues?

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

Android Studio Lint - set API level for lint

Here is the thing. I have an app that is compatible with API 15 and above, but since it's pretty big and I've already hit a 65k methods limit, I had to make it a descendant of MultiDexApplication class.
This slows down the build times a bit, so I had to implement some optimization to speed up the process. I have the following code in my manifest, which significantly reduce build times when building for API >= 21 (taken from some other SO thread):
productFlavors {
dev {
minSdkVersion 21
}
prod {
minSdkVersion 15
}
}
Everything is working perfectly, but the problem is that during the development, Android studio thinks that my minSdkVersion SDK level is 21 (correctly), and the lint does not show me incompatible API (15-21).
What I really want is to be able to build with minSdkVersion set to 21 (fast build), but set the "lint minSdkVersion" to 15, so I see the parts of the code that are not compatible with older API than 21.
I tried to google it and also to look into AS lint preferences but I didn't find anything useful. Thanks for any suggestions. My current solution is to switch minSdkVersion in dev flavor to 21 and check if there's any error, but this is not really what I want.
This gist answers your question. It shows how to build the project with whatever dev minimum SDK value, while maintaining production minimum SDK value for Lint warnings.
To summarize the post, you can dynamically calculate the minSdkVersion value:
int minSdk = hasProperty('devMinSdk') ? devMinSdk.toInteger() : 15
apply plugin: 'com.android.application'
android {
...
defaultConfig {
minSdkVersion minSdk
...
}
}
In this example, we're checking if devMinSdk property defined, and if true - we're using it. Otherwise, we default to 15.
How do we pass devMinSdk value to build script? Two options:
Using command line:
./gradlew installDebug -PdevMinSdk=21
Using Android Studio preferences:
Go to Preferences (Settings on Windows) -> Build, Execution, Deployment -> Compiler -> put -PdevMinSdk=21 in Command-line Options text box.

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.

How to make a gradle android app compatible with above api version

I have an app that works fine on API 19. However, I want it to work well in other API's as well. I'm targeting API 10 and above.
My gradle build looks like this:
android {
compileSdkVersion 18
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
}
But when I run the app on API 10 the AVD manager says compatible: no
How should I change my build so that it is compatible for API 10 and above?
Your gradle config looks correct. Be sure to check your virtual device settings and compare to what you have defined in your manifest file. Also be sure to check any other modules you may have added to the project and their respective gradle and manifest files.

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