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


    为了测试你的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"

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

  • 相关阅读:
    使用MySQL Workbench建立数据库,建立新的表,向表中添加数据
    IntelliJ IDEA15开发时设置中java complier 的问题
    IntelliJ 15 unmapped spring configuration files found
    Redis 的性能
    SSH框架
    jquery插件模版
    cygwin,在win中开发linux程序
    MinGw与CyGwin
    升级到tomcat8时Artifact SpringMvcDemo:war exploded: Server is not connected. Deploy is not
    Socket连接超时(转)
  • 原文地址:https://www.cnblogs.com/hainange/p/6153442.html
Copyright © 2020-2023  润新知