Android Studio shows methods from newer API

一个人想着一个人 提交于 2019-12-04 10:57:46

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

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

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.

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.

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

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 compiles your code against compilesdkversion

android {
    compileSdkVersion 18
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.pythagoras.sunshine"
        minSdkVersion 18
        targetSdkVersion 18
        versionCode 1
        versionName "1.0"
    }
  ...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!