當前位置:編程學習大全網 - 源碼下載 - 怎樣定制Android-Lint檢查問題的現有規則

怎樣定制Android-Lint檢查問題的現有規則

Android-Lint:查錯與代碼優化利器

Android-Lint的簡述:Lint檢查哪些問題;如何使用;有哪些選項;與其他系統集成Windows Live Blog。

定制Android-Lint檢查問題的現有規則

對Android-Lint發現的問題進行處理。可定制項目中采用的規則。

Android-Lint檢查問題列表

Android SDK Tools / ADT 20.0.3中所支持的默認檢查的所有問題。

有英文版本和中文版本。英文版本從Android-Lint中直接導出;中文版本還不完整,對每壹條的解釋會逐步完善。

當然,最關鍵最權威的還是應該看官方網站:/tips/lint

Android SDK自帶的APIDemo用Android-Lint檢查壹下,也還報了很多的問題。

壹、忽略XML文件中的問題

1.1 MissingPrefix問題

Layout的device_admin_sample.xml文件中定義了下面的Button

[html] view plaincopyprint?

<Button

android:id="@+id/set_password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android_layout_gravity="east|center_vertical"

android:text="@string/set_password">

</Button>

<Button

android:id="@+id/set_password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android_layout_gravity="east|center_vertical"

android:text="@string/set_password">

</Button>執行Android-Lint就會報MissingPrefix警告:

1.2 解決XML中的問題

可以在Lint Warnings View中解決(圖中圖標從左至右順序)

Suppress this error with an annotation/attribute

點擊該圖標之後,直接更改了device_admin_sample.xml文件:

[html] view plaincopyprint?

<Button

android:id="@+id/set_password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android_layout_gravity="east|center_vertical"

android:text="@string/set_password"

tools:ignore="MissingPrefix" >

</Button>

<Button

android:id="@+id/set_password"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android_layout_gravity="east|center_vertical"

android:text="@string/set_password"

tools:ignore="MissingPrefix" >

</Button>XML文件中直接增加了tools:ignore="MissingPrefix"。

Ignore in this file

在本文件中忽略,而在別的文件中仍然出現。

Ignore in this project

當前項目中都忽略該Issue。

執行之後,在本項目根目錄下創建了lint.xml的文件,內容為:

[html] view plaincopyprint?

<?xml version="1.0"encoding="UTF-8"?>

<lint>

<issue id="MissingPrefix"severity="ignore" />

</lint>

<?xml version="1.0"encoding="UTF-8"?>

<lint>

<issue id="MissingPrefix"severity="ignore" />

</lint> Always ignore

所有項目中都忽略。

[TIPS#1] Ignore in this project和 Always ignore操作,同執行Lint Warnings View中的最後壹個Icon -- Options…,然後配置某壹個項目或者全局設置中該Issue的Severity為ignore。

[TIPS#2] Eclipse中的實現有BUG,有時設置了這些Ignore操作,即便立即執行檢查也不壹定生效,需要重啟Eclipse。

二、解決Java代碼中的問題

2.1 NewAPI問題

APIDemo中指明了支持最低API-1,但是代碼裏卻用了API-3的接口,執行Lint會報錯:

2.2 解決問題

把光標放在報錯代碼處,會自動提示如何快速fix。

1. 前面紅色圓角框內是用Javaannotation方式解決(API-16之後才有):@SuppressLint‘<IssueId>’或@TargetAPI(<api>)

@SuppressLint ‘<IssueId>’用來忽略<IssueId>。適用範圍在所調用的方法處或整個類中。

@TargetAPI(<api>)用來指示API用給定的<api>,而不是項目中指定的。適用範圍在所調用的方法處或整個類中。

2. 後面粉紅色圓角框內同XML中解決方式——在本文件/ 本項目/ 全局範圍內忽略檢查。

三、命令行下解決問題

從上面Eclipse環境下的解決問題的方式知道,可以指定文件來定制Lint檢查Issue的處理方式。

下面是壹個lint.xml的例子:

[html] view plaincopyprint?

<?xml version="1.0" encoding="UTF-8"?>

<lint>

<!-- Disable the given check in thisproject -->

<issue id="IconMissingDensityFolder" severity="ignore" />

<!-- Ignore the ObsoleteLayoutParamissue in the given files -->

<issue id="ObsoleteLayoutParam">

<ignore path="res/layout/activation.xml" />

<ignore path="res/layout-xlarge/activation.xml" />

</issue>

<!-- Ignore the UselessLeaf issue inthe given file -->

<issue id="UselessLeaf">

<ignore path="res/layout/main.xml" />

</issue>

<!-- Change the severity of hardcodedstrings to "error" -->

<issue id="HardcodedText" severity="error" />

</lint>

<?xml version="1.0" encoding="UTF-8"?>

<lint>

<!-- Disable the given check in thisproject -->

<issue id="IconMissingDensityFolder" severity="ignore" />

<!-- Ignore the ObsoleteLayoutParamissue in the given files -->

<issue id="ObsoleteLayoutParam">

<ignore path="res/layout/activation.xml" />

<ignore path="res/layout-xlarge/activation.xml" />

</issue>

<!-- Ignore the UselessLeaf issue inthe given file -->

<issue id="UselessLeaf">

<ignore path="res/layout/main.xml" />

</issue>

<!-- Change the severity of hardcodedstrings to "error" -->

<issue id="HardcodedText" severity="error" />

</lint>Lint.xml中關鍵是對issue(用id指定)的severity進行指定,並且可以指定該issue作用於指定的文件還是當前項目。

把lint.xml放在項目的根目錄中,命令行執行lint時候,lint就會用lint.xml中的規則。

另外,執行lint時還可以用參數--config<fileName>指定壹個全局的配置用於所有的項目。當項目中已有lint.xml,則對於某個issue而言,在lint.xml中沒有對該issue特別定制的情況下,--config指定的文件<fileName>中的該issue的定制才起作用。

四、定制Lint檢查的規則

Android-Lint有默認的檢查和報錯的規則,但通過上面的分析知道,可以在Eclipse或者命令行下改變這種規則,從而可以定制Lint檢查的規則。

您好!

您提出的問題,我的答案已經給出,請您瀏覽壹遍!

有什麽不懂的地方歡迎回復我!

希望我的答案對您有所幫助!

如果滿意請及時點擊采納為滿意答案按鈕

若是客戶端的朋友在右上角評價點滿意

您的采納!

是我答題的動力

也同時給您帶來知識和財富值

O(∩_∩)O謝謝您!!!

  • 上一篇:什麽是博客,怎樣加入博客
  • 下一篇:魔獸世界 今天更新後就進不去了,大腳顯示是安全檢測,直接進EXE也進不去。。有圖。
  • copyright 2024編程學習大全網