【朝花夕拾】Lint使用篇

                                                  工作中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 (…, &#8230;) ? (at line 551)

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

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

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

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

   更新中...

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

OpenClaw Docker 安装与部署教程,环境隔离不冲突技术指南 本文介绍了使用Docker容器化部署OpenClaw的完整教程。首先通过流程图详细说明了Docker环境检测流程,包括系统支持性检查、Docker安装状态验证、版本检测和服务配置等步骤。然后提供了Windows、macOS和Linux(Ubuntu/CentOS)系统下的Docker安装指南,涵盖从下载、安装到权限配置的全过程。最后介绍了OpenClaw官方镜像的拉取方法以及自定义镜像构建的Dockerfile示例。该教程适用于开发和生产环境,利用Docker实现环境隔离和快速部署。 阅读详情

相关推荐

滑模控制实战:用Python从零实现SMC算法(附完整代码)

本文通过Python实战,从零详解滑模控制(SMC)算法的核心原理与代码实现。文章聚焦于一阶系统,使用NumPy和Matplotlib逐步构建控制器,并针对SMC固有的抖振问题,详细讲解了边界层法的原理与实现,提供了完整的代码和参数调优指南,帮助读者直观理解并掌握这一鲁棒性极强的非线性控制方法。

weixin_29285605的博客 474

Android 学习笔记(二)

系列文章目录 Anroid 学习笔记(一) 文章目录系列文章目录一、UI开发1、TextViewandroid:gravity="xxx"android:textSize="24sp"android:textColor="#00ff00"2、Button3、EditText二、使用步骤1.引入库2.读入数据总结 一、UI开发 1、TextView <TextView android:id="@+id/text_view" android:layout_width="wrap_content" a

qq_37497304的博客 1004

工控AI从2026到2030:OPC UA、MCP与数字孪生的落地实践

工业自动化与AI的融合正从概念走向工程落地。其核心原理在于通过标准化协议打通设备层、数据层与AI层,实现感知、决策、执行链路的闭环。OPC UA统一了设备信息模型,MCP则标准化了AI与工具间的调用接口,二者结合将大幅降低系统集成复杂度。技术价值在于提升数据质量与连接可靠性,使AI推理能安全地辅助甚至优化控制。典型应用包括产线数据采集、数字孪生同步、PLC代码生成与边缘智能。本文深入探讨了这些关键技术在工控场景中的实操要点与常见陷阱。

weixin_32025789的博客 192

Android开发系列——Hardcoded Text

  在Android开发过程中,在UI布局过程中,设置Button、TextView中默认的文字出现警告: Warning:Hardcoded string “XXXXX”, should use @string resource 。 可以通过字面意思大概知道问题的根源在于直接设置text(硬编码问题)。 接下来了解一下什么是硬编码: 硬编码:   是将数据直接嵌入到程序或其他可执行对象的源代...

Future_One的专栏 8687

excel 利用SUBTOTAL函数计算筛选后数列总和、均值、最大、最小值

表里有三个人,咱们需要筛选出三个人,并分别计算他们的总和值。 先看一下数据范围(A1:B27) 通过筛选找张三。 按照上图所示输入函数,选择9,求和。 然后按照函数输入即可。 就能计算出结果。 筛选项选择王五,就会自动计算王五的总和。 筛选到李四就会自动计算李四的总和。 如果把9改成1,就是自动计算筛选出来的均值结果,如下图所示。 如果改成4就是取筛选项的最大值,选择5就是取最小值。 ...

yuanYW7556的博客 3万+

逆向工程师视角:拆解DataDome滑块验证码的3层防护机制与绕过思路

本文从逆向工程师视角,深入剖析DataDome滑块验证码的三层核心防护机制。第一层通过动态加密参数实现会话绑定与抗重放;第二层采集TLS指纹、鼠标轨迹动力学等多维行为数据进行一致性验证;第三层则是基于机器学习的后端异步风险决策引擎。文章不仅拆解了其技术原理,更分享了模拟、钩取等超越单纯工具调用的底层分析与绕过思路。

f6g7h8i9j的博客 753

Using setJavaScriptEnabled can Introduce XSS Vulnerabilities into&

Android Warning : Using setJavaScriptEnabled can introduce XSS vulnerabilities into you application, review carefully. 原因:如果你的应用没有在WebView内直接使用JavaScript,不要调用setJavaScriptEnabled()我们见过这个方法在简单的代码中执行,也

未来很长,但我会努力的走下去。 4308

【Matlab 传感器布局优化】基于群智能算法的wsn覆盖优化研究

本文主要是对同构和异构传感器在指定空间内的布局进行优化,以最大覆盖率为目标,使用了DBO/SCA/WOA/SSA/NOA等启发式算法,在100x100的空间内,对30,100,200个传感器进行优化,得到最优的布局位置和对应覆盖率。

u013367499的博客 1831

【分享笔记】使用lint来改善你的代码

一.lint简介什么是静态代码分析静态代码分析是指无需运行被测代码,仅通过分析或检查源程序的语法、结构、过程、接口等来检查程序的正确性,找出代码隐藏的错误和缺陷,如参数不匹配,有歧义的嵌套语句,错误的递归,非法计算,可能出现的空指针引用等等。在软件开发过程中,静态代码分析往往先于动态测试之前进行,同时也可以作为制定动态测试用例的参考。统计证明,在整个软件开发生命周期中,30% 至 70% 的代码逻辑

有心好书 2404

Oracle EBS 寄销/VMI(2)-->采购篇

上篇有一部分设置需要建BPA后,将BPA编号与批准的供应商相关联.     本篇主要是讲的内容:一揽子采购协议-->准标采购订单-->收接入库-->子库转移-->创建冲减通知-->'按接收开票' 自动开票程序 1. PO-->采购订单à采购订单 创建一揽子采购协议,确定物料

陈显平的专栏-Oracle EBS 1万+

[QMT量化交易小白入门]-七十一、十年回测ETF动量策略,超额年化收益率为10.6%

本专栏主要是介绍QMT的基础用法,常见函数,写策略的方法,也会分享一些量化交易的思路,大概会写100篇左右。QMT的相关资料较少,在使用过程中不断的摸索,遇到了一些问题,记录下来和大家一起沟通,共同进步。

木头大左的博客 1880

【转载】Jenkins代码审查功能的实现方案

原文地址:Jenkins代码审查功能的实现方案 在Jenkins上实现了代码审查功能, 本篇博客记录一下具体的实施方案,主要包括Jenkins、Gerrit Trigger、Git Hook等。 一、目的 为了减少不必要的编译错误,同时提高代码书写质量,可以在Jenkins上实现了代码审查的功能。 Jenkins具有该功能后,将自动对Gerrit上提交的代码进行编

分享技术与经验的博客 3008

4.6 鞅-一致可积,L1收敛(Durrett)答案

概率-理论与实例第五章 答案:6 鞅-一致可积,L1收敛

weixin_40614311的博客 1069

【朝花夕拾】Lint篇

工作中Lint工具使用实录及整理 AndroidStudio内置的Lint工具,对app中的代码规范带来了极大的方便。对内存泄漏、代码冗余、代码安全、国际化、代码规范等很多方面都能检测,是一款非常强大的工具。本篇文章是自己使用过程中的过程记录,...

weixin_34029949的博客 274

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

run android lint value string 出错,Android Lint常见问题分析(for studio)

Android1. android resource Validation验证Android xml文件内的资源引用Unresolved class 'MainActivity'Cannot resolve symbol '@android:attr/textAppearanceMedium'Cannot resolve symbol '@android:attr/textAppearanceMe...

weixin_39759107的博客 1134

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

图片按钮 imagebutton

xml 文件 <?xml version="1.0" encoding="utf-8"?> <!-- 普通图片按钮 --> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/mia5" /> <!-- 按下时显示不同图片的按钮 --> <ImageButton

weixin_44701192的博客 363

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Android开发笔记02-Button01

Android开发笔记01-Button01

qq_44885775的博客 847

Android lint检查

一、什么是Android lint 检查 Android lint是一个静态代码分析工具,能够对项目中潜在的bug,可优化的代码,安全性,性能,可行性,可访问性,国际化等检查。 二、lint工作流程 针对我们的项目,通过lint.xml配置lint分析的选项,执行检查后,会针对要分析的issue生成报告。 lintTool 把源码和lint.xml配置文件 打包成lint output...

s297165331的专栏 6151

"xxx.xml has more than 80 views, bad for performance"该怎么处理

如图,请问大神为什么会提示这种警告,怎么才能消除这个警告啊 本来是一个水平方向的LinearLayout,里面又有3个水平方向的子LinearLayout,给出警告的是第二个子LinearLayout,如下图:

yanci5235的博客 1481

Content Provider使用良心总结

1.简介Content Provider主要用于在不同的应用程序之间(因而是完成IPC的一种)实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访数据的安全性。目前,使用ContentProvider是 Android 实现跨程序共享数据的标准方式,因为它提供了统一的数据访问方式。2.使用一种是使用现有的ContentProvider来读取和操作相应程序...

zhwadezh的博客 859

Android安全[app风险]

Activity组件暴露 风险描述 Activity组件的属性exported被设置为true或是未设置exported值但IntentFilter不为空,activity被认为是导出的,可通过设置相应的Intent唤起activity。 危害描述 黑客可能构造恶意数据针对导出activity组件实施越权攻击,注入/越权,拒绝服务,劫持/隐式意图等。 安全建议 如果组件不需要与其他ap...

0x00的博客 2911

Android开发中给button按钮添加事件处理

1事件处理作用 应用程序是事件驱动的,所以开发应用程序时,和用户的交互主要是监听控件的事件,并处理用户事件,在用户事件的程序程序中完成对应的业务或功能 2步骤 与一般的JAVA程序中事件处理的实现步骤相同 (1)找到控件 (2)设置控件的事件监听器 (3)添加事件处理程序 3实现方法: 与一般的JAVA程序中事件处理的实现方法相同 (1)使用匿名内部类(即在设置事件监听器的同时设置一个匿名类) (...

LJN951118的博客 1万+

关于Android的Lint检查的一些结果分析

前言 在安卓开发中,Lint可以用来帮助我们检查代码中存在的一些问题,使用方法也很简单,Analyze---Inspect Code Lint检查完成后,可以在Inspection Results中看到检查结果 下面总结一些常见的lint问题  1.Image without contentDescription,没有给ImageView指定

Small_Lee的博客 2201

Android Lint扫描优化(一)

一.使用Lint 工具栏 -> Analyze -> Inspect Code 弹出窗的Custom scope可以选择对哪一指定部分进行扫描,whole project是整个项目,不太建议每次都扫描整个项目,因为太耗时了。 二.观察Lint的结果 初次扫描之后,会弹...

weixin_34056162的博客 186

Android Studio lint工具所提示的需要注意的内容简要记录

Android lint部分:Duplicated icons under different names 相同的icon 用了不同的名字 FrameLayout can be replaced with tag FrameLayout 可以被替换成 merge标签 Handler reference leaks handler的内存溢出问题 替换成静态 + 弱引用 Hardcode

u012760183的专栏 3075

自定义AlertDialog对话框,包括解决对话框中edittext不能输入的问题

1、自定义代码实现: private void addAndEditBookmark(final String typeStr, String title) { // 1. 布局文件转换为View对象 LayoutInflater inflater = LayoutInflater.from(context); RelativeLayout layout = (RelativeLayout

leihuanhuan123的专栏 1703
上一篇: Android中颜色透明度对应16进制值
下一篇: Android创建或删除了文件,在电脑端查看的时候,却没有对应的变化,处理办法...
aw1070
博客等级 码龄11年 8粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值