I have this line in my code:
linearLayout.setBackground(drawable);
setBackground() shows the following error:
Call requires API level 16 (current min is 14)
What does this mean?
Can I raise my API level?
What does a higher/lower API level mean?
Does it limit the amount of devices my app can be used on if the API level is higher?
What does this mean?
It means that the setBackground(Drawable) method was added in API Level 16, and older devices do not have it. However, your app's minSdkVersion is 14. So, unless you take steps to avoid this line on API Level 14 and 15 devices, your app will crash on those devices.
Can I raise my API level?
You could set your minSdkVersion to 16.
Does it limit the amount of devices my app can be used on if the API level is higher?
Yes. If you say that your minSdkVersion is 16, then devices running a version of Android older than that cannot run your app.
At the time of this writing, about 90% of Android devices accessing the Play Store are on API Level 16 or higher (i.e., are running Android 4.1 or higher).
You can read more about the concept of API levels in the documentation. Note that this documentation is a bit old, in that it focuses on the minSdkVersion being defined in the manifest. In Android Studio projects, minSdkVersion is usually defined in your app module's build.gradle file.
Can I raise my API level?
You can set API to 16 it will limit device below 16 but it will perfectly work on devices API 16 and higher
Or else You can use API check and set
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.ready) );
} else {
layout.setBackground( getResources().getDrawable(R.drawable.ready));
}
Use a validation method that will be supported only in API LEVEL >= 16 (note the use of ContextCompat class:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
linearLayout.setBackground(ContextCompat.getDrawable(this, drawableid));
}
Use:
layout.setBackgroudResource(R.drawable.ready);
Related
This question already has answers here:
Android: Get TargetSDKVersion in runtime
(5 answers)
Closed 2 years ago.
I am trying to determine the Build SDK level from within my app. I tried
Log.i( "MYAPP", "SDK="+Build.VERSION.SDK_INT );
I previously had been building for level 28, but recently upgraded to level 30.
My module's current build.gradle file included:
android {
compileSdkVersion 30 // was 28
defaultConfig {
applicationId "com.ramrod.MyApp"
minSdkVersion 23
targetSdkVersion 30 // was 28
more ...
Strangely, logcat still shows level 28.
I've re-synced, did a clean and rebuilt the app but no change.
What's going on
?
As you can see in the documentation here, SDK_INT returns the OS version of the device in which the app is running, and not the targetSdkVersion you're looking for.
public static final int SDK_INT
The SDK version of the software
currently running on this hardware device.
This value never changes
while a device is booted, but it may increase when the hardware
manufacturer provides an OTA update.
To get the targetSdkVersion you need to use ApplicationInfo.targetSdkVersion class:
packageManager.getApplicationInfo(youPackage, 0).targetSdkVersion;
Build.VERSION.SDK_INT
returns 28 instead of 29 when running on Android Q emulator. Is there anything I am missing? I am trying to add logic specifically for Android Q but I do not know how to determine this version correctly.
app.gradle file contains
targetSdkVersion = 'Q'
compileSdkVersion = 'android-Q'
Before the API is finalized and officially becomes API 29 (where you'd use compileSdkVersion 29, etc), you must use BuildCompat.isAtLeastQ():
Checks if the device is running on a pre-release version of Android Q or newer.
Note: This method will return false on devices running release versions of Android. When Android Q is finalized for release, this method will be deprecated and all calls should be replaced with Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q.
Note that Ian's solution requires AndroidX and is only available from Java/Kotlin code.
If your project is not ready for AndroidX just yet, or you need the value in a resource or the manifest, you can use bool resources:
Create res/values/bools.xml and put <bool name="isQ">false</bool> in there
Create res/values-v29/bools.xml and put <bool name="isQ">true</bool> in there
At this point, if you refer to the isQ resource, you will get true on Android Q and higher devices, false otherwise.
From the article https://developer.android.com/distribute/best-practices/develop/target-sdk,
Google Play will require that new apps target at least Android 8.0 (API level 26) from August 1, 2018, and that app updates target Android 8.0 from November 1, 2018.
The following code is from my app.
1: Does it mean that targetSdkVersion must be greater than or equal to 26 ?
2: Does it mean that minSdkVersion can be 21 ?
Code
defaultConfig {
applicationId "info.dodata.mirror"
minSdkVersion 21
targetSdkVersion 26
versionCode 9
versionName "1.09"
archivesBaseName = "My-V" + versionName
}
1: Does it mean that targetSdkVersion must be greater than or equal to
26 ?
Yes.
2: Does it mean that minSdkVersion can be 21 ?
Yes. (or any lower version you need to support)
From the documentation:
android:minSdkVersion
An integer designating the minimum API Level required for the
application to run. The Android system will prevent the user from
installing the application if the system's API Level is lower than the
value specified in this attribute. You should always declare this
attribute.
android:targetSdkVersion
An integer designating the API Level that the application targets. If
not set, the default value equals that given to minSdkVersion. This
attribute informs the system that you have tested against the target
version and the system should not enable any compatibility behaviors
to maintain your app's forward-compatibility with the target version.
The application is still able to run on older versions (down to
minSdkVersion).
It says method call requires API level 23. My code below,
But it is not possible because, following is from android official android.widget.Editor.java source file in API 19.
Also associated constants TYPE_APPLICATION_SUB_PANEL was added from api level 1. Also I used this successfully on Xamarin under api 14.
What is wrong here?
It simply show that setWindowLayoutType method does not available for API level 14.
you have to add API level condition.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
}
Did you install api 23 sdk? You must download from android sdk manager.
I'm curious about what is the sense in setting both target and minimal supported API version when starting a new Android Studio project. I mean, if I set that minimal API is, say, 8, then I won't be able to use features from 22 (which could be my target), because it would break compatibility with API 8.
if I set that minimal API is, say, 8, then I won't be able to use
features from 22
You can use API level >= 8 features in application, but you have to check OS version of the device first, see following code, that's how you can maintain compatibility
if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB){
//use features of API 3.0
} else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB_MR1){
//use features of API 3.1
} else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB_MR2){
//use features of API 3.2
}else{
// and so on....
}
Other then code, use can use resource folders on the basis of API level, like:
values-v11
values-v12
values-14
....
and
drawable-v11
drawable-v12
drawable-14
....
This is not correct. By setting target API to 8 you can't use any features added after, but specifying minimum API is just a guarantee that you application will work on such devices. You don't guarantee that it provide all features on such devices, nor minimum API level restricts what features it could use when running on later versions of OS.
Look at PE history - all win32 executable files are compatible with MSDOS, but all they say after executing is just "This is not a MSDOS program.". Similar to this, it is your decision what features you provide on which OS you support.