Android: Relationship between minSdkVersion and targetSdkVersion - android

I've read everything in the Google documentation that I could find regarding minSdkVersion and targetSdkVersion but I'm still not entirely sure how to use these properly.
My current application has a minSdkVersion of 8 and a targetSdkVersion also of 8, and works fine as such.
I changed my targetSdkVersion to 18 and suddenly half of my application is no longer functioning as expected. Even some simple HTTP requests were not working.
From what I understood from reading the docs, targetSdkVersion is for using features of a certain level of API, is this correct? Why were simple HTTP requests not functioning as expected when targeting 18? Are they used differently?
If someone could give a short, clean explanation on how these two attributes work, I would be very grateful.
Also, if I keep my target at 8, will it work okay on newer devices?

minSdk is the minimum api level that the device should have to be able to install your app.
In your code you can use all functions that are available since targetSdk or lower.
Example:
if your targetSdk is 14 and your minSdk is 8, and you want to use a method that is available since sdk 11, before you call the method you would have to check whether the device's sdk is 11 or higher.
If you use functions that require a higher api level than your minSdk, your code will compile but it will crash if the function is called on a device lower than targetSdk. That's why you have to check for it before you call the method or use the class.

Related

Check min version of compatibility of Android app

For an Android application, how can I test what the lowest version/API of Android it can work on, without manually changing API level on the emulator?
The minSdkVersion set in the app level gradle file is the lowest API version it will work on.
There are other factors though...for example:
Lets say the minSdkVersion is 21...but you call a method that wasnt introduced until api 25 (and dont handle that), then it will crash...and not really work.
Open build.gradle of your app there should be written minSdkVersion which it supports.

Issues executing code on higher API levels Android

My minSDK version is 16 and my targetSDK version is 27. The compileSDK version is 28.
Since the targetSDK version is 27 it should run on Oreo(8.0.0) without issues but some of the features don't work as intended. However they do work fine on Nougat.
Why is this so?
The targetSDK is stating what you built in the app to be able to handle. So, there might be a new feature in API 28 and you're saying your code was build against API 27 so if you're running on API 28 and there is support on 28 for how you used the api before, then it will try to maintain your API 27 coded behavior. There still exists the possibility your API 27 code will result in different or wrong behavior if run on API 28 though.
Please review the documentation as well:
https://developer.android.com/guide/topics/manifest/uses-sdk-element
"To maintain your application along with each Android release, you should increase the value of this attribute to match the latest API level, then thoroughly test your application on the corresponding platform version."

Implications of setting a lower targetSdkVersion for the app

I have read the Android documentation here and this similar question here, but have a question on the real life implications of this version.
My app had a targetSdkVersion of 25 while in development, and I was trying it out with a SDK level 27 (Android 8.1) phone with no issues.
My question is specifically about broadcast receivers. Prior SDK 26, you can register for broadcasts in manifest file and you will receive all the broadcasts you registered for. But 26 changed that to severely limit this mechanism that you cannot receive any implicit broadcasts, like Wifi connection changes. And instead they recommend using JobScheduler, with a minimum period of 15 minutes. This would absolutely not work for my app.
Now what I want to understand is, what are the implications of releasing my app with a target SDK version of 25, given that I do not use any features of latest SDK (27)?
Would there be any restrictions for a phone running SDK > 25 to use my app?
Thank you.
The scenario you are mentioning is know as forward compatibility in Android. You can think it in other way as well, you app is already in Google Play Store and user updated his device software. In such case, Android/Google strongly focus on that existing apps built against prior SDKs should not break when the user updates to a new version of Android.
If any API of API level 25 you are using in your app is not deprecated in API level 27, there is no issue. It is completely fine to set targetSdkVersion : 25.
But it is always recommended to compile your app with latest available SDK so that you can minimize the risk of crash/warnings of your code with respect to latest SDK.
So basically what you should follow is:
minSdkVersion <= targetSdkVersion <= compileSdkVersion
Hope this will help you.
When you run your application on a version of on Android that have a higher version than the targetSdkVersion specified this may lead to some issues.
For example, if the targetSdkVersion is less than 14, your app may get an option button in the UI.
Thus, to have the compatibility of the android version you better need to set the compileSdkVersion and targetSdkVersion the same.

