API target and min confusion - android

I am confused about the target build and sdk usage
Lets say I have this code
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//do xyz
}
Let say that I built against API 19 (kitkat) and my target api in manifest is 19 and my minimum supported api is 9
Now if a device with API 9 runs the above code, will it crash? I expect the answer is yes becasuse it will not understand what Build.VERSION_CODES.KITKAT means. However, what's the point of the check above then in first place?
Please help clarify this
Thank you

It won't crash. Simply the code within the if won't be executed. Build.VERSION_CODES.KITKAT is a constant field, and as you can read here, the constant fields are replaced with the numbers themselves by the compiler.

lesser versions of android will use the support library, if the check for kit-kat fails, it will revert to the closest possibkle form that version supports.... via the support library...
you cannot run you app on anything less than the minimum version, but it will find a way to run with less than the target version as long as its aboe the minimum

No, it won't crash, because its Build.VERSION.SDK_INT value is 9. It simply will not enter inside your if clause. Only devices that have API version 19 or above will run your code inside the if. Build.VERSION_CODES.KITKAT is equal to 19.

The code you posted won't crash because the class Build is created and compiled for every build of your app (as the R file) depending of the target API you set in the manifest
As you setup your target API to 19, the Build class will contain the field Build.VERSION_CODES.KITKAT because it exists starting from API level 19.

Related

android:navigationBarDividerColor requires API level 28

Previously, I implemented light and dark navigation bar, however now tags as android:navigationBarDividerColor and android:windowLightNavigationBar require API level 28 when they previously required API level 27.
It seems API level 28 does not even exist as next API level is called P.
Is there any solution to this problem? Thanks in advance.
UPDATE: it seems to work now on API 27 with latest support libraries
When the "next upcoming API" is still under development, its "name" is a letter (P in your case).
Once the final version of the API is available, the "name" is changed form a letter to a number (P to 28).
Final version of Android API 28 (former Android P) is available since early June 2018. Just use the SDK manager and you'll be able to download it ;-)
Note:
Sources for "Android SDK Platform xx" (28 in your case)" is not available right away. (not available ATTOW)It can takes a few weeks before they are available for download from the SDK manager.
Today I've updated compileSdkVersion, targetSdkVersion and buildToolsVersionfrom 27 to 28. Now I'm facing the same issue as #Teďourek described.
While it was working for me on 27, since the upgrade I now get lint errors:
Error: android:navigationBarDividerColor requires API level 28 (current min is 19)
Error: windowLightNavigationBar requires API level 28 (current min is 19)
This is strange because according to the docs for both attributes it says:
"added in API Level 27"
My temporary fix is to move the two style attributes to v28/styles.xml instead of v27/styles.xml
Since I would like to use lightNavBar + color also on Android 8.1.0 as previously, I would be happy if anyone knows a fix!
With compileSdkVersion and targetSdkVersion set to 29 it seems to be correct.
The XML properties tell me it was added in API level 27.
Only the Java Window properties are added in API level 28.

Error setSelectionFromTop() requires API level 21 (current min. is 10)

I'm getting an error after using this
StackOverflow answer:
setSelectionFromTop() requires API level 21 (current min. is 10)
and When I checked android API, it was added in API Level 1. How can I solve it?
After some googling it seams that yeah, it's a bug...
https://code.google.com/p/android/issues/detail?id=172621
As #pelotasplus says it's a bug and seems google still doesn't solve it till now. You can add #SuppressLint("NewApi") annotation to prevent lint check. See https://developer.android.com/studio/write/lint.html#src

Android Lint - NewApi check still fails despite suppression with #TargetApi

Following an unsuccessful Lint run, I attempted to fix an error by adding the #TargetApi(Build.VERSION_CODES.HONEYCOMB) attribute, but the next time Lint is run, the following error still shows for the getScaleX() function:
Can anyone shed some light on this?
The call to getScaleX() requires API level 11 (Honeycomb), as mentioned in the message window. The message also indicates that the minimum API level is 9 (as per the minSdkVersion setting).
The Lint tool is warning you that you are using a method supported only in newer SDK versions (11+), but have set to allow the application to run on devices that don't support this method (SDK versions 9 and 10).
See a more detailed description of what the NewApi Lint check does here: (search for NewApi) http://tools.android.com/tips/lint-checks
Suppress such warnings with caution, I'd suggest protecting the code with something like this:
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
...getScaleX()...
}

getLoaderManager.initLoader() can't be resolved under API level 10 in Android

in Android :
I want to use the method : getLoaderManager.initLoader() with API level 10 .
I know that this method requires API Level 11 or higher and i tried to use Android Support package but this package doesn't have this method .
what can i do ??
It sure looks to me like the Android Support Package has it. See the docs for android.support.v4.app.LoaderManager.initLoader().
Update:
For the equivalent of getLoaderManager, see getSupportLoaderManager.
You can increase the minimum SDK to level 15 and it will work fine.

ActionBarSherlock gives tons of "Call requires API level 11 (current min is 7)" errors

I downloaded ActionBarSherlock 4.0.3, unzipped it and created a new project from the library folder. The src folder was, according to Eclipse, full of errors, so I followed various instructions online, like adding android-support-v4.jar, setting target API to 15 and compiler compliance level to 1.6. Still, the project has 194 errors, all of which are "Call requires API level 11 (current min is 7)". So when I look at one of the errors, I see this:
#Override
public void invalidateOptionsMenu() {
getSherlock().dispatchInvalidateOptionsMenu();
}
public void supportInvalidateOptionsMenu() {
invalidateOptionsMenu();
//the previous line has this error in Eclipse:
//Call requires API level 11 (current min is 7): android.app.Activity#invalidateOptionsMenu
}
This looks strange to me, because invalidateOptionsMenu() is overridden with the previous function, but Eclipse still complains about the function requiring a newer API level. When I look at the other errors, I find that this is the case with many other errors too.
I have much more experience with Python than Java, so I don't understand anything of what causes this to happen. Help would be appreciated, and if you do help, could you also explain what causes this and what you did to solve it? I wouldn't want to ask someone every time I have a problem, I want to learn too.
Happened to me after running Lint checks. Try right click on sherlock action bar project -> Android tools -> Clear Lint Markers.
Use ActivityCompat, provided in the support jar.
Since you're using min API level 7, and invalidateOptionsMenu() did not exist until API level 11, you can't override it without errors since if the device runs API level 7, the function isn't even available in the base class and a non existing function cannot be overridden.
All this answers are wrong Except dbrown0708 Answer but I will declare it more
You Can use invalidateOptionMenu in lower API by using ActivityCompat As it provided in support library v4
Syntax is invalidateOptionsMenu(Activity activity);
code is
ActivityCompat.invalidateOptionsMenu(this);
In API Since level 11
invalidateOptionsMenu();
Try and use the import android.support.v4.app.Fragment; as your library

Categories

Resources