工作中Lint工具使用实录及整理

      转载请声明,转自【https://www.cnblogs.com/andy-songwei/p/7090934.html】,谢谢!

      AndroidStudio内置的Lint工具,对app中的代码规范带来了极大的方便。对内存泄漏、代码冗余、代码安全、国际化、代码规范等很多方面都能检测,是一款非常强大的工具。本篇文章是自己使用过程中的过程记录,包括问题检测,问题分析,问题解决等做了一些总结。希望能对读者有一定的帮助。

对该工具的扫盲,不再本篇范畴内,读者可以参考:

(1)Android lint 简介:http://blog.csdn.net/hudashi/article/details/8333349;

(2)如何在android studio 中使用 link工具:http://www.cnblogs.com/cheerego/p/5175764.html

1.Inspection Result for Inspection Profile 'Project Default'

2 该项给了两类提示

(1) Image without contentDescription:

问题概要:[Accessibility] Missing contentDescription attribute on image

解决办法:所有的imageview都添加属性 android:contentDescription="@string/desc"  此处字符串不能为“”

说明:其实这个属性是方便一些生理功能有缺陷的人使用应用程序的。比如我们有一个ImageView里面放置一张颜色复杂的图片,可能一些色弱色盲的人,分不清这张图片中画的是什么东西。如果用户安装了辅助浏览工具比如TalkBack,TalkBack就会大声朗读出用户目前正在浏览的内容。TextView控件TalkBack可以直接读出里面的内容,但是ImageView TalkBack就只能去 读contentDescription的值,告诉用户这个图片到底是什么。

参考:https://stackoverflow.com/questions/8500544/android-lint-contentdescription-warning

(2) Missing labelFor attribute

问题概要:No label views point to this text field with an android:labelFor="@+id/@+id/editableHeight" attribute

解决办法:EditText添加labelFor属性,其值为对应的id

参考:http://blog.csdn.net/kx_nullpointer/article/details/18499509

3 Android>Lint>Correctness>Message

(1)Potential Plurals

问题概要:在字符串的国际化工程中,有一个英文字符串为:<string name="details_title">%1$d of %2$d items:</string>

lint提示warning:Formatting %d followed by words ("items"): This should probably be a plural rather than a string

解决办法:在该项使用plurals的形式定义该字符串

<plurals name="number_of_albumset_video">

                                  <item quantity="one">%1$d video</item>

                                  <item quantity="other">%1$d videos</item>

                           </plurals >

主要是针对不同语言对单复数等形式的数据进行处理

(2)Unused quantity translations

问题概要: 在中文字string.xml中定义了如下的字符串:

  <plurals name="number_of_albumset_photo">

                      <item quantity="one">%1$d 张照片</item>

                      <item quantity="other">%1$d 张照片</item>

                </plurals >

               lint提示For language "zh" (Chinese) the following quantities are not relevant: one

解决办法:中文中没有单复数的区分,所以,将"<item quantity="one">%1$d 张照片</item>" 删除即可

4.Android>Lint>Usability

(1) Button should be borderless

问题概要:在Button控件设置如下属性android:layout_width="0dp"  android:layout_weight="1" 时,提示warning

            解决办法:添加 style="?android:attr/buttonStyle" 属性

参考:http://blog.csdn.net/lyalei/article/details/48025417

(2) Missing inputType or hint

问题概要:使用EditText时,需要添加一些必备的属性,如hint,inputType等,否则会提示warning

解决办法:添加hint属性 android:hint="";

参考:http://blog.csdn.net/aeolus1019/article/details/8112716

(3)Missing support for Google App Indexing

不知道修改了哪里,改warning消失了 -_-

(4)Text size is too small

问题概要:从用户角度出发,官方建议开发者Avoid using sizes smaller than 12sp.

根据UI设计师的设计,该处没有修改

5.  Android>Lint>Usability>Icons

该项中可以检测重复命名的图片,规范要求必须有的密度文件夹,不同文件夹下应该有针对不同密度的图片等

6.Android>Lint>Security

(1) AllowBackup/FullBackupContent Problems