I want to keep the minSdkVersion 14 and the targetSdkVersion 19 and the compiledSdkVersion 23. Is it is technically OK?

I want to keep the min Sdk Version 14 and the target Sdk Version 19 and the compiled Sdk Version 23. Is it is technically OK or not?
if it is OK then will my App run properly on all Android devices?
Yes, it is technically OK, & if you are not using any dangerous permission then it will work on all android devices above API 14. If you are using any permission related task, then you will have to check the permission every time before using, otherwise your app might crash over some devices.
Read for more details:
https://developer.android.com/guide/topics/manifest/uses-sdk-element.html
https://medium.com/google-developers/picking-your-compilesdkversion-minsdkversion-targetsdkversion-a098a0341ebd
It's technically OK. Have a look at these very well described answers in other posts:
https://stackoverflow.com/a/27629181/4315095; https://stackoverflow.com/a/26694276/4315095

Newbie in Android: minSdkVersion vs compileSdkVersion

Hy,
I have some basic doubts about developing with android studio:
minSdkVersion 15: with this configuration I am forcing to use only
the features from api level 15 and not higher. Is correct? For what I
read I dont think so
minSdkVersion 15 and compileSdkVersion 24: with this configuration I
can use api level until level 24. Is this correct?
minSdkVersion 15 and compileSdkVersion 23: with this configuration if I
use api features from api 23, this application wont work in an android
device with api level 20 for example, right?
With the previous configuration, and android device with api level 20
will be able to download and install the application because its api is
higher than the minSdkVersion 15 but will not be able to run because it
has features from api 23, right?
If I want to make sure that I want to use an api level and not higher
because of the previous problems commented, the configuration minSdkVersion 15 and compileSdkVersion 15 is the only way? is a common
practice?
Thanks a lot!
minSdkVersion 15: with this configuration I am forcing to use only the features from api level 15 and not higher. Is correct?
No. A minSdkVersion of 15 means that you do not want your app to run on devices with a lower API level than that. 15 corresponds to Android 4.0.3.
minSdkVersion 15 and compileSdkVersion 24: with this configuration I can use api level until level 24. Is this correct?
Your IDE will allow you to write code using classes, methods, fields, etc. from API Level 24. Your IDE should also warn you that using classes, methods, fields, etc. that were added to the SDK after API Level 15 may result in runtime errors.
minSdkVersion 15 and compileSdkVersion 23: with this configuration if I use api features from api 23, this application wont work in an android device with api level 20 for example, right?
Well, API Level 20 is a special Android version for the first-generation Android Wear devices. But if we switch that to API Level 19 (Android 4.4), if you blindly call an Android 5.1 (API Level 23) method, you will crash on Android 4.4. This is why the IDE will warn you about this, why you often see checks of BuildConfig.VERSION.SDK_INT, and why the SDK has these ...Compat classes (which try to hide a lot of these version differences).
With the previous configuration, and android device with api level 20 will be able to download and install the application because its api is higher than the minSdkVersion 15 but will not be able to run because it has features from api 23, right?
The device will attempt to run the app. How far it gets depends on how well the app is written. Again, you can create an app that uses newer-API features that gracefully degrades to run on older devices. This is not significantly different than how a Web site or Web app might want to use the latest HTML5 features but gracefully degrades to handle older or less-capable browsers.
If I want to make sure that I want to use an api level and not higher because of the previous problems commented, the configuration minSdkVersion 15 and compileSdkVersion 15 is the only way?
No. Again, the IDE will (usually) yell at you when you try using SDK features that are acceptable for the compileSdkVersion but are newer than the minSdkVersion.
is a common practice?
Not since 2010 or so.
FWIW, here is the documentation on this subject, limited as it may be.

Categories

Resources