android代碼審查工具---lint工具的使用


轉載請著名出處:http://blog.csdn.net/lijunhuayc



搞了這麼久android我居然不知道lint工具是幹啥的,雖然每次在eclipse下右鍵項目android tools下面都能看到它,我卻木有去瞧瞧她能幹嘛~作爲一枚android搬磚人,我感到羞愧啊~~~~~

好吧,既然沒用過,那幾天看到這玩意兒,知道她能幹啥了,那麼就來現場調戲她一番再說~

你想調戲她麼?跟着我來吧,絕對新手級別現場表演~

先來看看谷歌是如何說的:

The Android lint tool is a static code analysis tool that checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization.

In Android Studio, the configured lint and other IDE inspections run automatically whenever you compile your program. You can also manually run inspections in Android Studio by selecting Analyze > Inspect Code from the application or right-click menu. The Specify Inspections Scope dialog appears so you can specify the desired inspection profile and scope.

大體意思就是lint工具可以從各個方面checks你的項目潛在的bug等等。在你編譯運行的時候它會自動運行。你也可以手動分析程序。(一下就暴露了我是英語渣渣~~~以後不能裝逼了~~)

具體詳細的可以看這裏:http://developer.android.com/tools/help/lint.html 

eclipse中運行lint有兩種方式,項目左上角工具欄裏面有個  帶勾勾  的工具可以單個或者批量檢查項目,其次是右鍵項目android tools裏面 Run lint ....


好吧,拿個項目跑一下試試:




我勒個去,不查不知道一check嚇一大跳。這麼多嗎蛋的問題。不說了,哭一會去了~~~


好吧,接下來一個一個的看看是不是都是有問題的,雙擊可以跳轉到對應位置去。


1、第一個紅色錯誤:是layout佈局裏面用@+id 定義了多個相同的id,之所以錯誤是因爲這個layout對應的界面只顯示界面,暫時沒有使用數據,佈局文件有雷同的地方,存在複製粘貼的操作導致的,這裏修改一下id就可以了


2、第二個紅色錯誤:提示信息 “ Class referenced in the manifest, com.goldtel.eim.activity.ChatNoticeActivity, was not found in the 
 project or the libraries
 ”。原來是manifest裏面註冊的activity沒找到,檢查發現是此activity已經不存在了。so以後注意,刪除不用的activity記得取消manifest中的註冊。


3、第三個紅色錯誤:

提示:“  Placing a <WebView> in a parent element that uses a wrap_content layout_height can lead to subtle 
 bugs; use match_parent instead
 ” webview的parent element使用了wrap_content作爲height屬性可能引起微妙的BUG,建議使用match_parent代替。


xml代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:fadeScrollbars="true"
        android:fastScrollEnabled="true"
        android:layout_weight="1" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/topLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="15dp" >

                <TextView
                    android:id="@+id/topTitleTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="18dp"
                    android:ellipsize="end"
                    android:shadowColor="@android:color/holo_blue_light"
                    android:shadowDx="2.0"
                    android:shadowDy="2.0"
                    android:gravity="center"
                    android:shadowRadius="4.0"
                    android:text="(消息服務中心)"
                    android:textColor="#259ddb"
                    android:textSize="20sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/titleLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/topLayout"
                android:gravity="left|center_vertical"
                android:padding="15dp" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/icon_recent_sysmsg" />

                <TextView
                    android:id="@+id/titleTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="25dp"
                    android:text="來自xxx發送的工作組通告" />
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/sysLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/titleLayout"
                android:padding="15dp"
                android:visibility="visible" >

                <TextView
                    android:id="@+id/sysContent"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dip"
                    android:text="asfasfsaf" />
            </RelativeLayout>

            <WebView
                android:id="@+id/webLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_below="@id/titleLayout"
                android:padding="15dp"
                android:visibility="gone" />

            <View
                android:id="@+id/loadingView"
                android:layout_width="65dp"
                android:layout_height="65dp"
                android:layout_below="@id/titleLayout"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="100dp"
                android:background="@anim/loading_anim_gray"
                android:visibility="gone" />
        </RelativeLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@+id/bottomLayout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/chat_bottombar_bg2"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingTop="6.0dip" >
    </RelativeLayout>

</LinearLayout>

修改webview父標籤的屬性爲match_parent,但我們發現webview的父標籤是ScrollView的一個子元素,谷歌推薦我們ScrollView的子元素應該使用warp_parent屬性,所以又會出現黃色的警告信息,這裏無關緊要,不管。




依次看下面的黃色警示信息,看看哪些是需要修改的~


1、The <service> com.gl.listener.BaseListenerService is not registered in the manifest 提示說這個service沒有在manifest註冊,查看這個服務是個基類服務,不需要註冊的。還有其他的service也是提示沒註冊的,其他的service是沒有使用到的廢棄的類,可以直接刪除了,醬紫就縮減了項目。


2、Expected duration Toast.LENGTH_SHORT or Toast.LENGTH_LONG, a custom duration value is not supported 提示說Toast.makeText的duration參數推薦應該使用Toase.LENGTH_SHORT或者Toast.LENGTH_LONG之一,沒影響不用修改。

查看了大部分警告信息,大都是無關緊要的提示,是google推薦我們的一些詳細的具體的規範,但有時候這些規範不利於我們編碼,so不一定要完全遵守,舉個例子:比如說使用ImageView控件的時候會提示我們需要一個contentDescription屬性,但是我們完全可以不寫這個屬性。


3、The resource R.dimen.bottom_tab_padding_drawable appears to be unused 提示說這個資源沒有被使用,意思就是說這些冗餘的資源文件中的信息,我們不再使用過來,可以刪除了。其他還有很多這樣的信息。依次排查。經過大量的篩查可以精簡項目文件。



看這裏有300多條沒使用的資源信息,其中包括乳品,動畫xml,背景xml,字符串資源,dimens資源等等,刪除可以減少安裝包大小。並且使項目結果較爲清晰,不然有朝一日[N年後的今天],你再次維護此項目的時候會讓你頭疼到死,因爲你無法知道當初留下這些沒有使用的資源的用意何在(當然,這一條同樣適用於代碼冗餘審查,留下不使用的代碼比資源更加難於維護)。


4、下面還有一條performance類型的警告信息,這類型的警告信息就是影響性能的警示信息了,能處理的儘量處理掉,比如:This LinearLayout layout or its RelativeLayout parent is possibly useless。本項目裏面定位到layout文件查看,發現是佈局頂層使用了一個RelativeLayout,然後包着一個LinearLayout作爲子佈局,其他所有佈局控件均在這個LinearLayout內部,意思就是說這裏的佈局層級冗餘了,頂層的相對佈局和二層的線性佈局有一個無濟於事,意思就是多了一層無用的佈局,可以去除掉一層。我們這裏的處理是刪除最外層RL佈局,這樣做的好處是LL內部的佈局是依賴LL來佈局的,如果刪除LL內部的佈局務必需要調整爲依賴RL,會大量修改,而刪除RL則不會影響到LL內部的控件佈局。一個字:方便!!!




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章