gpt4 book ai didi

Android 目标 sdk 与弃用的方法

转载 作者:行者123 更新时间:2023-11-29 20:27:10 26 4
gpt4 key购买 nike

此代码是在 Android 中设置标签页的一些基本代码。

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

如果你在 targetSDK < 20 或 compileSDK < 20 的项目中编写这段代码(我不知道 compileSDK 和 targetSDK 的版本之间有什么区别)那么就可以了。

但是,如果你有更高的 compileSDK,例如23,然后 Android Studio 会告诉您上面的代码已弃用(而且我猜是从 SDK 中删除了)。

如果我在设备上运行它会崩溃我的应用程序(顺便说一句,在 Android 级别 20 上,尽管项目的目标设置为 23 并且编译版本也为 23,所以它崩溃是因为它已被弃用并从 SDK 级别 23 中删除).

问题:

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?

编辑:

顺便说一句,现在我在 22 级的 Android 上启动了带有 targetSDK 和 compileSDK 20 的项目并且它工作正常。

所以似乎唯一真正重要的是 compileSDK 版本?设备的 SDK 版本甚至都不重要,但 compileSDK 真的很重要吗?

因为正如我所说的两个测试 - 在具有良好 LOW SDK 的设备上启动具有错误 HIGH SDK 的项目 = 崩溃。具有良好 LOW SDK 的项目在具有不良 HIGH SDK 的设备上启动 = 不会崩溃。

但是逻辑当然说它在设备的 android 版本上也一定很重要(例如访问一些新功能..或者真的只在编译的 SDK 上或至少从向后功能的角度来看)?

需要专家的一些意见。

最佳答案

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).

没有。 getActionBar() is not deprecated . setNavigationMode()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?

该应用程序应该可以正常运行。 “已弃用”表示“我们认为有更好的解决方案,我们希望您使用它”。

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?

首先,我不知道有任何“选项卡的‘新’代码,例如出现在 SDK 23 中”。 Design Support 库具有我能想到的唯一新选项卡实现,并且可以追溯到 API 级别 7。

其次,如果您尝试使用比您的 minSdkVersion 更新的东西而没有进行足够的检查(例如,使用 Build.VERSION.SDK_INT 并在旧设备上绕过该代码)。

最后,如果您引用旧 API 级别上不存在的类、方法、字段、接口(interface)等,那么当该代码被执行时,您的应用程序将会崩溃。一个典型的错误是 VerifyError,尽管它在一定程度上取决于您尝试使用不存在的内容的性质。

关于选项卡,有无数种实现可供您使用。大多数以 third-party libraries tied to ViewPager 的形式出现.在 Android SDK 方面,有来自设计支持库的上述 TabLayoutFragmentTabHostPagerTabStrip(尽管 the latter has a bug in the 23.0.0 edition of the support-v4 library, apparently)。

关于Android 目标 sdk 与弃用的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32384485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com