Lint代码规范记录

作者: Only凹凸曼 | 来源:发表于2017-06-28 11:04 被阅读760次

    背景

    最近在用lint做一个代码规范的检测,虽然意义不是特别大,但是也有一定的作用,这里把自己遇到的一些给记录一下,后面会不定时补充:

    DisableBaselineAlignment

    Set `android:baselineAligned="false"` on this element for better performance
    

    当LinerLayout的子View都是ViewGroup(自定义控件除外)时,Lint认为它的子View已经不需要基准线对齐了,为了不让LinerLayout去做无用的计算对齐的操作,提出了如上警告,修改掉之后就可以提高性能。

    DrawAllocation

    Avoid object allocations during draw/layout operations (preallocate and reuse instead)
    

    Android自带的lint测试,发现上面这个问题。。。。百度一番发现。。。在ondraw/onlayout中尽量不要用new 对象的操作。。。因为ondraw/onlayout会经常被调用;这样比较耗内存。。。。

    DuplicateIncludedIds

    It's okay for two independent layouts to use the same ids. However, if layouts are combined with include tags, then the id's need to be unique within any chain of included layouts, or `Activity#findViewById()` can return an unexpected view.
    

    如果是两个独立的布局用同一个ID,是可以的。但是,如果是用include标签把它们联合起来的话,那么id就必须是唯一的。

    ExportedContentProvider

    Exported content providers can provide access to potentially sensitive data
    

    其实就是说这些内容可以被其他应用访问到,如果这是你想的就标记忽略,否则就去掉:

    android:exported="true"
    

    ExportedService

    Exported service does not require permission
    

    其实就是说这些内容可以被其他应用访问到,如果这是你想的就标记忽略或者设置访问权限,否则就这只:

    android:exported="false"
    

    ExportedReceiver

    Exported receiver does not require permission
    

    其实就是说这些内容可以被其他应用访问到,如果这是你想的就标记忽略或者设置访问权限,否则就这只:

    android:exported="false"
    

    GetInstance

    `Cipher.getInstance` should not be called without setting the encryption mode and padding
    

    密码不应该被称为密码模式,也不需要设置密码模式,因为android的默认模式是欧洲,这是不安全的。
    Google了一下貌似说是Android的Bug大家参考一下

    Deprecated

    `android:singleLine` is deprecated: Use `maxLines="1"` instead
    

    这个东西是说android:singleLine`是一个过时的方法,让你用maxLines="1"来替代这个地方会有坑,有些老手机上是不好用的maxLines="1",所以我们只能选择忽略

    ExtraTranslation

    The resource string "`network_exception`" has been marked as `translatable="false"`
    

    大概的意思就是你的“network_exception”写了translatable="false"但是你又在其他地方做了多语言,出现这种问题大概的原因就是我们在开发的时候多个人或者多个Module写了相同的资源,导致了冲突,建议统一维护和修改就可以了。

    InvalidPackage

    Invalid package reference in library; not included in Android: `java.time`. Referenced from `com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec`.
    

    拿阿里的某一个Jar包为例,大概的意思就是你使用了Android中不支持的API

    LongLogTag

    Log tags are only allowed to be at most 23 tag characters long.
    

    后面的日志长度不能超过23个字符,你可以修改自己的日志,也可以直接关闭这一个lint检测。

    NewApi

    这个的意思就是你使用的方法有API限制,需要做API判断,如果你确认自己已经做了判断,那就直接标记忽略就行了。

    RestrictedApi

    FragmentManager.getFragments can only be called from within the same library group (groupId=com.android.support)
    

    拿FragmentManager.getFragments举例,大概的意思是说这个方法在support包里面如果support包升级了可能会变化,这种我们选择不改,当我们升级support包的时候监控一下就可以了。

    WrongConstant

    传递参数的类型不对。

    ResourceAsColor

    需要传递一个color值,不能直接用color的ID。

    ButtonStyle

    Buttons in button bars should be borderless; use `style="?android:attr/buttonBarButtonStyle"` (and `?android:attr/buttonBarStyle` on the parent)
    

    两个 Buttons 放在一个布局里会被判断为按钮栏,需要添加样式取消它的边框。个人感觉如果我们自己设置了背景什么的就不需要处理了,否则样式可能会变

    HardcodedText

    在代码中直接写了文字,我们应该写到string文件里面,这个我们一定要养成良好的习惯。

    InflateParams

    Avoid passing `null` as the view root (needed to resolve layout parameters on the inflated layout's root element)
    

    具体的说明可以参考这篇文章LayoutInflater使用的正确姿势

    相关文章

      网友评论

        本文标题:Lint代码规范记录

        本文链接:https://www.haomeiwen.com/subject/sekjcxtx.html