在開發上,若我們使用到了某些 API 是有 Android Version 限制的話
就會出現此錯誤 Call requires API level 23 (current min is 19) 詳見文底
要解這個問題很簡單,利用 Build.VERSION 的判斷即可解決
if version 到某一版本之上才執行 API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cryptoObject = new FingerprintManager.CryptoObject(cipher);
}
但若是有很多地方需要加此判斷的話
不只我們麻煩,程式看起來也會很亂
所以可以用 Annotation 來解決
只要加在 class, method 上方,即可作用於其內容,如下
@TargetApi(Build.VERSION_CODES.M)
private void init() { 。。。 }
@RequiresApi(api = Build.VERSION_CODES.M)
public class FingerprintHelper { 。。。 }
這時候我們發現了解決的 Annotation 似乎不止一個
沒錯,有以下的解法 TargetApi、RequiresApi、suppressLint 擇一即可
若想知道其差異性
可點我查看 「Android TargetApi、RequiresApi、suppressLint 的用法與差異』Willy's Fish教學筆記 」
錯誤訊息:
Call requires API level 23 (current min is 19): new android.hardware.fingerprint.FingerprintManager.CryptoObject less... (⌘F1)
This check scans through all the Android API calls in the application and warns about any calls that are not available on all versions targeted by this application (according to its minimum SDK attribute in the manifest). If you really want to use this API and don't need to support older devices just set the minSdkVersion in your build.gradle or AndroidManifest.xml files. If your code is deliberately accessing newer APIs, and you have ensured (e.g. with conditional execution) that this code will only ever be called on a supported platform, then you can annotate your class or method with the @TargetApi annotation specifying the local minimum SDK to apply, such as @TargetApi(11), such that this check considers 11 rather than your manifest file's minimum SDK as the required API level. If you are deliberately setting android: attributes in style definitions, make sure you place this in a values-vNN folder in order to avoid running into runtime conflicts on certain devices where manufacturers have added custom attributes whose ids conflict with the new ones on later platforms. Similarly, you can use tools:targetApi="11" in an XML file to indicate that the element will only be inflated in an adequate context. Issue id: NewApi
留言列表