简体   繁体   English

使用minSdkVersion管理Android项目中的“已弃用”警告

[英]managing 'deprecated' warnings in Android project with minSdkVersion

I hate warnings. 我讨厌警告。 Our Android project has 151 now, and I am sure that somewhere down the list there is one that actually warns us against a potential trouble. 我们的Android项目现在有151个,而且我确信在列表的某个地方有一个实际上警告我们免受潜在的麻烦。

One type of these warnings is about deprecated fields and methods. 这些警告中的一种是关于不推荐使用的字段和方法。 This could be useful, except that the Manifest contains <uses-sdk android:minSdkVersion="10" /> , and these warnings only take into account the target SDK which is android-17 . 这可能很有用,除了Manifest包含<uses-sdk android:minSdkVersion="10" /> ,这些警告只考虑target SDK即android-17

It's easy to mute these warnings - add @SuppressWarnings("deprecation") annotation before the offending line, or the whole method. 这些警告很容易静音 - 在违规行或整个方法之前添加@SuppressWarnings("deprecation")注释。 But this misses the whole point of it - if/when we decide to change minSdkVersion="11" , the APIs that are deprecated at level 10 will still not show up, and somebody will have to go through all annotations in all our project, to find which code must be rewritten. 但这忽略了它的全部要点 - 如果/当我们决定改变minSdkVersion="11" ,在10级弃用的API仍然不会出现,并且有人将不得不在我们所有项目中完成所有注释,找到必须重写的代码。

Is there some solution that could manage these warnings based on my minSdkVersion ? 是否有一些解决方案可以根据我的minSdkVersion管理这些警告?


It seems that Mark who posted an interesting answer below, and even filed a feature request inspired by my question does not agree with me about the importance of minSdkVersion . 似乎Mark在下面发布了一个有趣的答案,甚至提出了一个受我的问题启发的功能请求 ,但我不同意minSdkVersion的重要性。 He would prefer to see deprecation warnings (most likely from Lint, similar to the @TargetApi(NN) annotations) based on the target API level. 他希望看到基于目标API级别的弃用警告(很可能来自Lint,类似于@TargetApi(NN)注释)。 But I cannot agree with this approach. 但我不同意这种方法。

Consider a simple application that opens the Camera. 考虑一个打开相机的简单应用程序。 It might want to check the preview frame rate . 它可能想要检查预览帧速率 But this method was deprecated at API 9, and now we must check the preview FPS range . 但是这种方法在API 9中已弃用,现在我们必须检查预览FPS范围 If we use platforms/android-8/android.jar , the Java compiler will not show deprecation warnings. 如果我们使用platforms/android-8/android.jar ,Java编译器将不会显示弃用警告。

But it will not allow us to find the preferred video resolution , even in case the application is running on a device that supports such query. 但即使应用程序在支持此类查询的设备上运行,它也不允许我们找到首选的视频分辨率 We will probably add @TargetApi(11) annotation there, to make sure the application is built with platforms/android-11/android.jar or higher. 我们可能会在@TargetApi(11)添加@TargetApi(11)注释,以确保应用程序是使用platforms/android-11/android.jar或更高版本构建的。

Now that we have target Honeycomb and higher, the deprecation warning will be shown for getPreviewFrameRate() , and this is exactly what bothers me. 现在我们已经定位了Honeycomb及更高版本,将为 getPreviewFrameRate() 显示弃用警告,这正是困扰我的。 For a while, my imaginary application must support some Froyo devices, therefore I have no choice but set minSdkVersion=8 and use the deprecated method. 有一段时间,我想象中的应用程序必须支持一些Froyo设备,因此我别无选择, minSdkVersion=8设置minSdkVersion=8并使用不推荐使用的方法。 Naturally, I will use any advanced APIs in conditional blocks, and have the @TargetApi(NN) in place. 当然,我将在条件块中使用任何高级API,并使用@TargetApi(NN) Luckily, for Donut and higher, the class loader does not crash when a call to a non-existent method or member is detected, and wrapping the questionable calls in mere if() is enough. 幸运的是,对于Donut和更高版本,当检测到对不存在的方法或成员的调用时,类加载器不会崩溃,并且仅仅在if()中包装可疑调用就足够了。

So what do I do? 那我该怎么办? Add @SuppressWarnings("deprecation") around the calls to getPreviewFrameRate() to mute the warnings? 在对 getPreviewFrameRate() 的调用周围添加@SuppressWarnings("deprecation") 来静音警告? Add @SuppressDeprecation(8) instead? 改为添加@SuppressDeprecation(8)

But at some point, I will decide that the backward compatibility with Froyo is not important anymore. 但在某些时候,我将决定与Froyo的向后兼容性不再重要。 I set <uses-sdk android:minSdkVersion="9" /> in my Manifest, but the deprecation warnings for getPreviewFrameRate() are still suppressed... Well, the new approach is definitely better than the existing annotation, because it's easier to grep -R "@SuppressDeprecation(8)" the whole project after the change is made in Manifest. 我在我的Manifest中设置了<uses-sdk android:minSdkVersion="9" /> ,但 getPreviewFrameRate() 的弃用警告仍被抑制......好吧,新方法肯定比现有的注释更好,因为它更容易grep -R "@SuppressDeprecation(8)"在Manifest中进行更改后的整个项目。

But I would prefer a bit more tight integration here: let Lint parse the Manifest file for me. 但我更喜欢这里更紧密的集成:让Lint为我解析Manifest文件。

Does this make better sense now? 这会更好吗?

Is there some solution that could manage these warnings based on my minSdkVersion? 是否有一些解决方案可以根据我的minSdkVersion管理这些警告?

No, because deprecation has nothing to do with android:minSdkVersion . 不,因为弃用与android:minSdkVersion无关。

If you are really worried about this, isolate the deprecated stuff into their own methods, to minimize the odds that your annotation will obscure future deprecations of which you are unaware. 如果您真的对此感到担心,请将已弃用的内容隔离到自己的方法中,以最大程度地降低注释将掩盖您不了解的未来弃用的几率。

What's not completely out of the question would be a @SuppressDeprecation(NN) annotation, that would suppress deprecations for a given build target. 什么不是完全不可能的是@SuppressDeprecation(NN)注释,它将抑制给定构建目标的弃用。 This would be akin to how @TargetApi(NN) suppresses Lint complaints for a given android:minSdkVersion . 这类似于@TargetApi(NN)如何抑制给定android:minSdkVersion Lint投诉。 I have filed a feature request for this . 我已经为此提交了功能请求

实际上有一项功能请求: https//code.google.com/p/android/issues/detail? id = 411318于2012年12月12日提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM