2.1、Android Studio通过Lint提升你的代码

简介: 为了测试你的Android应用符合功能需求。最重要的是确保你的代码没有结构性问题。结构差的代码影响你的Android应用的可靠性,让你的代码难以维护。

为了测试你的Android应用符合功能需求。最重要的是确保你的代码没有结构性问题。结构差的代码影响你的Android应用的可靠性,让你的代码难以维护。比如,如果你的XML资源文件包含未使用的明明空间,这会花费空间和不必要的进程。其他机构行问题,比如是哟个过时的API调用,如果设备API版本不兼容,可能导致运行失败。

概览

Android Studio提供了一个称为Lint的代码扫描工具,可以非常容易的帮你辨别和纠正代码的结构性质量问题,而无须你执行app或者编写测试用例。工具检测到的每个错误都会标明严重程度一个描述信息,这样你就可以迅速的确定优先级。你也可以将在你项目中无关紧要的错误进行忽略。这个工具有相应的命令行接口,所以你可以非常容易的整合到你的测试进程中。
Lint工具通过检测你的Android项目源代码来发现潜在的bug并提供代码优化建议。你可以从命令行或者Android Studio中运行Lint。
这里写图片描述

Application 源文件(Application source files)

源文件包含组成你的Android项目的文件,包含Java和XML文件、图标和ProGuard配置文件。

Lint.xml文件

配置文件,用于配置你想忽略的问题,或者更高问题的严重级别。

Lint工具

你可以通过命令行或者Android Studio运行在你的Android项目上的静态代码扫描工具。Lint工具检测影响你的Android应用的质量和性能的代码结构性问题。强烈建议在发布你的应用之前修复Lint检测到的任何问题

Lint检测结果

你可以在Android Studio的Event Log中来查看Lint的结果(或者在命令行中)。
Lint工具作为Android SDK工具的一部分(>=16)。

在Android Studio中运行Lint

在Android Studio中,当你构建你的app时,Lint自动运行。
在Android Studio中你可以在android设置中添加lintOptions属性。如下:

android {
    lintOptions {
       // 设置为true会关闭lint分析进度
       quiet true
       // 如果为true,则在发现错误时停止gradle构建
       abortOnError false
       // 如果为true,则只报告错误
       ignoreWarnings true
       }
}

命令行运行Lint

运行lint检测项目:

lint [flags] <project directory>

检测项目的某个错误,以下这个用于检测Android命名空间前缀:

lint --check MissingPrefix myproject

查看lint帮助,可用:

Lint输出示例

接下来展示lint检测一个Earthquake的项目输出的信息:

$ lint Earthquake

Scanning Earthquake: ...............................................................................................................................
Scanning Earthquake (Phase 2): .......
AndroidManifest.xml:23: Warning: <uses-sdk> tag appears after <application> tag [ManifestOrder]
  <uses-sdk android:minSdkVersion="7" />
  ^
AndroidManifest.xml:23: Warning: <uses-sdk> tag should specify a target API level (the highest verified version; when running on later versions, compatibility behaviors may be enabled) with android:targetSdkVersion="?" [UsesMinSdkAttributes]
  <uses-sdk android:minSdkVersion="7" />
  ^
res/layout/preferences.xml: Warning: The resource R.layout.preferences appears to be unused [UnusedResources]
res: Warning: Missing density variation folders in res: drawable-xhdpi [IconMissingDensityFolder]
0 errors, 4 warnings

配置lint

默认情况下,当你运行一个扫描的时候,会检测Lint支持的所有的issue。你可以禁止Lint检测某个issue或者设置issue的安全级别。
你可以在不同级别来配置Lint检测:
1、 全局,针对整个项目
2、 每个项目模块
3、 每个测试模块
等等。

在Android Studio中配置Lint

当你使用Android Studio时,内置的Lint工具可以检测你的代码。你可以通过两种方式来查看警告和错误:
1、 代码编辑器中,当Lint发现错误之后,会黄色高翔显示问题代码。
2、 选择Analyze > Inspect Code,打开Lint Inspection Results。

设置默认Lint检测:

1、 在Android Studio中,打开你的项目。
2、 选择File > Other Settings > Default Settings
3、 选择Editor > Inspections,打开Default Preferences对话框。
4、 在Profile中,选择Default或者Project Default。
5、 根据需要更改Lint设置。
6、 点击OK。

将Lint检测显示在Inspection Results窗口:

1、 在Android Studio中,打开项目,选择你要测试的部分
2、 选择Analyze > Inspect Code
3、 在Specify Inspection Scope对话框中,选择需要测试的部分
4、 点击OK。
结构将根据分类显示在Inspection Results窗口。

配置Lint文件

你可以在lint.xml文件中声明Lint检测参数。如果你手动创建这个文件,将它放在你的Android项目的根目录。如果你在Android Studio中配置你的Lint参数,lint.xml自动生成并添加到你的项目中。
Lint.xml 文件包含一个父标签,包含一个或多个标签。如下:

