This article is a part of Configuring Android Project series:
Everything we have discussed in current article is available in template project.
Static code analysis tools — analyze code without executing it. Generally used to find bugs or ensure conformance to coding guidelines. Helps to keep your code healthy and maintain code quality.
On Android the most popular code analysis tools are:
I usually keep static code analysis scripts and related file in separate folder.
The lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization.
To add lint to your android project create script-lint.gradle file.
<a href="https://medium.com/media/e1c1a139f832df2c153e1e8545138ce5/href">https://medium.com/media/e1c1a139f832df2c153e1e8545138ce5/href</a>
Important lint options:
Import script-lint.gradle to your build.gradle file.
<a href="https://medium.com/media/16aa0e976adcf7c7749861fc8ff5c035/href">https://medium.com/media/16aa0e976adcf7c7749861fc8ff5c035/href</a>
Rebuild your project and then run lint with ./gradlew lint command. In case it finds some issues you will see similar output.
./gradlew lint
Execution failed for task ':app:lint.
Lint found errors in the project; aborting build.
Wrote HTML report to: template/app/build/outputs/lint/lint.html
When you open lint.html report file you will see list of issues with description and advices how to fix them.
In case you want to ignore this issues add following rule to rules-lint.xml file.
<a href="https://medium.com/media/619666e58f5a5652f89442c84b2de41c/href">https://medium.com/media/619666e58f5a5652f89442c84b2de41c/href</a>
Note: there are other ways how you can suppress lint warnings. More information about lint is available on official site.
Static code analysis tool that analyses Java bytecode and detects a wide range of problems.
To add findbugs to your android project create script-findbugs.gradle file.
<a href="https://medium.com/media/66e837890a9d6b6124c1e0a27664aa1a/href">https://medium.com/media/66e837890a9d6b6124c1e0a27664aa1a/href</a>
Important findbugs options:
Import script-findbugs.gradle to your build.gradle file.
<a href="https://medium.com/media/150d839a15607039ff56c49a48969eb3/href">https://medium.com/media/150d839a15607039ff56c49a48969eb3/href</a>
For testing purposes we will create following method.
<a href="https://medium.com/media/e64a6d52e65f83728c86a201568def86/href">https://medium.com/media/e64a6d52e65f83728c86a201568def86/href</a>
Rebuild your project and then run findbugs with ./gradlew findbugs command. In case it finds some issues you will see similar output.
>./gradlew findbugs
Execution failed for task ':app:findbugs'.
FindBugs rule violations were found.
See the report at: template/app/build/outputs/findbugs/findbugs.html
When you open findbugs.html report file you will see list of issues with description and advices how to fix them.
In case you want to ignore this issues add following rule to rules-findbugs.xml file.
<a href="https://medium.com/media/8b0789ec964d2c9fd255fbffeedb3d5c/href">https://medium.com/media/8b0789ec964d2c9fd255fbffeedb3d5c/href</a>
Note: there are other ways how you can suppress findbugs warnings. More information about findbugs is available on official site.
PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth.
To add pmd to your android project create script-pmd.gradle file.
<a href="https://medium.com/media/eeebcff2d66f7f242a2997bfd3c2f062/href">https://medium.com/media/eeebcff2d66f7f242a2997bfd3c2f062/href</a>
Important pmd options:
Import script-pmd.gradle to your build.gradle file.
<a href="https://medium.com/media/96f7bed2efff2bf44e6a48d857ddbcb5/href">https://medium.com/media/96f7bed2efff2bf44e6a48d857ddbcb5/href</a>
For testing purposes we will create following method.
<a href="https://medium.com/media/18c8b8e281468c7517007365b77def14/href">https://medium.com/media/18c8b8e281468c7517007365b77def14/href</a>
Rebuild your project and then run pmd with ./gradlew pmd command. In case it finds some issues you will see similar output.
>./gradlew pmd
Execution failed for task ':app:pmd.
7 PMD rule violations were found.
See the report at: template/app/build/outputs/pmd/pmd.html
When you open pmd.html report file you will see list of issues with description and advices how to fix them.
In case you want to ignore this issues add following rule to rules-pmd.xml file.
<a href="https://medium.com/media/e62e195c7b0a0e091f455ace252a7e25/href">https://medium.com/media/e62e195c7b0a0e091f455ace252a7e25/href</a>
Note: there are other ways how you can suppress pmd warnings. More information about pmd is available on official site.