第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

全部開發(fā)者教程

Android Studio 編輯器教程

Android Studio 檢查改進(jìn)代碼

前一小節(jié)我們介紹了如何查找優(yōu)質(zhì)的 Android 代碼示例,本小結(jié)我們學(xué)習(xí)如何通過 lint 來檢查我們的代碼,如何發(fā)現(xiàn)代碼中存在的問題,保證應(yīng)用能高效穩(wěn)定的運(yùn)行。

1. 什么是 lint

Android Studio 提供了一個(gè)名為 lint 的代碼掃描工具,可幫助我們發(fā)現(xiàn)并更正代碼結(jié)構(gòu)質(zhì)量的問題,而無需實(shí)際執(zhí)行應(yīng)用,也不必編寫測(cè)試用例。系統(tǒng)會(huì)報(bào)告該工具檢測(cè)到的每個(gè)問題并提供問題的描述消息和嚴(yán)重級(jí)別,以便我們可以快速確定需要優(yōu)先進(jìn)行的關(guān)鍵改進(jìn)。此外,我們還可以降低問題的嚴(yán)重級(jí)別以忽略與項(xiàng)目無關(guān)的問題,或者提高嚴(yán)重級(jí)別以突出特定問題。

lint 工具可以檢查 Android 項(xiàng)目源文件是否有潛在的錯(cuò)誤,以及在正確性、安全性、性能、易用性、無障礙性和國(guó)際化方面是否需要優(yōu)化改進(jìn)。使用 Android Studio 時(shí),無論何時(shí)構(gòu)建應(yīng)用,都會(huì)運(yùn)行配置的 lint 和 IDE 檢查。

下圖顯示了 lint 工具如何掃描處理文件:

  • 應(yīng)用源文件:源文件包含組成 Android 項(xiàng)目的文件,包括 Java、Kotlin 和 XML 文件、圖標(biāo)以及 ProGuard 配置文件;

  • lint.xml 文件:一個(gè)配置文件,可用于指定要排除的任何 lint 檢查以及自定義問題嚴(yán)重級(jí)別;

  • lint 工具:一個(gè)靜態(tài)代碼掃描工具,我們可以從命令行或在 Android Studio 中對(duì) Android 項(xiàng)目運(yùn)行該工具;

  • lint 檢查結(jié)果:我們可以在控制臺(tái)或 Android Studio 的 Inspection Results 窗口中查看 lint 檢查結(jié)果。

2. lint 的配置

默認(rèn)情況下,當(dāng)我們運(yùn)行 lint 掃描時(shí),lint 工具會(huì)檢查它支持的所有問題。 我們也可以限制 lint 要檢查的問題,并為這些問題分配嚴(yán)重級(jí)別。例如,我們可以禁止對(duì)與項(xiàng)目無關(guān)的特定問題進(jìn)行 lint 檢查,也可以將 lint 配置為以較低的嚴(yán)重級(jí)別報(bào)告非關(guān)鍵問題。

2.1 通過 lint 文件配置

我們可以在 lint.xml 文件中指定 lint 檢查偏好設(shè)置。如果我們是手動(dòng)創(chuàng)建此文件,請(qǐng)將其放置在 Android 項(xiàng)目的根目錄下。

lint.xml 文件由封閉的 <lint> 父標(biāo)記組成,此標(biāo)記包含一個(gè)或多個(gè) < issue > 子元素。lint 會(huì)為每個(gè) < issue > 定義唯一的 id 屬性值。

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

Tips:我們可以通過在 issue 標(biāo)記中設(shè)置嚴(yán)重級(jí)別屬性來更改某個(gè)問題的嚴(yán)重級(jí)別或?qū)υ搯栴}停用 lint 檢查。如需查看 lint 支持的問題及其對(duì)應(yīng)的問題 ID 的完整列表,請(qǐng)運(yùn)行 lint --list 命令。

以下是 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>

2.2 通過 Gradle 配置

通過 Android Plugin for Gradle,我們可以使用模塊級(jí) build.gradle 文件中的 lintOptions {} 代碼塊配置某些 lint 選項(xiàng),例如要運(yùn)行或忽略哪些檢查。以下代碼段展示了可以配置的部分屬性:

android {
  ...
  lintOptions {
    // Turns off checks for the issue IDs you specify.
    disable 'TypographyFractions','TypographyQuotes'
    // Turns on checks for the issue IDs you specify. These checks are in
    // addition to the default lint checks.
    enable 'RtlHardcoded','RtlCompat', 'RtlEnabled'
    // To enable checks for only a subset of issue IDs and ignore all others,
    // list the issue IDs with the 'check' property instead. This property overrides
    // any issue IDs you enable or disable using the properties above.
    check 'NewApi', 'InlinedApi'
    // If set to true, turns off analysis progress reporting by lint.
    quiet true
    // if set to true (default), stops the build if errors are found.
    abortOnError false
    // if true, only report errors.
    ignoreWarnings true
  }
}
...

