注解改进代码检查
Last updated
Was this helpful?
Last updated
Was this helpful?
使用注解改进代码检查
Using code inspections tools such ascan help you find problemsy and improve your code, but inspection tools can only infer so much. Android resource IDs, for example, use anint
to identify strings, graphics, colors, and other resource types, so inspection tools cannot tell when you have specified a string resource where you should have specified a color. This situation means that your app may render incorrectly or fail to run at all, even if you use code inspection.使用比如Lint的代码检测工具,能帮助你找到问题和改善你的代码,但检查工具只能推断而已。Android资源IDs, 例如,使用int去标识的字符串、制图、颜色、和其它资源类,所以检查工具不能告诉当你有特定的字符串资源在你使用的特殊颜色。这个情况的含义是你的app可能渲染错误或运行完全错误,甚至当你使用了代码检查。
Annotations allow you to provide hints to code inspections tools like Lint, to help detect these more subtle code problems. They are added as metadata tags that you attach to variables, parameters, and return values to inspect method return values, passed parameters, local variables, and fields. When used with code inspections tools, annotations can help you detect problems, such as null pointer exceptions and resource type conflicts.注解允许你提供提示像Lint在代码审查工具,去帮助检查这些敏感代码问题。它们的添加像附加在变量的metadata标签、参数和检查方法的返回值,传参、本地变量和字段。当使用代码检查工具,注解能帮助你去检查问题,像空指针异常和资源类型冲突。
Android supports a variety of annotations through the. You can access the library through thepackage.android支持各种注解能连 ASL。你能连接到库通过 android.support.annotation包。
To enable annotations in your project, add the support-annotations dependency to your library or app. Any annotations you add then get checked when you run a code inspection or lint task.
Add the support annotations library dependency The Support Annotations library is part of the Android Support Repository. To add annotations to your project, you must download the support repository and add the support-annotations dependency to your build.gradle file.
Open the SDK Manager by clicking SDK Manager in the toolbar or selecting Tools > Android > SDK Manager. Click the SDK Tools tab. Expand Support Repository and check the Android Support Repository checkbox. Click OK. Continue through the wizard to install the packages. Add the support-annotations dependency to your project by putting the following line in the dependencies block of your build.gradle file: dependencies { compile 'com.android.support:support-annotations:24.2.0' }