Android Studio shows methods from newer API

2.2k views Asked by At

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.

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"
    }
  ...
}

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.

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.

Thank you!

2

There are 2 answers

8
rahul.ramanujam On

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"
    }
  ...
}
0
vman On

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.