1

This code is some basic code to set up tabs in Android.

final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

If you write this code in a project where targetSDK < 20 or compileSDK < 20 (I don't know what the difference is between versions of compileSDK and targetSDK) then it is fine.

However, if you have compileSDK higher, e.g. 23, then Android Studio will tell you that the code above is deprecated (and moreover is removed from SDK, I guess).

And it will crash my app if i run it on device (btw on Android level 20, though the target of project was set to 23 and compiled version too 23, so it crashes because it is deprecated and removed from SDK level 23).

Questions:

1) What happens if I compile my project with compileSDK version and targetSDK version set to 20 (where there is no problem with being deprecated) and try run it on Android with SDK 21 or 23 where these functions are obviously removed from SDK? Will the app crash or work?

2) What happens if I decide to target the "high" SDK (e.g. 23)? That is, I will leave the "old" code mentioned above and will use some "new" code for tabs, which appeared in SDK 23 or so (which didn't exist in lower SDK); then I will try to run the application on Android with lower SDK, e.g. 20. Will the app crash or work?

edit:

Btw now i launched project with targetSDK and compileSDK 20 on Android with level 22 and it worked.

So it seems the only thing that really matters is the compileSDK version? Doesn't even matter on the SDK version of device but it really matters the compileSDK?

Because as i said about the two tests - project with bad HIGH SDK launched on device with good LOW SDK = crash. Project with good LOW SDK launched on devide with bad HIGH SDK = not crash.

But the logic of course say that it must also matter on android version of device (eg to access some new functions.. or really only matter on compiledSDK or at least from view of backwards-functionallity)?

Need some opinions of experts on this.

luky
  • 2,263
  • 3
  • 22
  • 40
  • Guys if you try put actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); in project with compileSDK eg 23 you will see warning saying like "this can cause crash" and really it crashes ! so it CAN cause crash :) obviously.. from what i saw today, that code was probably removed in 23 or so – luky Sep 03 '15 at 20:36
  • "and really it crashes" -- presumably, that is because you have a bug in your code. [This sample application](https://github.com/commonsguy/cw-omnibus/tree/master/ActionBar/TabFragmentDemoNative) works perfectly fine with a `compileSdkVersion` of 23, despite the fact that it uses the deprecated `setNavigationMode()` and `NAVIGATION_MODE_TABS`. – CommonsWare Sep 03 '15 at 20:44
  • f**k how is it possible? my android studio says this can throw null pointer exception and it throws ! if i change the levels of SDK no exception is thrown and the tab bar really appears ! so i don't know :( – luky Sep 03 '15 at 20:53
  • "this can throw null pointer exception and it throws" -- perhaps you should ask a separate Stack Overflow question where you provide more of your code, along with the complete stack trace. For example, if you are using `appcompat-v7` (e.g., you are inheriting from `AppCompatActivity`), you need to use `getSupportActionBar()`. – CommonsWare Sep 03 '15 at 20:54
  • aha the warning that it can cause null pointer exception is not related with deprecated but with that there is no check if (actionBar != null) so then the crash can be related that some another activity class doesn't return the action bar after call of getActionBar() – luky Sep 03 '15 at 21:02
  • ah, that will be it... i will check it. million thanks to you CommonsWare !!! and to all too. thank you all – luky Sep 03 '15 at 21:03

3 Answers3

3

However, if you have compileSDK higher eg 23, then Android Studio will tell you, the code above is deprecated (and moreover removed from SDK i guess).

No. getActionBar() is not deprecated. setNavigationMode() is deprecated but not removed.

What happens, if i compile my project with compileSDK version and targetSDK version eg 20 (where is not problem with deprecated) and will try run it on Android with SDK eg 21 or 23 where are these functions from SDK obviously removed? Will app crash or work?

The app should work fine. "Deprecated" means "we think that there is a better solution and we would prefer that you use it".

What happens, if i decide to target the "high" SDK that means eg 23, i will leave the "old" code mentioned above and will use some "new" code for tabs, which appeared eg in SDK 23 or so (that means didn't exist in lower SDK) and i will try run the application on Android with lower SDK, eg 20? Will app crash or work?

First, I am not aware of any "'new' code for tabs, which appeared eg in SDK 23". The Design Support library has the only new tab implementation that I can think of, and it works back to API Level 7.

Second, your app should fail to compile, if you are trying to use something that is newer than your minSdkVersion without adequate checks (e.g., using Build.VERSION.SDK_INT and bypassing that code on older devices).

In the end, if you refer to a class, method, field, interface, and so on that does not exist on the older API level, your app will crash if and when that code gets executed. A typical error is VerifyError, though it depends a bit on the nature of what it is that you are trying to use that does not exist.

With respect to tabs, there are countless implementations available to you. Most come in the form of third-party libraries tied to ViewPager. In terms of the Android SDK, there the aforementioned TabLayout from the Design Support library, FragmentTabHost, and PagerTabStrip (though the latter has a bug in the 23.0.0 edition of the support-v4 library, apparently).

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The application will not fail to compile if minSdkVersion points to a SDK that does not have the apis being used, even if they do not have the checks. It will throw NoSuchMethodError. You can add the checks though to allow the application to run on older SDKs while still using latest APIs. – fhsilva Sep 03 '15 at 20:37
  • @fhsilva: "even if they do not have the checks" -- while the *compiler* will not throw an error, Lint will. – CommonsWare Sep 03 '15 at 20:38
  • Ok, assuming he is running lint (what perhaps he should be). I just wanted to point out that the compiler itselt will not complain. – fhsilva Sep 03 '15 at 20:44
  • @fhsilva: Hmmmm... Lint *used* to be run automatically, though it appears that it is not now. I'll have to look into that -- thanks! – CommonsWare Sep 03 '15 at 20:47
  • If you are using gradle, you have several targets. Running targets "assembleDebug" or "assembleRelease" or "assemble" will not run lint (nor unit tests). They will just compile and generate the APK. The "lint" target runs lint (assuming you have enabled it on build.gradle). The "check"target runs lint and others checks as unit tests. The "build" target will run all checks (lint, unit tests, etc) and also compile and generate apks. – fhsilva Sep 03 '15 at 21:05
0

Deprecated API are still available, so you're free to use them - the app won't crash on neither of platforms. However, it's advised that you read the documentation to understand why the API have been deprecated: usually developers provide alternative solutions.

Egor
  • 39,695
  • 10
  • 113
  • 130
0

Here is some explanation on compile/target/minSDK.

1) What happens, if i compile my project with compileSDK version and targetSDK version eg 20 (where is not problem with deprecated) and will try run it on Android with SDK eg 21 or 23 where are these functions from SDK obviously removed? Will app crash or work?

Android Studio is probably only warning you that the API has been deprecated, but not removed. If it had been removed, setting the compile SDK to 23 should break your compilation. Your application should work fine even if it is using a deprecated API, but you should update that when you have the time as it is no longer the recommended one.

2) What happens, if i decide to target the "high" SDK that means eg 23, i will leave the "old" code mentioned above and will use some "new" code for tabs, which appeared eg in SDK 23 or so (that means didn't exist in lower SDK) and i will try run the application on Android with lower SDK, eg 20? Will app crash or work?

When you use an API that has been introduced in SDK 23, you will need to set compileSDK to 23 as well. In case you set your minSDK to 22, you will be able to install it on a device that is using SDK 22, however, your app will crash with a NoSuchMethodError if you try to use that method. You can however check the SDK running on the device (use Build.VERSION.SDK_INT) and not call that method in this case.

Community
  • 1
  • 1
fhsilva
  • 1,217
  • 1
  • 9
  • 17