This article discusses the best tools to implement for Git hooks in an Android project, focusing on Ktlint, Detekt, AndroidLint, and Terminal-notifier.
Abstract
The article introduces Git Hooks as programs that can be placed in the .git/hooks/ directory to trigger actions at certain points in Git's execution. The author shares their selection of the best tools to use in hooks, which they use at Shopmium. The tools presented are Ktlint -F (Formatter part), Detekt (Kotlin linter), AndroidLint (Android framework linter), Terminal-notifier (for notifications), and a bonus section on generating Detekt and Android baseline files. The author explains the importance of each tool and provides code samples for their implementation.
Bullet points
Git Hooks are programs that can be placed in the .git/hooks/ directory to trigger actions at certain points in Git's execution.
Ktlint is a tool for formatting Kotlin source code and should be used in the pre-commit stage.
Detekt is a Kotlin linter that should be used in the pre-commit stage and has a baseline system.
AndroidLint is a powerful tool for detecting dead code, bad practices, security issues, and basic memory leaks.
Terminal-notifier is a command-line tool that allows for triggering notifications from the MacOS Notification center.
The article provides code samples for implementing each tool in Git hooks.
The bonus section explains how to generate Detekt and Android baseline files.
4 best tools to implement for your githooks in an Android project.
Git Hooks are programs that you can place in a your .git/hooks/ directory to trigger actions at certain points in Git’s execution. These scripts are the best enablers for increasing the quality of your codebase or your productivity. Here is my selection of the best tools to use in your hooks that we use at Shopmium. You can find also which tools we use on iOS in our hooks here. I will present hooks code sample about :
Ktlint -F (Formatter part)
Detekt : Kotlin linter
AndroidLint : Android framework linter
Terminal-notifier: Get notified after checks failed or succeeded
Bonus: How to generate Detekt and Android baseline files
#1 Ktlint — -F the mandatory one
Ktlint is the #1 tool you should put in your git-hook pre-commit. Ktlint allows you to format Kotlint source code and the available options and configuration make this tool very flexible and powerful, on demand. This tool should NOT be used on the CI side, it’s not the job of the CI at all to format your source code. Incorporating this into Android Studio has several drawbacks.
Slowing down the build just for formatting
Seeing your code moving at each build 👀
Because we use Detekt for linting the Kotlint source code, we only use ktlint -F (autoreformatter part). Sample of pre-commit with Ktlint usage:
#2 Detekt — The second mandatory one
Dealing with a linter is mandatory and no developer should push code smells. Adding Detekt at the pre-commit level acts as a gate, in addition to using it in Android Studio with the awesome Detekt plugin. Moreover Detekt has a baseline system (Swiftlint on iOS doesn’t support baseline yet but this article explains how to workaround).
I don’t use gradle task for it, only Android plugin for background analysis with your baseline and Detekt configuration rules. You will have no overhead on build time / CI side.
To generate the baseline jump at the end of this article ⬇️
Detekt exit status code is != 0 if some warnings are found, here is how pre-commit could looks like :
#3 AndroidLint — Framework linter
Android-lint is a powerful tool that everyproject have to run in order to detect dead code, bad practices, security issues, basic memory leaks, etc...
#4 Terminal-notifier — Take ☕️ and get notified
With all these tools above, pre-commit of pre-push could take time to finish, in addition to your own tools added. Terminal-notifier is a command line that allow to trigger a notification from the Macos Notification center. You will be able to commit, take your ☕️ , read teammates PR and get notified when pre-commit checks finished with success or fail !
Execute script on success or failure when tap on notification or open an app
Thank you for reading this article. If you know more tools that could be awesome to add in a githooks (commit, push or whatever) tell me in comment. If you enjoyed it, you can 👏 me, and if you want to see more content, you can follow me 🚀.
Bonus: How to generate Detekt and Android baseline files
Baseline are use to create a fixed configuration file when you are starting to use a tools with huge legacy (so, with many warnings) to start from a new line, you must generate a baseline.
Wanna contribute? If you know more tools that could be awesome, productive to add in a githook (commit, push or whatever) share it in comment. If you enjoyed the quality of this article, you can 👏 me, and if you want to see more content, you can follow me 🚀.