如何获取Android API级别不一致的编译时错误而不是NoSuchMethodError运行时错误? [英] How to get a compile time error instead of NoSuchMethodError run-time error for android API level inconsistencies?
问题描述
我有一个Android Xamarin应用,其中 android:minSdkVersion = 15
和 android:targetSdkVersion = 21
。原来,IDE(xamarin Studio,Visual Studio)和编译过程只是忽略了我是否使用的是高于API 15的东西,这在这里不是预期的行为。
例如,那里是来自 API 21
的方法 Path.addArc(float,float,float,float,float,float)
能够在上面的清单设置中使用它,这给了我一个运行时错误 NoSuchMethodError
。
有一个简单的修复方法,可以使用来自 API 1
的重载 Path.addArc(RectF,float,float)
,但是我的问题是更多有关如何获取编译时错误或运行时错误的信息。
[UPDATE]
我基本上想保持对 API 15
(最低Android版本)的向后兼容性,同时具有 API 21
作为目标android版本。当我尝试使用高于 API 15
的任何方法时,我希望获得IDE支持。这意味着我需要警告说我使用的方法在定义的最低android版本中不存在,因此请进行运行时版本检查。
有可能吗?
要获取编译时间警告,您需要将目标框架设置为要支持的最小SDK。当您尝试使用更高级别的API时,这将给您带来错误。打开项目选项对话框。在此对话框中,单击Build> General
您可以在此处
I have an Android Xamarin appliation with android:minSdkVersion="15"
and android:targetSdkVersion="21"
. Turned out that IDE (xamarin studio, visual studio) and compilation process just ignore if I'm using something above API 15 which is not expected behavior here.
For example there is a method Path.addArc(float,float,float,float,float,float)
from API 21
and I was able to use it with the manifest settings above which gave me a run-time error NoSuchMethodError
.
There is an easy fix to use an overload Path.addArc(RectF,float,float)
from API 1
but my question is more about how to get a compile time error instead or a run-time.
[UPDATE]
Basically I want to maintain the backward compatibility for API 15
(minimum android version) while having API 21
as a target android version. And I want to get an IDE support when I'm trying to use any method above API 15
. That means I need a warning saying that I'm using a method which doesn't exist in the minimum android version defined so please do a run-time version check.
Is it possible?
To get compile time warnings you need to set your Target Framework to the min SDK you want to support. This will give you errors when you try to use higher level apis. Open Project Options dialog. In this dialog, click Build > General
You can read about api levels here https://developer.xamarin.com/guides/android/application_fundamentals/understanding_android_api_levels/#Target_Framework
If you just want to get an error while keeping the target framework the same you can modify your IDE preferences to throw a compile time error vs a warning.
这篇关于如何获取Android API级别不一致的编译时错误而不是NoSuchMethodError运行时错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!