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

查看:161
本文介绍了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 = 21 minSdkVersion = 18 targetSdkVersion = 21

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

由于您正在使用SDK 21进行编译,因此您将看到直到API级别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中的高级功能,并且不激活那些较新设备上的兼容性行为,则应将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方法时,不会显示棉绒警告.例如:如果我使用compileSdkVersion = 23,targetSdkVersion = 23,minSdkVersion = 19,则当我使用API​​级别21方法 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天全站免登陆