Skip to content
AgileLogics
5 min read

Packages, Modules, and Code Style Tools

Prerequisites: Gradle basics, Visibility modifiers

As a project grows, a clear structure and automatic style checks keep it manageable.

Packages

Every Kotlin file can declare a package — a namespace that keeps names from clashing:

// src/main/kotlin/com/example/shop/cart/Cart.kt
package com.example.shop.cart

class Cart
// src/main/kotlin/com/example/shop/App.kt
package com.example.shop

import com.example.shop.cart.Cart

fun main() {
    val cart = Cart()
}

Package names are lowercase, usually starting with a reversed domain you control (com.example). Kotlin doesn't require the folder structure to match the package, but matching it is the convention.

Organize by feature

Group code by feature rather than by kind: cart/, checkout/, and profile/ packages — each with its own models, logic, and UI — scale better than one giant models/ package and one giant services/ package. It's fine to put several small related classes (for example, a sealed hierarchy) in one file.

Modules

Larger projects split into several Gradle modules — for example :core, :data, :feature-cart, and :app. Each builds and tests on its own, internal visibility hides code between modules, and Gradle can rebuild only what changed. Declare them in settings.gradle.kts with include(":core", ":app").

Formatting: ktlint

ktlint checks and fixes formatting according to the official Kotlin style:

ktlint --format "src/**/*.kt"

Static analysis: detekt

detekt finds code smells and likely bugs — overly complex functions, unused code, swallowed exceptions:

src/main/kotlin/com/example/shop/Cart.kt:14:9: TooGenericExceptionCaught: The caught exception is too generic. Prefer catching specific exceptions to the case that is currently handled.

Both run as Gradle tasks, in IntelliJ, and in CI, so style never has to be debated in code review.

Common mistakes

  • Huge files and classes mixing UI, logic, and data access. Split by responsibility early; it's much harder later.
  • Disabling every detekt rule that complains. Read the explanation first — many point to real problems.