Tuesday, June 24, 2014

@TargetApi 和 @SuppressLint 的差別

文章攢寫時間︰2014/06/25 14:10

Android自從加入Lint工具後,
會自動幫開發者們檢查是否在不對的版本使用超岀版本的api。
倘若此事成立,
Android Lint就會跳岀來問你要在此scope上方加上@TargetApi還是@SuppressLint。

剛才在stackoverflow論壞看到一個很有趣的解釋︰
@TargetApi(NN) says "Hey, Android! Yes, I know I am using something newer than what is allowed for in my android:minSdkVersion. That's OK, though, 'cause I am sure that I am using Build (or something) such that the newer code only runs on newer devices. Please pretend that my minSdkVersion is NN for the purposes of this (class|method)".
@TargetApi 意思是說︰「嘿!Android兄,我知道我現在正用一些比我AndroidManifest.xml裡android:minSdkVersion還要新的的API。我想這是沒問題的。因為我很確定我的編譯環境是在新的SDK和新的機器上,我minSdkVersion設那麼低其實只是假的啦(為了在Google Play上被更多人看到我的App)!

@SuppressLint, to address the same error, says "Hey, Android! Yes, I know I am using something newer than what is allowed for in my android:minSdkVersion. Quit complaining.".
@SuppressLint 則是指︰「嘿!Android兄,我知道我現在正用一些比我AndroidManifest.xml裡android:minSdkVersion還要新的的API。不要再跟我碎唸了!

Hence, given a choice of @TargetApi(NN) or @SuppressLint, go with @TargetApi(NN). There, if you start using something newer than NN -- and therefore your existing version-checking logic may be insufficient -- you will get yelled at again.
因此,如果要在@TargetApi或@SuppressLint裡做選擇,盡量選@TargetApi就是了。

1 comment:

Sandy said...

哈哈很有趣的翻譯XD