Modular Android Project Template based on Kotlin with Static Analysis and CI set up.

If you are reading this article, either you’re an Android App Developer or want to be one, either way, you must be familiar with the pain of the time-consuming setup of a new project, which includes, but is not limited to, tasks such as buildSrc setup, Code Formatter, Static Analysis, CI/CD, and what not?

Of course, I’m no magician or a telepath, I know all this because due to the very same reason many of my (Billion Dollar 😜) app ideas have died in the past. So, to stop this cycle once and for all, I thought of creating a Project Template that I can use as a starting point for my new projects which will save me a tremendous amount of redundant work and time so that I can focus on what really matters, my App.

Fortunately, I’ve had some spare time with me lately. and guess what?! I finished the template. 🎉

Here is my Android Project template if you wanna take a look:

Now, let’s see what all this template is capable of doing.

1. Gradle setup: buildSrc

To make use of Kotlin, I’ve used Gradle’s Kotlin DSL to write build files. This is very useful because:

a) It provides autocompletion in build files.

b) It eliminates groovy, making use of only Kotlin.

To manage dependencies, I’m using buildSrc. This is to facilitate centralized dependencies with auto-completion. For example, Dependencies.kt contains the dependencies that can be used by any module in the project.

To keep the libraries up to date, I’m using the gradle-versions-plugin. You can generate a library update report by running the following command in your terminal:

./gradlew dependencyUpdates

Also, I’ve used Gradle’s Plugin DSL to apply Gradle plugins. For instance, to apply the android library plugin, all I need to do is:

plugins {
id("com.android.library")
}

Moreover, to avoid further repetition of writing build files whenever a new module is created, I’ve written common build files for the Android library, Kotlin library, and Android Dynamic feature, which can be applied to the build file of the new module and it’ll work right out of the box. For instance, for the android library module, all I need to do is:

plugins {
id("commons.android-library")
}

2. Static Analysis: Ktlint, detekt

Nobody wants to make silly mistakes such as irregular indentations, blank lines, or unnecessary or wildcard imports, etc in their code. Not only it compromises the code quality but also looks ugly. Thankfully, we don't have to do it manually every time we make changes to our code. We can make use of tools available for such tasks. I’m using ktlint and detekt for the same.

  • KtLint: Analyzer that helps in spotting Android and Kotlin related code irregularities. eg. Unnecessary imports, wrong indentations, irregular spacing, etc.
  • Detekt: A Kotlin specific analyzer that can help find potential errors or anti-patterns in the Kotlin code. eg. uncommented class or method, magic numbers, etc.

Once set up,

ktlint can be invoked using:

./gradlew ktlintFormat

detekt can be invoked using:

./gradlew detekt

and that should be all to format your codebase.

3. Code Formatting: spotless

Additional to code formatting, let’s consider a scenario where you had a specific piece of code that you want to replace with an updated version. For instance, a license header. Instead of updating every file manually or missing a bunch of them in hurry, we can make use of the spotless plugin. I’ve setup spotless to update the license header across the codebase.

License header is located at .spotless/copyright.kt

Run ./gradlew spotlessCheck to check if the copyright is up to date.

Run ./gradlew spotlessApply to update the copyright across the project.

4. Git Hooks

Git hook is a shell script containing a series of tasks to be performed before making a commit. This ensures that the code adheres to the coding rules defined in the project. To install git hooks run the following command:

./gradlew installGitHooks

Git Hook shell script is located at scripts/git-hooks/pre-commit.sh

5. CI: Github Actions

A project must have a Continuous Integration setup, that ensures that the coding style is consistent and passes all the necessary checks on every commit.

This template is using Github Actions to automate the build process. Following are the sample workflows added to this repository:

  • Build To build the project whenever there is a new commit pushed to any branch.
  • Deploy App To run tests, build, generate bundle, sign it with Signing Key whenever a Pull Request is merged into the master Branch.

6. Documentation: dokka

Documentation is really important for a project. If you write comments when writing code, you can leverage that information to generate documentation using dokka.

To generate documentation in HTML format, run the following command:

./gradlew dokkaHtml

7. Conclusion: Maintainance

Although this template serves as a good starting point for a new project, I plan to add boilerplate code to it so that I can save time by not writing the code for common use cases such as a Network call, Room DB setup, etc.

Feel free to give this template a try. Let me know if you found this article helpful.

Your feedback and suggestions are welcome.

Get in touch @ Twitter/Vishal1337

Android Developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store