<?xml version="1.0" encoding="UTF-8"?>
    <lint>
        <!-- list of issues to configure -->
</lint>

通过设置标签,你可以禁用Lint检测某个issue或者更高某个issue的级别。
注意:查看lint支持的issue列表,可以运行lint –list命令
Lint.xml文件示例
下面是一个lint.xml文件示例:

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <!-- Disable the given check in this project -->
    <issue id="IconMissingDensityFolder" severity="ignore" />

    <!-- Ignore the ObsoleteLayoutParam issue in the specified files -->
    <issue id="ObsoleteLayoutParam">
        <ignore path="res/layout/activation.xml" />
        <ignore path="res/layout-xlarge/activation.xml" />
    </issue>

    <!-- Ignore the UselessLeaf issue in the specified file -->
    <issue id="UselessLeaf">
        <ignore path="res/layout/main.xml" />
    </issue>

    <!-- Change the severity of hardcoded strings to "error" -->
    <issue id="HardcodedText" severity="error" />
</lint>

在Java和XML源文件中配置lint检测

你可以在Java和XML文件中禁用Lint检测

在Java中配置lint检测

为了在你的Android项目中指定的类或方法中金庸Lint检测,在Java代码中添加@SupprewwLint注解。

接下来的示例显示在OnCreate方法中如何关闭Lint检测,Lint工具在其他方法中继续检测NewApi issue。

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.main);

下面的示例显示如何关闭ParserError issue:

@SuppressLint("ParserError")
public class FeedProvider extends ContentProvider {}

在Java文件中禁止所有Lint检测,如下:

@SuppressLint("all")

在XML中配置Lint检测

在XML文件中,你可以使用tools:ignore属性来金庸Lint检测。为了使相关属性被Lint工具识别,必须添加如下命名空间到你的XML文namespace xmlns:tools=http://schemas.android.com/tools下:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedResources" >

    <TextView
        android:text="@string/auto_update_prompt" />
</LinearLayout>

禁用多个issue,如下:

tools:ignore="NewApi,StringFormatInvalid"

禁用所有的issue 检测,如下:

tools:ignore="all"

本文作者:宋志辉
个人微博:点击进入

目录
相关文章
|
12月前
|
SQL 人工智能 Dart
Android Studio的插件生态非常丰富
Android Studio的插件生态非常丰富
679 1
|
12月前
|
Ubuntu Linux Android开发
Android Studio支持多种操作系统
Android Studio支持多种操作系统
548 1
|
7月前
|
开发工具 Android开发 iOS开发
如何在Android Studio中配置Flutter环境?
如何在Android Studio中配置Flutter环境?
1709 61
|
4月前
|
编解码 Java Android开发
安卓虚拟摄像头免root版,虚拟摄像头替换真实摄像头,jar代码开源分享
通过动态替换摄像头输入流的方式实现虚拟摄像头功能,代码经过简化展示核心逻辑。实际开发中还需要考虑视频编解码优化
|
6月前
|
Android开发 Windows
Android studio 报错Connect to 127.0.0.1:8888 [/127.0.0.1] failed: Connection refused: connect(已解决)
这是一篇关于解决Android Studio报错“Connect to 127.0.0.1:8888 failed: Connection refused”的文章。问题通常因系统代理设置被Android Studio自动保存导致。解决方法是找到系统中Android Studio使用的gradle.properties文件(位于Windows的C:\Users\你的电脑用户名\.gradle或Mac的/Users/.{你的用户目录}/.gradle),删除或注释掉多余的代理配置后保存并重新Sync项目。希望此经验能帮助快速解决同类问题!
897 36
|
6月前
|
Java Android开发
Android studio中build.gradle文件简单介绍
本文解析了Android项目中build.gradle文件的作用,包括jcenter仓库配置、模块类型定义、包名设置及依赖管理,涵盖本地、库和远程依赖的区别。
583 19
|
9月前
|
前端开发 Java 编译器
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
252 36
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
|
9月前
|
Dart 前端开发 Android开发
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
266 4
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
12月前
|
前端开发 数据处理 Android开发
Flutter前端开发中的调试技巧与工具使用方法,涵盖调试的重要性、基本技巧如打印日志与断点调试、常用调试工具如Android Studio/VS Code调试器和Flutter Inspector的介绍
本文深入探讨了Flutter前端开发中的调试技巧与工具使用方法,涵盖调试的重要性、基本技巧如打印日志与断点调试、常用调试工具如Android Studio/VS Code调试器和Flutter Inspector的介绍,以及具体操作步骤、常见问题解决、高级调试技巧、团队协作中的调试应用和未来发展趋势,旨在帮助开发者提高调试效率,提升应用质量。
405 8
|
12月前
|
数据可视化 开发工具 Android开发
Android Studio
Android Studio
736 1

热门文章

最新文章