2.3 在源文件中配置 Lint

配置 Java 的 lint 檢查

要專門對(duì) Android 項(xiàng)目中的某個(gè)類或方法停用 lint 檢查,請(qǐng)向該代碼添加 @SuppressLint 注釋。
以下示例展示了如何對(duì) onCreate 方法中的 NewApi 問題關(guān)閉 lint 檢查。lint 工具會(huì)繼續(xù)檢查該類的其他方法中的 NewApi 問題。

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

以下示例展示了如何對(duì) FeedProvider 類中的 ParserError 問題關(guān)閉 lint 檢查:

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

要禁止 lint 檢查文件中的所有問題,請(qǐng)使用 all 關(guān)鍵字,如下所示:

@SuppressLint("all")

配置 XML 的 lint 檢查

我們通過 tools:ignore 屬性對(duì) XML 文件的特定部分停用 lint 檢查。在 lint.xml 文件中添加以下命名空間值,以便 lint 工具能夠識(shí)別該屬性:

namespace xmlns:tools="http://schemas.android.com/tools"

以下示例展示了如何對(duì) XML 布局文件的 < LinearLayout > 元素中的 UnusedResources 問題關(guān)閉 lint 檢查。如果某個(gè)父元素聲明了 ignore 屬性,則該元素的子元素會(huì)繼承此屬性。在本例中,也會(huì)對(duì) <TextView> 子元素停用 lint 檢查。

<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>

要禁止檢查多個(gè)問題,請(qǐng)使用以逗號(hào)分隔的字符串列出要禁止檢查的問題。例如:

tools:ignore="NewApi,StringFormatInvalid"

要禁止 lint 檢查 XML 元素中的所有問題,請(qǐng)使用 all 關(guān)鍵字,如下所示:

tools:ignore="all"

3. 如何運(yùn)行 Lint

3.1 從命令行運(yùn)行 lint

我們可以在項(xiàng)目的根目錄下輸入以下某個(gè)命令,使用 Gradle 封裝容器 對(duì)項(xiàng)目調(diào)用 lint 任務(wù):

  • 在 Windows 上:
gradlew lint
  • 在 Linux 或 Mac 上:
./gradlew lint

我們會(huì)看到類似于以下內(nèi)容的輸出:

lint 工具完成其檢查后,會(huì)提供 XML 和 HTML 版 Lint 報(bào)告的路徑。然后,我們可以轉(zhuǎn)到 HTML 報(bào)告并在瀏覽器中將其打開,如下圖所示:

3.2 從 Android Studio 運(yùn)行 lint

在 Android Studio 中手動(dòng)運(yùn)行 lint 檢查,請(qǐng)按如下步驟:

  1. 在 Android 視圖中,打開我們的項(xiàng)目,然后選擇要分析的項(xiàng)目、文件夾或文件;

  2. 從菜單欄中,依次選擇 Analyze > Inspect Code

  3. Specify Inspection Scope 對(duì)話框中,查看設(shè)置:

  4. Inspection profile 下,保留默認(rèn)配置文件 (Project Default);

  5. 點(diǎn)擊 OK 以運(yùn)行檢查。

通過運(yùn)行 Inspect Code 所生成的 lint 及其他 IDE 檢查結(jié)果如下圖:

  • 在左側(cè)窗格樹狀視圖中,通過展開并選擇錯(cuò)誤類別、類型和問題來查看檢查結(jié)果;

  • 在右側(cè)窗格顯示選定錯(cuò)誤類別、類型或問題的檢查報(bào)告,并提供錯(cuò)誤的名稱和位置。在適用情況下,檢查報(bào)告會(huì)顯示問題概要等其他信息,以幫助我們更正問題;

  • 在左側(cè)窗格樹狀視圖中,右鍵點(diǎn)擊某個(gè)類別、類型或問題,以顯示上下文菜單。根據(jù)上下文,我們可以執(zhí)行以下全部或部分操作:跳到源代碼、排除和包含選定項(xiàng)、抑制問題、修改設(shè)置、管理檢查警報(bào)和重新運(yùn)行檢查。

4. 小結(jié)

本節(jié)課程我們主要學(xué)習(xí)了 Android Studio 如何通過 lint 來檢查我們的代碼。本節(jié)課程的重點(diǎn)如下:

  • 了解如何配置 lint;
  • 了解如何使用 lint 檢查代碼和更正問題。