您的位置:首页 > 移动开发 > Android开发

android代码审查工具---lint工具的使用

2015-03-27 13:23 417 查看
转载请著名出处: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内部的控件布局。一个字:方便!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: