4

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!

rose
  • 327
  • 3
  • 10

2 Answers2

4

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.

vman
  • 1,264
  • 11
  • 20
1

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"
    }
  ...
}
rahul.ramanujam
  • 5,608
  • 7
  • 34
  • 56
  • Thank you--- so just to make sure I understand, even though the device I am testing on is running android 5.0.2 (API 21) I should still compile for API 18? – rose Jun 04 '15 at 21:31
  • 2
    What is the point of specifying a minimum SDK version if Android Studio does not use that information? Is this only for when the app is distributed on an app store? – rose Jun 04 '15 at 21:32
  • if you want to run the app on api 21 , keep the compile version as 21 – rahul.ramanujam Jun 04 '15 at 21:33
  • the information is not used during compiling the code , its for installation of the app in the device. the app wont work on devices having api level lower than the minimumsdkversion – rahul.ramanujam Jun 04 '15 at 21:36
  • I have made this change and pressed the "Sync Project with Gradle Files", restarted Android Studio, pressed the green arrow and it still does not show me an error for the methods that I know are not supported in API 18. Also, it still autocompletes with unsupported API methods. Is there another change that I still need to make? – rose Jun 04 '15 at 21:57
  • Just as clarification: My device uses API 21. I would like to run and debug my app on API 21. The devices I am designing for run API 18-21. I would like Android Studio to give me errors/not autocomplete for methods that are too new for API 18. I would still like to test on 21, but receive errors with my minimum SDK in mind. – rose Jun 04 '15 at 22:12
  • If you would like to run you app on 21 too, I would suggest you to make use of the autocomplete feature, this would help you to know to updated methods and avoid using any deprecated methods – rahul.ramanujam Jun 04 '15 at 23:30
  • @rose, you can see my answer for more information. You can still test on an API 21 device while using targetSdkVersion of 18. However, the device will use compatibility measures. The other option is to set targetSdkVersion 21 and use compileSdkVersion 21. This should give you warnings when you use an API level 19/20/21 method, since your minSdkVersion is 18. However, in my experience Android Studio only warns of certain methods. – vman Aug 31 '15 at 01:42