如何在增加 Android 目标 SDK 时识别潜在的不兼容性

How to identify potential incompatibilies when increasing Android target SDK

我想尝试将需要目标 SDK 级别 26 的功能(通知渠道)添加到当前目标 SDK 级别 22 的开源 Android 项目。

查看通知渠道的文档,我看到了一些令人不安的事情:显然增加目标级别可能会导致以前有效使用 API 的功能不同——在这种情况下,通知没有指定频道将不再显示。这告诉我,仅仅改变目标级别,可能还会有其他事情发生。

从级别 22 更改为级别 26 时,如何找到项目中我需要检查可能不兼容的每个 API 调用?或者,有没有一种方法可以隔离代码段,以便我没有更改的代码仍然使用级别 22,而只有新代码使用 26?

I want to attempt to add a feature (notification channels) which requires target SDK level 26

如果您将 targetSdkVersion 设置为 26 或更高,则需要通知渠道。如果您的 targetSdkVersion 设置低于 26,则不需要通知渠道,但据我所知,如果您 运行 在 API 级别 26+ 设备上,您仍然可以设置它们。就个人而言,我从来没有尝试过这个;使您的 targetSdkVersion 保持最新在现代 Android 应用程序开发中相当重要。

How can I find every API call in the project that I need to examine for possible incompatibility when changing from level 22 to level 26?

一般来说,你不能。欢迎阅读 Android 的发行说明;在过去的几个版本中,Google 在具体调用由 targetSdkVersion 触发的更改方面做得更好。您还可以阅读有关 Build.VERSION_CODES 值(例如 the JavaDoc for M)的 JavaDocs,因为它们列出了由 targetSdkVersion 触发的更改。 IDE 可能会给您一些警告。除此之外,这是一个测试问题。

Alternatively, is there a way to isolate sections of code so that the code I’m not changing still uses level 22, and only the new code uses 26?

将它们放在完全独立的应用程序中。否则,不。 targetSdkVersion 是 per-app 设置,而不是 per-file 或 per-class 设置。