<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Amal G.Raja on Medium]]></title>
        <description><![CDATA[Stories by Amal G.Raja on Medium]]></description>
        <link>https://medium.com/@amalgraja9393?source=rss-a21a327ff83e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*FRTDNVxGSaRT--fO</url>
            <title>Stories by Amal G.Raja on Medium</title>
            <link>https://medium.com/@amalgraja9393?source=rss-a21a327ff83e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 23:30:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amalgraja9393/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Static Code Analysis Tools]]></title>
            <link>https://medium.com/achieve-high-quality-android-code-with-static-code/static-code-analysis-tools-80558d9cb6cb?source=rss-a21a327ff83e------2</link>
            <guid isPermaLink="false">https://medium.com/p/80558d9cb6cb</guid>
            <category><![CDATA[android-lint]]></category>
            <category><![CDATA[checkstyle]]></category>
            <category><![CDATA[static-code-analysis]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[findbugs]]></category>
            <dc:creator><![CDATA[Amal G.Raja]]></dc:creator>
            <pubDate>Sun, 17 Jun 2018 19:54:24 GMT</pubDate>
            <atom:updated>2018-06-17T19:58:44.566Z</atom:updated>
            <content:encoded><![CDATA[<p>These are tools that parse and analyse your source code without actually executing it. The goal is to find potential vulnerabilities such as bugs and security flaws and ensure conformance to coding guidelines. Helps to keep your code healthy and maintain code quality.</p><p>Think of static code analysis tools as an additional compiler that is run before the final compilation into the system language.</p><h3>Benefits</h3><ul><li>Helps detect potential bugs that even unit or manual testing might have missed.</li><li>Defines project-specific rules. For example, static analysis as part of the build chain helps newcomers get up to speed with the code standards of their new team.</li><li>Helps you improve your knowledge of a new language.</li><li>Scans your whole project, including files that you might not have ever read.</li></ul><p>On <em>Android</em> the most popular code analysis tools are:</p><ul><li>Android Lint</li><li>Checkstyle</li><li>Findbugs</li></ul><p>Before starting the android integration, it would be great if a gist of what every tool does, should be provided. So, here it goes.</p><h3>Android Lint</h3><ol><li>This one comes with Android Studio by default.</li><li>It checks your Android project source files for potential bugs and optimizations for correctness, security, performance, usability, accessibility, and internationalization.</li></ol><h3>FindBugs</h3><ol><li>It analyses Java byte code mainly .classes to find any design flaw and potential bugs.</li><li>It needs compiled code to work around and will eventually be fast since it works on byte code level.</li><li>The major categories in this tool are: Correctness, Bad practice, Dodgy code, Multithreaded Correctness, Performance Malicious, Code Vulnerability, Security Experimental and Internationalization</li></ol><h3><strong>Checkstyle</strong></h3><ol><li>It basically analyses source code and looks to improve the coding standard by traversing over simple AST generated by Checkstyle.</li><li>It verifies the source code for coding conventions like headers, imports, whitespaces, formatting etc.</li></ol><p><strong>How to set up?</strong></p><p>All the code analysis tools we’ll learn about in this tutorial are available as Gradle plugins, so we can create individual Gradle tasks for each of them. Let’s use a single Gradle file that will include them all. But before that, let’s create a folder that will contain all of our files for the static code analysis.</p><p>Open Android Studio and inside the app module (in <strong>Project </strong>view), create a new folder and name it <strong>code_quality_tools</strong>. This folder will contain the XML files for the code analysis tools, and it will also have a Gradle file, <strong>quality.gradle</strong>, which will run our static analysis tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/0*aw1oiZhHjDEhj-uQ" /></figure><p>Finally, visit your <strong>build.gradle</strong> in the app module folder and include this line at the end of the file:</p><blockquote><strong><em>apply from: ‘/code_quality_tools/quality.gradle’</em></strong></blockquote><p>Here, our <strong>quality.gradle</strong> Gradle script is<a href="https://docs.gradle.org/current/userguide/plugins.html#sec:using_plugins"> being applied</a> with a reference to its local file location.</p><h3>Android Lint</h3><p>To configure Lint, you have to include the lintOptions {} block in your module-level <strong>build.gradle</strong> file:</p><blockquote><em>lintOptions {</em></blockquote><blockquote><em>abortOnError false</em></blockquote><blockquote><em>quiet true</em></blockquote><blockquote><em>lintConfig file(‘./code_quality_tools/lint.xml’)</em></blockquote><blockquote><em>}</em></blockquote><p>The key<a href="https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.internal.dsl.LintOptions.html"> Lint options</a> we are concerned with are:</p><ul><li><strong>abortOnError</strong>: whether lint should set the exit code of the process if errors are found.</li><li><strong>quiet</strong>: whether to turn off analysis progress reporting.</li><li><strong>lintConfig</strong>: the default configuration file to use.</li></ul><p>Your <strong>lint.xml</strong> file can include issues you want Lint to ignore or modify, such as the example below:</p><blockquote><em>&lt;?xml version=”1.0&quot; encoding=”UTF-8&quot;?&gt;</em></blockquote><blockquote><em>&lt;lint&gt;</em></blockquote><blockquote><em>&lt;! — Disable the given check in this project →</em></blockquote><blockquote><em>&lt;issue id=”IconMissingDensityFolder” severity=”ignore” /&gt;</em></blockquote><blockquote><em>&lt;! — Change the severity of hardcoded strings to “error” →</em></blockquote><blockquote><em>&lt;issue id=”HardcodedText” severity=”error” /&gt;</em></blockquote><blockquote><em>&lt;/lint&gt;</em></blockquote><p>You can run Lint manually from Android Studio by clicking on the <strong>Analyze </strong>menu, choosing <strong>Inspect Code…</strong> (the inspection scope is the whole project), and then clicking on the <strong>OK</strong> button to proceed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/0*_k8fy4TAgXpusjfA" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/0*pelEC-Dnrfg5gCVD" /></figure><p>You can also run Lint by visiting the Gradle tool window, opening the <strong>verification</strong> group, and then clicking on <strong>lint</strong>. Finally, you can run it via the command line.</p><blockquote><em>On Windows:</em></blockquote><blockquote><em>gradlew lint</em></blockquote><blockquote><em>On Linux or Mac:</em></blockquote><blockquote><em>./gradlew lint</em></blockquote><p>A report will also be generated when the task has finished executing, which is available at <strong>app module &gt; build &gt; outputs &gt; lint-results.html</strong>.</p><h3>Checkstyle</h3><p>Given rules you specify in an XML file to enforce a coding standard for your project, Checkstyle enforces those rules by analysing your source code and compares them against known coding standards or conventions.</p><p>Checkstyle is an open-source tool that is actively maintained by the community. This means you can create your own custom checks or modify existing ones to suit your needs. For example, Checkstyle can run a check on the constant names (final, static, or both) in your classes. If your constant names do not stick to a rule of being in uppercase with words separated by an underscore, the problem will be flagged in the final report.</p><blockquote>// incorrect</blockquote><blockquote>private final static String myConstant = “myConstant”;</blockquote><blockquote>// correct</blockquote><blockquote>private final static String MY_CONSTANT = “myConstant”;</blockquote><h4>Integrating Checkstyle</h4><p>I’ll show you how to integrate Checkstyle into our Android Studio project and demonstrate a practical example.</p><p>First, we need to create our coding rules. Inside <strong>checkstyle.xml</strong>, we create some Checkstyle configuration rules that will be run against our code.</p><blockquote>&lt;?xml version=”1.0&quot;?&gt;</blockquote><blockquote>&lt;module name=”Checker”&gt;</blockquote><blockquote>&lt;! — Checks for Naming Conventions →</blockquote><blockquote>&lt;! — See<a href="http://checkstyle.sourceforge.net/config_naming.html"> http://checkstyle.sourceforge.net/config_naming.html</a> →</blockquote><blockquote>&lt;module name=”MethodName”/&gt;</blockquote><blockquote>&lt;module name=”ConstantName”/&gt;</blockquote><blockquote>&lt;! — Checks for Imports →</blockquote><blockquote>&lt;! — See<a href="http://checkstyle.sourceforge.net/config_imports.html--"> http://checkstyle.sourceforge.net/config_imports.html--</a>&gt;</blockquote><blockquote>&lt;module name=”AvoidStarImport”/&gt;</blockquote><blockquote>&lt;module name=”UnusedImports”/&gt;</blockquote><blockquote>&lt;! — Checks for Size →</blockquote><blockquote>&lt;! — See<a href="http://checkstyle.sourceforge.net/config_sizes%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0"> http://checkstyle.sourceforge.net/config_sizes </a>→</blockquote><blockquote>&lt;module name=”ParameterNumber”&gt;</blockquote><blockquote>&lt;property name=”max” value=”6&quot;/&gt;</blockquote><blockquote>&lt;/module&gt;</blockquote><blockquote>&lt;! — other rules ignored for brevity →</blockquote><blockquote>&lt;/module&gt;</blockquote><p>In the above code, we include the rules or checks we want Checkstyle to validate in our source code. One rule is <a href="http://checkstyle.sourceforge.net/config_imports.html#AvoidStarImport">AvoidStarImport</a> which, as the name says, checks if your source code included an import statement like java.util.*. (Instead, you should explicitly specify the package to import, e.g. java.util.Observable.)</p><p>Take a look at some of the<a href="http://checkstyle.sourceforge.net/checks.html"> other checks on the Checkstyle website</a>.</p><p>To run this check, we need to<a href="https://docs.gradle.org/current/userguide/tutorial_using_tasks.html"> create a Gradle task</a>. So visit the <strong>quality.gradle </strong>file and create a task called checkstyle:</p><blockquote>apply plugin: ‘checkstyle’</blockquote><blockquote>task checkstyle(type: Checkstyle) {</blockquote><blockquote>description ‘Check code standard’</blockquote><blockquote>group ‘verification’</blockquote><blockquote>configFile file(‘./code_quality_tools/checkstyle.xml’)</blockquote><blockquote>source ‘src’</blockquote><blockquote>include ‘**/*.java’</blockquote><blockquote>exclude ‘**/gen/**’</blockquote><blockquote>classpath = files()</blockquote><blockquote>ignoreFailures = false</blockquote><blockquote>}</blockquote><p>Notice that in the code above, we first applied the<a href="https://docs.gradle.org/current/userguide/checkstyle_plugin.html"> Checkstyle Gradle plugin</a>. We gave it a description and added it to an already predefined Gradle group called verification.</p><p>The key<a href="https://docs.gradle.org/3.5/dsl/org.gradle.api.plugins.quality.Checkstyle.html"> properties of the Checkstyle Gradle task</a> we are concerned with are:</p><ul><li><strong>configFile</strong>: the Checkstyle configuration file to use.</li><li><strong>IgnoreFailures</strong>: whether or not to allow the build to continue if there are warnings.</li><li><strong>include</strong>: the set of include patterns.</li><li><strong>exclude</strong>: the set of exclude patterns. In this case, we don’t scan generated classes.</li></ul><p>Finally, you can run the Gradle script by visiting the Gradle tool window on Android Studio, opening the <strong>verification</strong> group, and then clicking on <strong>checkstyle</strong> to run the task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/0*QSxyU4HuJwELmekz" /></figure><p>You can use command line to execute checkstyle task</p><blockquote>gradle checkstyle</blockquote><p>After the task has finished running, a report will be generated, which is available at <strong>app module &gt; build &gt; reports &gt; checkstyle</strong>. You can open <strong>checkstyle.html</strong> to view the report.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/0*gNMZmbkK_XF-YAdF" /></figure><p>A <a href="https://plugins.jetbrains.com/plugin/1065-checkstyle-idea">Checkstyle plugin</a> is freely available for Android Studio or IntelliJ IDEA. It offers real-time scanning of your Java files.</p><h3>FindBugs</h3><p><a href="http://findbugs.sourceforge.net/">FindBugs</a> is another free static analysis tool which analyses your class looking for potential problems by checking your bytecodes against a<a href="http://findbugs.sourceforge.net/bugDescriptions.html"> known list of bug patterns</a>. Some of them are:</p><ul><li><strong>Class defines hashCode() but not equals()</strong>: A class implements the hashCode() method but not equals() — therefore two instances might be equal but not have the same hash codes. This falls under the bad practice category.</li><li><strong>Bad comparison of int value with long constant</strong>: The code is comparing an int value with a long constant that is outside the range of values that can be represented as an int value. This comparison is vacuous and possibly will yield an unexpected result. This falls under the correctness category.</li></ul><p>FindBugs is an open-source project, so you can view, contribute or monitor the progress of the source code on<a href="https://github.com/findbugsproject/findbugs"> GitHub</a>.</p><p>In the <strong>findbugs-exclude.xml</strong> file, we want to prevent FindBugs from scanning some classes (using regular expressions) in our projects, such as auto-generated resource classes and auto-generated manifest classes. Also, if you use Dagger, we want FindBugs not to check the generated Dagger classes. We can also tell FindBugs to ignore some rules if we want.</p><blockquote><em>&lt;FindBugsFilter&gt;</em></blockquote><blockquote><em>&lt;! — Do not check auto-generated resources classes →</em></blockquote><blockquote><em>&lt;Match&gt;</em></blockquote><blockquote><em>&lt;Class name=”~.*R\$.*”/&gt;</em></blockquote><blockquote><em>&lt;/Match&gt;</em></blockquote><blockquote><em>&lt;! — Do not check auto-generated manifest classes →</em></blockquote><blockquote><em>&lt;Match&gt;</em></blockquote><blockquote><em>&lt;Class name=”~.*Manifest\$.*”/&gt;</em></blockquote><blockquote><em>&lt;/Match&gt;</em></blockquote><blockquote><em>&lt;! — Do not check auto-generated classes (Dagger puts $ into class names) →</em></blockquote><blockquote><em>&lt;Match&gt;</em></blockquote><blockquote><em>&lt;Class name=”~.*Dagger*.*”/&gt;</em></blockquote><blockquote><em>&lt;/Match&gt;</em></blockquote><blockquote><em>&lt;! —</em><a href="http://findbugs.sourceforge.net/bugDescriptions.html#ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD--"><em> http://findbugs.sourceforge.net/bugDescriptions.html#ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD--</em></a><em>&gt;</em></blockquote><blockquote><em>&lt;Match&gt;</em></blockquote><blockquote><em>&lt;Bug pattern=”ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD” /&gt;</em></blockquote><blockquote><em>&lt;/Match&gt;</em></blockquote><blockquote><em>&lt;/FindBugsFilter&gt;</em></blockquote><p>And finally, we’ll include the <strong>findbugs</strong> task in <strong>quality.gradle</strong>:</p><blockquote>apply plugin: ‘findbugs’</blockquote><blockquote>task findbugs(type: FindBugs) {</blockquote><blockquote>description ‘Run findbugs’</blockquote><blockquote>group ‘verification’</blockquote><blockquote>classes = files(“$project.buildDir/intermediates/classes”)</blockquote><blockquote>source ‘src’</blockquote><blockquote>classpath = files()</blockquote><blockquote>effort ‘max’</blockquote><blockquote>reportLevel = “high”</blockquote><blockquote>excludeFilter file(‘./code_quality_tools/findbugs-exclude.xml’)</blockquote><blockquote>reports {</blockquote><blockquote>xml.enabled = false</blockquote><blockquote>html.enabled = true</blockquote><blockquote>}</blockquote><blockquote>ignoreFailures = false</blockquote><blockquote>}</blockquote><p>In the first line above, we applied FindBugs as a<a href="https://docs.gradle.org/current/userguide/findbugs_plugin.html"> Gradle Plugin</a> and then created a task called findbugs. The key properties of the findbugs task we are really concerned with are:</p><ul><li><strong>classes</strong>: the classes to be analyzed.</li><li><strong>effort</strong>: the analysis effort level. The value specified should be one of min, default, or max. Be aware that higher levels increase precision and find more bugs at the cost of running time and memory consumption.</li><li><strong>reportLevel</strong>: the priority threshold for reporting bugs. If set to low, all bugs are reported. If set to medium (the default), medium and high priority bugs are reported. If set to high, only high priority bugs are reported.</li><li><strong>excludeFilter</strong>: the filename of a filter specifying bugs to exclude from being reported, which we have created already.</li></ul><p>You can then run the Gradle script by visiting the Gradle tool window, opening the verification group folder, and then clicking on <strong>findbugs</strong> to run the task. Or launch it from the command line:</p><blockquote>gradle findbugs</blockquote><p>A report will also be generated when the task has finished executing. This will be available at <strong>app module &gt; build &gt; reports &gt; findbugs</strong>. The <a href="https://plugins.jetbrains.com/plugin/3847-findbugs-idea">FindBugs plugin</a> is another freely available plugin for download and integration with either IntelliJ IDEA or Android Studio.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80558d9cb6cb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/achieve-high-quality-android-code-with-static-code/static-code-analysis-tools-80558d9cb6cb">Static Code Analysis Tools</a> was originally published in <a href="https://medium.com/achieve-high-quality-android-code-with-static-code">Achieve High-Quality Android Code With Static Code Analysis Tools stories</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>