问题概要:该项显示了清单文件中属性allowBackup=true使得backup/restore会导致数据不安全

解决办法:将allowBackup设置为false

参考:http://blog.csdn.net/qq_17338093/article/details/55506543

https://wenku.baidu.com/view/c43af666f342336c1eb91a37f111f18583d00c79.html(如何使用adb backup 和 adb restore 进行备份)

https://developer.android.com/preview/backup/index.html(一般打不开)

(2) Using setJavaScriptEnabled

问题概要:Using setJavaScriptEnabled can introduce XSS vulnerabilities into you application, review carefully.

解决办法:Your code should not invoke setJavaScriptEnabled if you are not sure that your app really requires JavaScript support.

添加该行:@SuppressLint("SetJavaScriptEnabled")

参考:http://blog.csdn.net/huanongjingchao/article/details/18261611

(3) ConentProvider 安全问题(没有及时现场截图)

问题概要:Exported content providers can provide access to potentially sensitive

解决办法:如果确定要给其他应用调用  设置export = "true" 时,加上tools:ignore="ExportedContentProvider",并在manifest文件开头添加tools命名空间;如果只在应用内使用,设置export = "false"

参考: https://stackoverflow.com/questions/13448480/exported-content-providers-can-provide-access-to-potentially-sensitive-data

(4) Service安全问题(没有及时现场截图)

问题概要:Exported service does not require permission

解决办法:由于我使用的Service不需要给其他app使用,设置android:exported="false"

参考:http://www.cnblogs.com/myorange/p/5425835.html

7 Android > Lint > Internationalization

(1)Hardcoded text:该项主要针对xml布局文件中的硬编码问题,[I18N] Hardcoded string "title", should use @string resource;[I18N] Hardcoded string "这是PreviewLayout", should use @string resource

(2)Overlapping items in RelativeLayout:该项主要针对RelativeLayout中部分空间相互重叠了,只要调整大小和位置即可。

(3)TextView Internationalization:该项主要针对代码中直接使用字符串的问题,尽量提取取来作为资源文件存储在xml中

8.Android > Lint > Performance(性能)

(1)FrameLayout can be replaced with <merge> tag

问题描述:xml布局文件中,FrameLayout作为根布局时,会给出该warning(自己通过观察和对比总结的,不敢保证是否准确)

解决办法:使用merge来代替FrameLayout ;

参考:https://stackoverflow.com/questions/21488027/warning-this-framelayout-can-be-replaced-with-a-merge-tag/21490723

http://blog.csdn.net/time_hunter/article/details/8664665

(2)Handler reference leaks

问题概要:Handler的使用不当引起内存泄漏

解决办法:如果按照下面参考网站的建议,handler被定义为static,那么其中引用的很多变量和方法都必须定义为static,会比较麻烦。笔者在按照提其给定的方法 private final MyHandler mHandler = new MyHandler(this); 实例化静态handler的时候甚至连编译都无法通过,this处报错。自己通过尝试,使用了如下的方法进行了处理,最终解决了hanler内存泄漏的问题 

                   private Handler mHandler = new Handler(getMainLooper());

参考:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1106/1922.html (引用了权威开发者的建议,对handler做了一定深度的剖析)

(3) HashMap can be replaced with SparseArray

问题描述:For maps where the keys are of type integer, it's typically more efficient to use the Android SparseArray API. This check identifies scenarios where you might want to consider using SparseArray instead of HashMap for better performance.

解决办法:如果HashMap中key值为Integer或Long,使用SparseArray来取代

参考:https://stackoverflow.com/questions/25560629/sparsearray-vs-hashmap

(4) Inefficient layout weight

当在一个LinearLayout布局中仅包含一个定义weight属性的view控件时,直接指定0dp到其width或height属性更高效,因为在初始化时不再需要测量这个view控件的尺寸,反正到最后它都会霸占所有剩余空间的了。

(5) Layout has too many views

问题概要:检测某个布局文件,出现warning:activity_share_template_detail.xml has more than 80 views, bad for performance

解决办法:减少单个页面的view个数,少于80个

