16

I am writing a Xamarin Android app using Visual Studio 2015 (Windows). I want to target the latest Android API, while maintaining backwards compatibility to API 16 (4.1 Jelly Bean).

I know how to ensure there is no run-time error thrown via an API level check:

if(Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
    //Call API supported by Lollipop and above, but not by lower API's
}
else
{
   //Alternative code for graceful backwards compatibility
}

Unfortunately, if I don't have this check, Visual Studio still happily compiles and runs my code - and if the API is not supported, I get a run-time Java.Lang.NoSuchMethodError exception. I can manually reset the project to a lower level API to catch the errors compile-time, but this is a bit cumbersome - it would be great to catch these errors (even as warnings) at compile-time, without manually resetting to each lower API I'd like to support.

How can I detect an incompatible API call at compile time?

NOTE: I did find a similar question on StackOverflow, but it specifically addresses Xamarin Studio for Mac - which suggests this should be possible to do in Visual Studio 2015 for Windows (2017 solution ok too).

Community
  • 1
  • 1
CoolBots
  • 4,770
  • 2
  • 16
  • 30
  • `Unfortunately, if I don't have this check, Visual Studio still happily compiles and runs my code` - but if it did not compile your code, it would not compile it even if you had the check, which is even worse. – GSerg Mar 09 '17 at 15:12
  • Possible duplicate of [Android Version settings](http://stackoverflow.com/q/22843803/11683) – GSerg Mar 09 '17 at 15:12
  • @GSerg, the SO question you referenced does not address my question - it contains [quite a bit] of general information about API levels - I am familiar with this information, and encountered this question before posting mine. I am specifically looking for a way to detect an improper API level call at compile-time. I am not entirely sure what you mean by your first comment - perhaps I wasn't clear in my question - I am certainly happy that VS compiles my code in general - I simply prefer compile-time errors to run-time errors. It's possible on Xamarin for Mac - there should be a way on Windows. – CoolBots Mar 09 '17 at 15:26
  • 1
    There are Android Lint checks that you can use: http://tools.android.com/tips/lint-checks You can enable this via `true` in your .csproj but it might be quite limited. See https://bugzilla.xamarin.com/show_bug.cgi?id=23968 – Jon Douglas Mar 09 '17 at 16:10
  • @JonDouglas, unfortunately Android Lint does not catch this type of error (I just tried in my project). Thank you for the suggestion though, it's likely going to be helpful overall. :) – CoolBots Mar 09 '17 at 16:20

0 Answers0