android 硬编码报错,Android Lint:自定义Lint调试与开发

作为移动应用开发者,我们总希望发布的apk文件越小越好,不希望资源文件没有用到的图片资源也被打包进apk,不希望应用中使用了高于minSdk的api,也不希望AndroidManifest文件存在异常,lint就能解决我们的这些问题。Android lint是在ADT 16提供的新工具,它是一个代码扫描工具,能够帮助我们识别代码结构存在的问题,主要包括:

1)布局性能(以前是 layoutopt工具,可以解决无用布局、嵌套太多、布局太多)

2)未使用到资源

3)不一致的数组大小

4)国际化问题(硬编码)

5)图标的问题(重复的图标,错误的大小)

6)可用性问题(如不指定的文本字段的输入型)

7)manifest文件的错误

Android lint可以解决如上的问题,当然还有更多,具体的可以参考Android Lint Checks。Android官方也总结了lint能解决的问题,如下图。

4375befb15f3?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Android代码优化——使用Android lint工具(才发现这是个不错的工具)

lint是命令工具,它已经完美的集成到了Eclipse中,我们可以方便的使用。通过lint,我们可以检测出每个问题的说明和问题的严重性,根据检测报告可以对程序作出改进。下面介绍下在Eclipse怎么使用lint。

lint的使用可以通过两个途径,Eclipse左上角的打钩的按钮或者选择项目->右键->Android Tools,如下图所示:

4375befb15f3?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Android代码优化——使用Android lint工具(才发现这是个不错的工具)

4375befb15f3?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Android代码优化——使用Android lint工具(才发现这是个不错的工具)

图一 图二

lint工具简单实用,自动化分析,分析完成会给我们分析报告:

4375befb15f3?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Android代码优化——使用Android lint工具(才发现这是个不错的工具)

分析包括中会包括错误和警告,会给出具体的描述、类别、位置。上图是一个错误的描述,下图给出警告描述。

4375befb15f3?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

Android代码优化——使用Android lint工具(才发现这是个不错的工具)

Android lint是对android开发者很有帮助的一款工具,对于项目打包发布前优化代码、查找没用到的资源、查找错误等非常有帮助。作为开发者是必须掌握的工具之一

【1】JDK环境变量配置

JDK的安装文件夹为:C:\Program Files (x86)\Java\jdk1.7.0_45。

JDK环境变量配置共同拥有三种方法,即绝对路径法、JAVA_HOME法、完整法。

(1)绝对路径法:即JDK的环境变量的配置所有使用绝对路径。

是最简单的配置方法。

path=C:\Program Files (x86)\Java\jdk1.7.0_45\bin;C:\Program Files (x86)\Java\jdk1.7.0_45\jre\bin

(2)JAVA_HOME法:这是最常见的配置方法。修改比較方便,较为灵活。也能够理解为相对路径。

JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_45

path中=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

(3)完整法:是在JAVA_HOME方法的基础上添加了classpath.在某些使用过程中可能会使用到classpath来寻找路径.推荐使用这样的方式。

JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_45

path=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

classpath=,;%JAVA_HOME%\lib;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar(注意:classpath要以.;开头。

英文状态下的“点 分号”)

1.工具位置

lint 工具在安卓 sdk 的 tools 目录下

2. lint使用

1>在命令行窗口 输入lint,得到lint 帮助说明如下:

Usage: lint [flags]

Flags:

--help This message.

--help Help on the given topic, such as "suppress".

--list List the available issue id's and exit.

--version Output version information and exit.

--exitcode Set the exit code to 1 if errors are found.

--show List available issues along with full explanations.

--show Show full explanations for the given list of issue id's.

Enabled Checks:

--disable Disable the list of categories or specific issue id's. The list should be a comma-separated list of issue id's or categories.

--enable Enable the specific list of issues. This checks all the default issues plus the specifically enabled issues. The list should be a comma-separated list of issue id's or categories.

--check Only check the specific list of issues. This will disable everything and re-enable the given list of issues. The list should be a comma-separated list of

issue id's or categories.

-w, --nowarn Only check for errors (ignore warnings)

-Wall Check all warnings, including those off by default

-Werror Treat all warnings as errors

--config Use the given configuration file to determine whether issues are enabled or disabled. If a project contains a lint.xml file, then this config file will be used as a fallback.

Output Options:

--quiet Don't show progress.

--fullpath Use full paths in the error output.

--showall Do not truncate long messages, lists of alternate locations, etc.

--nolines Do not include the source file lines with errors in the output. By default, the error output includes snippets of source code on the line containing the

error, but this flag turns it off.

--html Create an HTML report instead. If the filename is a directory (or a new filename without an extension), lint will create a separate report for each scanned project.

--url filepath=url Add links to HTML report, replacing local path prefixes with url prefix. The mapping can be a comma-separated list of path prefixes to correspondingURL prefixes, such as C:\temp\Proj1=http://buildserver/sources/temp/Proj1. To turn off linking to files, use --url none

--simplehtml Create a simple HTML report

--xml Create an XML report instead.

Project Options:

--resources

--sources

--classpath

--libraries

2>常用命令讲解

lint --list 列出所有的问题id

lint --show 列出所有的问题,并给出详细解释

lint --check 只检查List列表中的问题id,其中List中的id 使用逗号分开

lint -w 只检查错误,忽略警告

lint --xml filename 创建一个xml的结果报告

lint --html filename 创建一个html 的结果报告

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值