Android静态代码检查

gradle lint

gradle中有lint任务,可以直接执行lint静态代码检查,但是前提是你的build.gradle设置了lintOptions选项:

<code class="hljs bash has-numbering">android {
    compileSdkVersion <span class="hljs-number">23</span>
    buildToolsVersion <span class="hljs-string">"21.1.2"</span>
    lintOptions {
          abortOnError <span class="hljs-literal">false</span>
      }</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>

然后我们在项目的根目录下执行gradle lint

<code class="hljs asciidoc has-numbering">localhost:Sunshine-Version-2 wuxian$ gradle lint
<span class="hljs-attribute">:app:preBuild</span> UP-TO-DATE
<span class="hljs-attribute">:app:preDebugBuild</span> UP-TO-DATE
<span class="hljs-attribute">:app:checkDebugManifest</span>
<span class="hljs-attribute">:app:preReleaseBuild</span> UP-TO-DATE
<span class="hljs-attribute">:app:prepareComAndroidSupportAppcompatV72102Library</span> UP-TO-DATE
<span class="hljs-attribute">:app:prepareComAndroidSupportSupportV42102Library</span> UP-TO-DATE
<span class="hljs-attribute">:app:prepareDebugDependencies</span>
<span class="hljs-attribute">:app:compileDebugAidl</span> UP-TO-DATE
<span class="hljs-attribute">:app:compileDebugRenderscript</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateDebugBuildConfig</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateDebugAssets</span> UP-TO-DATE
<span class="hljs-attribute">:app:mergeDebugAssets</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateDebugResValues</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateDebugResources</span> UP-TO-DATE
<span class="hljs-attribute">:app:mergeDebugResources</span> UP-TO-DATE
<span class="hljs-attribute">:app:processDebugManifest</span> UP-TO-DATE
<span class="hljs-attribute">:app:processDebugResources</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateDebugSources</span> UP-TO-DATE
<span class="hljs-attribute">:app:processDebugJavaRes</span> UP-TO-DATE
<span class="hljs-attribute">:app:compileDebugJavaWithJavac</span> UP-TO-DATE
<span class="hljs-attribute">:app:compileLint</span>
<span class="hljs-attribute">:app:checkReleaseManifest</span>
<span class="hljs-attribute">:app:prepareReleaseDependencies</span>
<span class="hljs-attribute">:app:compileReleaseAidl</span> UP-TO-DATE
<span class="hljs-attribute">:app:compileReleaseRenderscript</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateReleaseBuildConfig</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateReleaseAssets</span> UP-TO-DATE
<span class="hljs-attribute">:app:mergeReleaseAssets</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateReleaseResValues</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateReleaseResources</span> UP-TO-DATE
<span class="hljs-attribute">:app:mergeReleaseResources</span> UP-TO-DATE
<span class="hljs-attribute">:app:processReleaseManifest</span> UP-TO-DATE
<span class="hljs-attribute">:app:processReleaseResources</span> UP-TO-DATE
<span class="hljs-attribute">:app:generateReleaseSources</span> UP-TO-DATE
<span class="hljs-attribute">:app:processReleaseJavaRes</span> UP-TO-DATE
<span class="hljs-attribute">:app:compileReleaseJavaWithJavac</span> UP-TO-DATE
<span class="hljs-attribute">:app:lint</span>
Ran lint on variant release: 58 issues found
Ran lint on variant debug: 58 issues found
Wrote HTML report to file:/Users/wuxian/Documents/sourcecode/self/Sunshine-Version-2/app/build/outputs/lint-results.html
Wrote XML report to /Users/wuxian/Documents/sourcecode/self/Sunshine-Version-2/app/build/outputs/lint-results.xml

BUILD SUCCESSFUL

Total time: 17.941 secs

This build could be faster, please consider using the Gradle Daemon: http://gradle.org/docs/2.4/userguide/gradle<span class="hljs-emphasis">_daemon.html
</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li></ul>

build/outputs/目录下会生成一个lint-result.html文件,打开一看如下:这里写图片描述

报告做的太次了

更多参数

<code class="hljs sql has-numbering">lintOptions {
        // <span class="hljs-operator"><span class="hljs-keyword">set</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">true</span> <span class="hljs-keyword">to</span> turn off analysis progress reporting <span class="hljs-keyword">by</span> lint
        quiet <span class="hljs-keyword">true</span>
        // <span class="hljs-keyword">if</span> <span class="hljs-keyword">true</span>, stop the gradle build <span class="hljs-keyword">if</span> errors <span class="hljs-keyword">are</span> <span class="hljs-keyword">found</span>
        abortOnError <span class="hljs-keyword">false</span>
        // <span class="hljs-keyword">if</span> <span class="hljs-keyword">true</span>, <span class="hljs-keyword">only</span> report errors
        ignoreWarnings <span class="hljs-keyword">true</span>
        // <span class="hljs-keyword">if</span> <span class="hljs-keyword">true</span>, emit <span class="hljs-keyword">full</span>/<span class="hljs-keyword">absolute</span> paths <span class="hljs-keyword">to</span> files <span class="hljs-keyword">with</span> errors (<span class="hljs-keyword">true</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">default</span>)
        //absolutePaths <span class="hljs-keyword">true</span>
        // <span class="hljs-keyword">if</span> <span class="hljs-keyword">true</span>, <span class="hljs-keyword">check</span> <span class="hljs-keyword">all</span> issues, including those that <span class="hljs-keyword">are</span> off <span class="hljs-keyword">by</span> <span class="hljs-keyword">default</span>
        checkAllWarnings <span class="hljs-keyword">true</span>
        // <span class="hljs-keyword">if</span> <span class="hljs-keyword">true</span>, treat <span class="hljs-keyword">all</span> warnings <span class="hljs-keyword">as</span> errors
        warningsAsErrors <span class="hljs-keyword">true</span>
        // turn off checking the given issue id<span class="hljs-string">'s
        disable '</span>TypographyFractions<span class="hljs-string">','</span>TypographyQuotes<span class="hljs-string">'
        // turn on the given issue id'</span>s
        enable <span class="hljs-string">'RtlHardcoded'</span>,<span class="hljs-string">'RtlCompat'</span>, <span class="hljs-string">'RtlEnabled'</span>
        // <span class="hljs-keyword">check</span> *<span class="hljs-keyword">only</span>* the given issue id<span class="hljs-string">'s
        check '</span>NewApi<span class="hljs-string">', '</span>InlinedApi<span class="hljs-string">'
        // if true, don'</span>t include source code lines <span class="hljs-keyword">in</span> the error <span class="hljs-keyword">output</span>
        noLines <span class="hljs-keyword">true</span>
        // <span class="hljs-keyword">if</span> <span class="hljs-keyword">true</span>, <span class="hljs-keyword">show</span> <span class="hljs-keyword">all</span> locations <span class="hljs-keyword">for</span> an error, <span class="hljs-keyword">do</span> <span class="hljs-keyword">not</span> <span class="hljs-keyword">truncate</span> lists, etc.
        showAll <span class="hljs-keyword">true</span>
        // Fallback lint configuration (<span class="hljs-keyword">default</span> severities, etc.)
        lintConfig file(<span class="hljs-string">"default-lint.xml"</span>)
        // <span class="hljs-keyword">if</span> <span class="hljs-keyword">true</span>, generate a text report <span class="hljs-keyword">of</span> issues (<span class="hljs-keyword">false</span> <span class="hljs-keyword">by</span> <span class="hljs-keyword">default</span>)
        textReport <span class="hljs-keyword">true</span>
        // location <span class="hljs-keyword">to</span> <span class="hljs-keyword">write</span> the <span class="hljs-keyword">output</span>;</span> can be a file or 'stdout'
        textOutput 'stdout'
        // if true, generate an XML report for use by for example Jenkins
        xmlReport false
        // file to write report to (if not specified, defaults to lint-results.xml)
        xmlOutput file("lint-report.xml")
        // if true, generate an HTML report (with issue explanations, sourcecode, etc)
        htmlReport true
        // optional path to report (default will be lint-results.html in the builddir)
        htmlOutput file("lint-report.html")

        // <span class="hljs-operator"><span class="hljs-keyword">set</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">true</span> <span class="hljs-keyword">to</span> have <span class="hljs-keyword">all</span> release builds run lint <span class="hljs-keyword">on</span> issues <span class="hljs-keyword">with</span> severity=fatal
        // <span class="hljs-keyword">and</span> abort the build (controlled <span class="hljs-keyword">by</span> abortOnError above) <span class="hljs-keyword">if</span> fatal issues <span class="hljs-keyword">are</span> <span class="hljs-keyword">found</span>
        checkReleaseBuilds <span class="hljs-keyword">true</span>
        // <span class="hljs-keyword">Set</span> the severity <span class="hljs-keyword">of</span> the given issues <span class="hljs-keyword">to</span> fatal (which means they will be
        // checked during release builds (even <span class="hljs-keyword">if</span> the lint target <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> included)
        fatal <span class="hljs-string">'NewApi'</span>, <span class="hljs-string">'InlineApi'</span>
        // <span class="hljs-keyword">Set</span> the severity <span class="hljs-keyword">of</span> the given issues <span class="hljs-keyword">to</span> error
        error <span class="hljs-string">'Wakelock'</span>, <span class="hljs-string">'TextViewEdits'</span>
        // <span class="hljs-keyword">Set</span> the severity <span class="hljs-keyword">of</span> the given issues <span class="hljs-keyword">to</span> warning
        warning <span class="hljs-string">'ResourceAsColor'</span>
        // <span class="hljs-keyword">Set</span> the severity <span class="hljs-keyword">of</span> the given issues <span class="hljs-keyword">to</span> ignore (same <span class="hljs-keyword">as</span> disabling the <span class="hljs-keyword">check</span>)
        ignore <span class="hljs-string">'TypographyQuotes'</span>
    }</span></code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li></ul>

总结

这种方式适合自动化CI系统中采集结果。

手动分析

这里写图片描述

选在分析代码后,弹出对话框,你可以选择分析整个项目/单个模块/文件bulabula:

这里写图片描述

点击OK后,等待一段时间后分析完毕,会自动弹出结果窗口

这里写图片描述

这里写图片描述
会根据检查类型不同分不同菜单,点击相应菜单进入不同的问题点:

这里写图片描述

点击问题点后右边会出现该问题的详细信息,文件名,文件位置,出现问题的点在文件中的位置以及问题描述,还会告诉你忽略这个问题的解决方法,有可能还会显示解决方法。

总结

这个方法适合开发自查!

lint命令行

lint工具位于sdk目录下的tools下,如果在命令行执行lint找不到该命令,那么需要将lint的根目录配置到环境变量中,配置成功后,执行lint命令输出如下:

<code class="hljs vhdl has-numbering"><span class="hljs-number">58</span>deMacBook-Pro-<span class="hljs-number">5</span>:tools wuxian$ lint
Usage: lint [flags] <project directories>

Flags:

<span class="hljs-comment">--help                   This message.</span>
<span class="hljs-comment">--help <topic>           Help on the given topic, such as "suppress".</span>
<span class="hljs-comment">--list                   List the available issue id's and exit.</span>
<span class="hljs-comment">--version                Output version information and exit.</span>
<span class="hljs-comment">--exitcode               Set the exit code to 1 if errors are found.</span>
<span class="hljs-comment">--show                   List available issues along with full explanations.</span>
<span class="hljs-comment">--show <ids>             Show full explanations for the given list of issue</span>
                         id<span class="hljs-attribute">'s</span>.

Enabled Checks:
<span class="hljs-comment">--disable <list>         Disable the list of categories or specific issue</span>
                         id<span class="hljs-attribute">'s</span>. The list should be a comma-separated list <span class="hljs-keyword">of</span>
                         issue id<span class="hljs-attribute">'s</span> <span class="hljs-keyword">or</span> categories.
<span class="hljs-comment">--enable <list>          Enable the specific list of issues. This checks all</span>
                         the <span class="hljs-keyword">default</span> issues plus the specifically enabled
                         issues. The list should be a comma-separated list <span class="hljs-keyword">of</span>
                         issue id<span class="hljs-attribute">'s</span> <span class="hljs-keyword">or</span> categories.
<span class="hljs-comment">--check <list>           Only check the specific list of issues. This will</span>
                         disable everything <span class="hljs-keyword">and</span> re-enable the given list <span class="hljs-keyword">of</span>
                         issues. The list should be a comma-separated list <span class="hljs-keyword">of</span>
                         issue id<span class="hljs-attribute">'s</span> <span class="hljs-keyword">or</span> categories.
-w, <span class="hljs-comment">--nowarn             Only check for errors (ignore warnings)</span>
-Wall                    Check <span class="hljs-keyword">all</span> warnings, including those off by <span class="hljs-keyword">default</span>
-Werror                  Treat <span class="hljs-keyword">all</span> warnings as errors
<span class="hljs-comment">--config <filename>      Use the given configuration file to determine whether</span>
                         issues are enabled <span class="hljs-keyword">or</span> disabled. <span class="hljs-keyword">If</span> a project contains
                         a lint.xml <span class="hljs-keyword">file</span>, <span class="hljs-keyword">then</span> this config <span class="hljs-keyword">file</span> will be used
                         as a fallback.

Output Options:
<span class="hljs-comment">--quiet                  Don't show progress.</span>
<span class="hljs-comment">--fullpath               Use full paths in the error output.</span>
<span class="hljs-comment">--showall                Do not truncate long messages, lists of alternate</span>
                         locations, etc.
<span class="hljs-comment">--nolines                Do not include the source file lines with errors in</span>
                         the output. By <span class="hljs-keyword">default</span>, the error output includes
                         snippets <span class="hljs-keyword">of</span> source code <span class="hljs-keyword">on</span> the line containing the
                         error, but this flag turns it off.
<span class="hljs-comment">--html <filename>        Create an HTML report instead. If the filename is a</span>
                         directory (<span class="hljs-keyword">or</span> a <span class="hljs-keyword">new</span> filename without an extension),
                         lint will create a separate <span class="hljs-keyword">report</span> <span class="hljs-keyword">for</span> each scanned
                         project.
<span class="hljs-comment">--url filepath=url       Add links to HTML report, replacing local path</span>
                         prefixes <span class="hljs-keyword">with</span> url prefix. The mapping can be a
                         comma-separated list <span class="hljs-keyword">of</span> path prefixes <span class="hljs-keyword">to</span>
                         corresponding URL prefixes, such as
                         C:\temp\Proj1=http://buildserver/sources/temp/Proj1. 
                         <span class="hljs-keyword">To</span> turn off linking <span class="hljs-keyword">to</span> files, <span class="hljs-keyword">use</span> <span class="hljs-comment">--url none</span>
<span class="hljs-comment">--simplehtml <filename>  Create a simple HTML report</span>
<span class="hljs-comment">--xml <filename>         Create an XML report instead.</span>

Project Options:
<span class="hljs-comment">--resources <dir>        Add the given folder (or path) as a resource</span>
                         directory <span class="hljs-keyword">for</span> the project. Only valid <span class="hljs-keyword">when</span> running
                         lint <span class="hljs-keyword">on</span> a single project.
<span class="hljs-comment">--sources <dir>          Add the given folder (or path) as a source directory</span>
                         <span class="hljs-keyword">for</span> the project. Only valid <span class="hljs-keyword">when</span> running lint <span class="hljs-keyword">on</span> a
                         single project.
<span class="hljs-comment">--classpath <dir>        Add the given folder (or jar file, or path) as a</span>
                         class directory <span class="hljs-keyword">for</span> the project. Only valid <span class="hljs-keyword">when</span>
                         running lint <span class="hljs-keyword">on</span> a single project.
<span class="hljs-comment">--libraries <dir>        Add the given folder (or jar file, or path) as a</span>
                         class <span class="hljs-keyword">library</span> <span class="hljs-keyword">for</span> the project. Only valid <span class="hljs-keyword">when</span>
                         running lint <span class="hljs-keyword">on</span> a single project.

<span class="hljs-keyword">Exit</span> Status:
<span class="hljs-number">0</span>                        Success.
<span class="hljs-number">1</span>                        Lint errors detected.
<span class="hljs-number">2</span>                        Lint usage.
<span class="hljs-number">3</span>                        Cannot clobber existing <span class="hljs-keyword">file</span>.
<span class="hljs-number">4</span>                        Lint help.
<span class="hljs-number">5</span>                        Invalid command-line argument.</code>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值