(6) Missing baselineAligned attribute

问题概要:When a LinearLayout is used to distribute the space proportionally between nested layouts, the baseline alignment property should be turned off to make the layout computation faster.(as中的提示)

解决办法:Set android:baselineAligned="false" on this element for better performance(as中的提示)

参考:http://markjoker.iteye.com/blog/2243398

(7) Missing recycle() calls

问题概要:Many resources, such as TypedArrays, VelocityTrackers, etc., should be recycled (with a recycle() call) after use. This lint check looks for missing recycle() calls.

解决办法:使用完后,回收即可

(8) Nested layout weights

问题概要:在布局进行嵌套使用时,父布局与子布局都使用了android:layout_weight,但不是必须使用时,会报这个warning

解决办法:根据实际情况,去掉部分weight属性

(9) Node can be replaced by a TextView with compound drawables

问题概要:A LinearLayout which contains an ImageView and a TextView can be more efficiently handled as a compound drawable (a single TextView, using the drawableTop, drawableLeft,drawableRight and/or drawableBottom attributes to draw one or more images adjacent to the text). If the two widgets are offset from each other with margins, this can be replaced with a drawablePadding attribute.

解决方法:使用一个Textview + compound drawables 可以替代掉 一个LinearLayout 嵌套 textview和imageview的布局,效率和代码量明显极大地优化了

(10) Obsolete layout params

问题概述:The given layout_param is not defined for the given layout, meaning it has no effect. This usually happens when you change the parent layout or move view code around without updating the layout params.This will cause useless attribute processing at runtime, and is misleading for others reading the layout so the parameter should be removed.

解决办法:把无效的属性删除即可。比如LinearLayout子布局中使用layout_toRightOf属性,属于无效属性

(11)Overdraw: Painting regions more than once

问题概述:如果在布局文件xml中的根布局中使用background属性,会和主题中的background产生重复绘制,导致了“overdraw”

If you set a background drawable on a root view, then you should use a custom theme where the theme background is null. Otherwise, the theme background will be painted first, only to have your custom background completely cover it; this is called "overdraw". NOTE: This detector relies on figuring out which layouts are associated with which activities based on scanning the Java code, and it's currently doing that using an inexact pattern matching algorithm. Therefore, it can incorrectly conclude which activity the layout is associated with and then wrongly complain that a background-theme is hidden. If you want your custom background on multiple pages, then you should consider making a custom theme with  your custom background and just using that theme instead of a root element background. Of course it's possible that your custom drawable is translucent and you want it to be mixed with the background. However, you will get better performance if you pre-mix the background with your drawable and use that resulting image or color as a custom theme background instead.

解决办法:在自定义主题中将background设置为null。因为不是每个界面的根布局都使用同一个背景 ,需要自己定义,所以最好在主题中处理(根据自己的项目中的情况来的)

(12)Unused resources

该处列出了app中的没有使用过的各种资源,包括图片,anim文件,颜色,字符串等

9. Android > Lint > Usability > Typography

该项主要检验一些排版错误

(1) 问题概要:在字符串中用三个"."表示省略号"…" ,导致提示warning:Replace "..." with ellipsis character (…, …) ? (at line 551)

解决办法:使用标准的省略号"…"

(2)问题概要:在字符串中使用了非标准的连字符,导致提示warning:Replace "-" with an "en dash" character (–, –)

解决办法:用“–”(有;没有"")代替“–”;(没有找到此处应该用的标注符号)

参考:部分符号对应的编码 http://www.w3school.com.cn/tags/html_ref_symbols.html

更新中...

转载于:https://www.cnblogs.com/andy-songwei/p/7090934.html

