我对Android开发很新,我想了解一下Lint工具和NewApi检查。做了一些测试后,我有点困惑。
在IntelliJ IDEA中创建新的Android应用程序项目后,我在onCreate方法中添加了一些代码,所以它现在看起来像这样:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Display d = getWindowManager().getDefaultDisplay();
Point size = new Point();
d.getSize(size); // API level 13
isDestroyed(); // API level 17
NativeActivity nativeActivity = new NativeActivity(); // API level 11
nativeActivity.getContentResolver(); // API level 11
Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); // API level 9
boolean canDisableShutterSound = cameraInfo.canDisableShutterSound; // API level 17
}
在清单文件中我有
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="17" />
编译项目并从命令行运行lint后:
lint --check NewApi --classpath c:\_dev\TestNewApi\out c:\_dev\TestNewApi
我得到了以下输出:
Scanning TestNewApi: .......
out\production\TestNewApi\com\example\TestNewApi\MyActivity.class: Error: Call requires API level 9 (current min is 7): android.app.NativeActivity#getContentResolver [NewApi]
out\production\TestNewApi\com\example\TestNewApi\MyActivity.class: Error: Call requires API level 9 (current min is 7): new android.app.NativeActivity [NewApi]
out\production\TestNewApi\com\example\TestNewApi\MyActivity.class: Error: Call requires API level 9 (current min is 7): new android.hardware.Camera.CameraInfo [NewApi]
out\production\TestNewApi\com\example\TestNewApi\MyActivity.class: Error: Field requires API level 9 (current min is 7): android.hardware.Camera.CameraInfo#canDisableShutterSound [NewApi]
4 errors, 0 warnings
所以没有关于getSize和isDestroyed方法的抱怨。将minSdkVersion更改为9后,检查结果为:
Scanning TestNewApi: .......
No issues found.
它找我,就像在两种情况下只检查类一样,如果在minSdkVersion或之后引入类,那么一切都还可以。这是预期的行为吗?或者也许我错过了什么?
答案 0 :(得分:1)
恕我直言,没有。我在Eclipse中看到了相同的行为,恕我直言这是一个错误。通常情况下,Lint会抱怨比这是预期的行为吗?
android:minSdkVersion
或@TargetApi()
更新的方法。我不太清楚为什么不这样做。