This article is a part of series: Configuring Android Project Little Things That Matter Version Name & Code Static Code Analysis Tools Continuous Integration Everything we have discussed in current article is available in project. template Static Code Analysis Tools 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 the most popular code analysis tools are: Android Lint PMD Findbugs I usually keep static code analysis scripts and related file in . separate folder Lint The lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. Config To add to your android project create file. lint script-lint.gradle <a href="https://medium.com/media/e1c1a139f832df2c153e1e8545138ce5/href">https://medium.com/media/e1c1a139f832df2c153e1e8545138ce5/href</a> Important options: lint — path to lint rulesets file where you can suppress issues. lintConfig — path where html report will be generated. htmlOutput Import to your file. script-lint.gradle build.gradle <a href="https://medium.com/media/16aa0e976adcf7c7749861fc8ff5c035/href">https://medium.com/media/16aa0e976adcf7c7749861fc8ff5c035/href</a> Test Rebuild your project and then run with command. In case it finds some issues you will see similar output. lint ./gradlew lint ./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 report file you will see list of issues with description and advices how to fix them. lint.html In case you want to ignore this issues add following rule to file. rules-lint.xml <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 . Findbugs Static code analysis tool that analyses Java bytecode and detects a wide range of problems. Config To add to your android project create file. findbugs script-findbugs.gradle <a href="https://medium.com/media/66e837890a9d6b6124c1e0a27664aa1a/href">https://medium.com/media/66e837890a9d6b6124c1e0a27664aa1a/href</a> Important options: findbugs — path to rulesets file where you can suppress issues. excludeFilter findbugs — path to generated classes classes (if you have more then one flavor, path consists of flavor name, in current case ‘dev’). — path to source code. source — path where html report will be generated. html.destination Import to your file. script-findbugs.gradle build.gradle <a href="https://medium.com/media/150d839a15607039ff56c49a48969eb3/href">https://medium.com/media/150d839a15607039ff56c49a48969eb3/href</a> Test 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 with command. In case it finds some issues you will see similar output. findbugs ./gradlew findbugs >./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 report file you will see list of issues with description and advices how to fix them. findbugs.html In case you want to ignore this issues add following rule to file. rules-findbugs.xml <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 PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. Config To add to your android project create file. pmd script-pmd.gradle <a href="https://medium.com/media/eeebcff2d66f7f242a2997bfd3c2f062/href">https://medium.com/media/eeebcff2d66f7f242a2997bfd3c2f062/href</a> Important options: pmd — path to rulesets file where you can suppress issues and define which issues to track. ruleSetFiles pmd — path to source code. source — path where html report will be generated. html.destination Import to your file. script-pmd.gradle build.gradle <a href="https://medium.com/media/96f7bed2efff2bf44e6a48d857ddbcb5/href">https://medium.com/media/96f7bed2efff2bf44e6a48d857ddbcb5/href</a> Test 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 with command. In case it finds some issues you will see similar output. pmd ./gradlew pmd >./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 report file you will see list of issues with description and advices how to fix them. pmd.html In case you want to ignore this issues add following rule to file. rules-pmd.xml <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 .
Share Your Thoughts