Android Studio 显示来自较新 API 的方法 [英] Android Studio shows methods from newer API

查看:21
本文介绍了Android Studio 显示来自较新 API 的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我的 Android Studio 项目建议的方法和类型在我的最低 SDK 中不可用.我使用的是 Android Studio 1.1.0 版.

I have noticed that my Android Studio project is suggesting methods and types that are not available for use in my minimum SDK. I am using Android Studio version 1.1.0.

我可以看到在 build.gradle 文件中正确设置了我的最低 SDK 版本:

I can see that my minimum SDK version is set correctly in the build.gradle file:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.pythagoras.sunshine"
        minSdkVersion 18
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
  ...
}

但我注意到 Android Studio 中的自动完成"功能仍然建议 API 18 中不可用的方法.当我构建项目时,我没有收到有关使用这些较新方法的任何错误,并且由于设备我正在测试是否使用目标 API,我的应用程序中没有发现任何问题.

but I have noticed that the "auto-complete" feature in Android Studio still suggests methods that are not available in API 18. When I build the project I do not get any errors about using these newer methods, and since the device I am testing on is using the target API, I do not see any problems in my application.

Android Studio 中是否有设置可以从 API 中删除自动完成选项大于我的最小值?或者,如果使用太新的方法,是否至少有一种方法可以导致构建错误?我已经尝试了将项目与 Gradle 文件同步"按钮,但我在重建时仍然没有收到错误.

Is there a setting in Android Studio that can remove auto-complete options from APIs greater than my minimum? Or is there at least a way to get a build error if a method that is too recent is used? I have tried the "Sync Project with Gradle Files" button, but I still did not receive an error upon rebuilding.

谢谢!

推荐答案

这是我在 Android Studio 1.3.2 中看到的行为,使用您的配置:compileSdkVersion=21minSdkVersion=18targetSdkVersion=21

This the behavior I see in Android Studio 1.3.2, using your configuration: compileSdkVersion=21 minSdkVersion=18 targetSdkVersion=21

您将看到 API 级别 21 以内的所有方法,因为您正在使用 SDK 21 进行编译.

You will see all methods up to API level 21, since you are compiling with SDK 21.

如果你将 targetSdkVersion 设置为 18,那么你也可以将 compileSdkVersion 降低到 18,从 API 19, 20, 21 中删除方法.但是,这意味着你将无法使用更新的方法/类使用新 SDK 的设备.减少 targetSdkVersion 还会告诉 Android 设备您尚未对其进行测试,并启用兼容性行为,这可能是您想要的,也可能不是.

If you set the targetSdkVersion to 18, then you can also lower the compileSdkVersion to 18 as well, removing the methods from API 19, 20, 21. But, this means you will not be able to use newer methods/classes on devices that use the new SDKs. Reducing the targetSdkVersion also tells an Android device that you have not tested for it, and enables compatibility behaviors, which may or may not be what you want.

如果您决定要在支持 API 19/20/21 的设备上使用 API 19/20/21 的高级功能,并且不想在这些较新的设备上激活兼容性行为,则应将 targetSdkVersion 设置为 21.

If you decide you want to use advanced features from API 19/20/21 on the devices that support it, as well as not activating compatibility behaviors on those newer devices, you should keep targetSdkVersion set to 21.

现在,当您使用 API 级别 19+ 的方法时,Android Studio 会向您发出警告.这是因为该方法在 SDK 级别为 18 的设备上会因 NoSuchMethodError 崩溃,因为它不存在.您现在可以检查 设备的 SDK 版本 并且只使用基于它的给定方法(比较使用 SDK 版本).

Now, Android Studio should give you a warning when you use a method from API level 19+. This is because the method will crash with NoSuchMethodError on devices with SDK level 18, since it does not exist. You can now check the device's SDK Version and only use a given method based on it (compare using SDK Version).

旁注:我在使用 Android Studio 时注意到的一件有趣的事情是,使用 API 级别 23 方法时不会显示 lint 警告.例如:如果我使用 compileSdkVersion=23、targetSdkVersion=23、minSdkVersion=19,当我使用 API 级别 21 方法时,Android Studio 会显示错误 finishAndRemoveTask 或 API 级别 22 方法 getReferrer.但是,它没有显示 API 级别 23 方法的错误 getSearchEvent.也许 android lint 没有针对 API 级别 23 更新.

Side note: Something interesting I noticed when using Android Studio is that the lint warning is not shown when using an API level 23 method. For example: if I use compileSdkVersion=23, targetSdkVersion=23, minSdkVersion=19, Android Studio shows errors when I use API level 21 method finishAndRemoveTask or API level 22 method getReferrer. However, it does not show an error for API level 23 method getSearchEvent. Maybe android lint is not updated for API level 23.

这篇关于Android Studio 显示来自较新 API 的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