Lint工具使用说明

来源:互联网 发布:淘宝客怎么建站2016 编辑:程序博客网 时间:2024/06/06 03:17

Lint工具使用说明

    Android-Lint是SDK Tools16 (ADT 16)之后才引入的工具,通过代码检查,可发现潜在的问题,并能对Android程序进行优化处理。在AndroidStudio中已经集成了该工具,下面直接演示使用方法。

       1)首先单击菜单栏中的“Analyse”——>选择“Inspect Code”然后Android Studio会弹出“Specify Inspection Scope”对话框,然后选择“Whole project”,单击OK按钮,Android Studio就会进行代码检查。


2)等待一段时间之后,会出现以下结果:


通常检查的内容包括以下几点:

    Correctness

    1) DuplicatedIds

    Layout中id应该唯一

    2) NewApi

    代码中使用的某些API高于Manifest中的Min SDK

    3) InconsistentArrays

    字符串国际化中,同一名字的的String-Array对应的item值不相同

    4) Registered

   Activity/Service/ContentProvider没有通过AndroidManifest注册

    5) Deprecated

    使用已经废弃的API

    6) PxUsage

    避免使用px,使用dp

    Correctness:Messeges

    1) MissingTranslation

    字符串国际化不完全

    2) ExtraTranslation

    国际化的字符串,在默认位置(defaultlocale),没有定义

    Security

    1) SetJavaScriptEnabled

    不确定你的程序中确实需要JavaScript就不要执行SetJavaScriptEnabled。

   2)ExportedContentProvider/ExportedReceiver/ExportedService/ExportedActivity

   ContentProvider/Receiver/Service/Activity的exported为true时,设置一个Permission,让使用者获取了Permission才能使用。

    3) HardcodedDebugMode

    不要在manifest中设置android:debuggable。

    设置它,编译的任何版本都要采用指定的debug模式。不设置,编译Eng版本采用debug模式;编译User版本采用release模式。

    4. Performance

    1) DrawAllocation

    避免在绘制或者解析布局(draw/layout)时分配对象。E.g.,Ondraw()中实例化Paint对象。

    2) ObsoleteLayoutParam

    Layout中无用的参数。

    3) UseCompoundDrawables

    可优化的布局:如包含一个Imageview和一个TextView的线性布局,可被采用CompoundDrawable的TextView代替。

    4) UseSparseArrays

    尽量用Android的SparseArray代替Hashmap

    5) DisableBaselineAlignment

    如果LinearLayout被用于嵌套的layout空间计算,它的android:baselineAligned属性应该设置成false,以加速layout计算。

    6) FloatMath

    使用FloatMath代替Math。

    7) NestedWeights

    避免嵌套weight,那将拖累执行效率

    8) UnusedResources/UnusedIds

    未被使用的资源会是程序变大,并且编译速度降低。 **

    9) Overdraw

    如果为RootView指定一个背景Drawable,会先用Theme的背景绘制一遍,然后才用指定的背景,这就是所谓的“Overdraw”。

    可以设置theme的background为null来避免。

    10) UselessLeaf/UselessParent

    View或view的父亲没有用

    11)Handler Referenceleaks

    handler可能导致的内存泄漏

    Usability:Typography

    1) TypographyDashes

    特殊字符需用编码代替:“–”需要用“–”;“—”需要用“—”

    2) TypographyEllipsis

    特殊字符需用编码代替:“…”需要用“…”

    3) TypographyOther

    问题:“(c)”需要用“©”

    Usability:Icons

    1) IconNoDpi

    Icon在nodpi和指定dpi的目录下都出现。

    2) GifUsage

    Image不要用GIF,最好用PNG,可以用JPG。

    Usability

    1) BackButton

    Android中不要设计有Back的按钮,Android中一般有Back的硬按键。

    2) ButtonCase

    Button的“Ok”/“Cancel”显示大小写一定,不要全大写或全小写。有标准的资源的字符串,不要自己再定义,而要用系统定义的:@android:string/ok和@android:string/cancel

    Accessibility

    1) ContentDescription

    ImageView和ImageButton应该提供contentDescription

    Internationalization

    1) HardcodeText

    硬编码的字符串应该在资源里定义

    2) EnforceUTF8

    所有XML资源文件都应该以UTF-8编码

0 0
原创粉丝点击