【朝花夕拾】Lint使用篇相关推荐

  1. Android 系统(174)---Android代码分析lint检查篇

    Android代码分析lint检查篇 1.什么是lint检查?   Android lint检查是一个静态代码分析工具,它能够对你的Android项目中潜在的bug,可优化的代码,安全性,性能,可用性 ...

  2. 《朝花夕拾》读书笔记|主要内容思维导图

    暑期很多老师都会布置阅读名著的作业,<朝花夕拾>是七年级的必读名著,其中的<从百草园到三味书屋>更是被收录在语文课本里,要求精读.那么今天小编就给大家分享一下[树图思维导图]模 ...

  3. 【朝花夕拾】Android性能篇之(二)Java内存分配

    前言       原文:[朝花夕拾]Android性能篇之(二)Java内存分配        在内存方面,相比于C/C++程序员,咱们java系程序员算是比较幸运的,因为对于内存的分配和回收,都交给 ...

  4. 【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发机制...

    前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五 ...

  5. 【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发逻辑及经常遇到的一些“诡异”现象

    前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五 ...

  6. 【朝花夕拾】Android性能篇之(七)Android跨进程通信篇

    前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/10256379.html],谢谢! 只要是面试高级工程师岗位,Android跨进程通信就是最受面 ...

  7. Android Lint 检查规则的定制(基本篇)

    本人博客原文 英文原文: http://tools.android.com/tips/lint/suppressing-lint-warnings http://tools.android.com/r ...

  8. Android Studio Lint 工具看完这一篇还不够

    前言 以前对下面的问题,我的态度是,不报错就是没问题,报错就用快捷键,根据Android Studio提示修复问题,从来不去问个为什么?现在代码洁癖症越来越严重的我,忍不住想看清什么东西在搞鬼. 认真 ...

  9. 【朝花夕拾】Android性能篇之(四)Apk打包

    前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/9721337.html],谢谢! APK,即Android Package,是将android程 ...

  10. 【朝花夕拾】Android自定义View篇之(一)View绘制流程

    前言 转载请申明转自[https://www.cnblogs.com/andy-songwei/p/10955062.html]谢谢! 自定义View.多线程.网络,被认为是Android开发者必须牢 ...

最新文章

  1. arm-linux下如何安装GDB?pc-linux下如何升级GDB?
  2. copyproperties爆红_BeanUtils.copyProperties复制失败探究
  3. 最后一天,最后一刻。。。。。。情理之中,意料之外。。。。。。
  4. Java枚举根据key获取value
  5. JavaFX UI控件教程(七)之Checkbox
  6. 物联网生态系统的安全与威胁
  7. linux sha1sum命令,讲解Linux中校验文件的MD5码与SHA1码的命令使用
  8. 程序异常退出后,托盘残留图标清理方法(C#)
  9. 语音的基本概念--译自CMU sphinx
  10. spark学习-40-Spark的UnifiedMemoryManager
  11. k8s ready 不调度_【零基础学云计算】k8s部署---master节点组件部署(三)
  12. pinpoint 监控mysql_基于Centos7系统安装部署Pinpoint分布式监控
  13. 天津农学院计算机科学与技术在那个校区,天津农学院有几个校区及校区地址
  14. excel计算机考试操作题,Excel计算机考试操作题全解
  15. vega8显卡和mx250哪个好_哪种显卡等效于vega8 vs.mx150?什么显卡等效于MX110与mx110....
  16. 『IT视界』 [IT风云]MyEclipse 8.5 开发环境配置 插件安装(转载)
  17. bilibili弹幕获取api
  18. linux kettle命令,Linux下用命令来执行kettle文件资源库的文件ktr与kjb的方法
  19. 【地图导航】3D地图软件是如何做路径规划的?为什么准确率这么高
  20. 爬取女朋友用我淘宝已购买的宝贝数据,发现了她特殊的秘密...

热门文章

  1. linux桌面记事本,推荐6款简单实用的手机记事本APP,总有一款适合你的‖APP展览馆...
  2. Nginx 上传图片500错误
  3. linux: It seems that scikit-learn has not been built correctly.
  4. Angular4学习笔记(一):准备和环境搭建
  5. 代码有毒/凉茶/mrcode 关于本博客
  6. 开始写博客之学习编程的重要性
  7. h5直播|微直播weLiveShow|视频h5|video直播
  8. iOS之安装包优化以及瘦身
  9. Okhttp简单介绍
  10. 3d游戏建模学习心得,自学maya,zbrush,substance一个月的感想