Skip to the content.

Architecture Guide — init-android-apps

1. Overview & Architecture Principles

init-android-apps follows Google’s Official Guide to App Architecture and Now in Android conventions in a single-module (:app) Clean Architecture layout partitioned into four top-level layers (core/, data/, domain/, feature/) backed by a Gradle composite build (build-logic/convention) for shared convention plugins.


2. Canonical Package Tree

Main Source Tree (app/src/main/java/io/github/amirisback/androidapp/)

io.github.amirisback.androidapp/
|-- ProjectApplication.kt
|-- core/
|   |-- common/
|   |   |-- BuildConfigExt.kt
|   |   |-- JsonExt.kt
|   |   |-- Resource.kt
|   |-- di/
|   |   |-- DatabaseModule.kt
|   |   |-- DispatcherModule.kt
|   |   |-- GameModule.kt
|   |   |-- NetworkModule.kt
|   |   |-- RepositoryModule.kt
|   |   |-- UseCaseModule.kt
|   |-- game/
|   |   |-- ads/
|   |   |   |-- AdMobRewardedAdController.kt
|   |   |   |-- RewardedAdController.kt
|   |   |   |-- RewardedAdState.kt
|   |   |-- audio/
|   |   |   |-- AndroidGameAudioManager.kt
|   |   |   |-- GameAudioController.kt
|   |   |-- loop/
|   |   |   |-- FixedTimestepClock.kt
|   |   |   |-- GameLoopController.kt
|   |   |-- render/
|   |   |   |-- GameCanvas.kt
|   |   |   |-- GameSurfaceView.kt
|   |   |-- save/
|   |   |   |-- DataStoreGameSaveStore.kt
|   |   |   |-- GameSaveSnapshot.kt
|   |   |   |-- GameSaveStore.kt
|   |   |-- window/
|   |       |-- ImmersiveModeEffect.kt
|   |-- ui/
|       |-- component/
|       |   |-- AppTopAppBar.kt
|       |   |-- CommonState.kt
|       |   |-- MealCard.kt
|       |-- theme/
|           |-- Color.kt
|           |-- Theme.kt
|           |-- Type.kt
|-- data/
|   |-- local/
|   |   |-- dao/
|   |   |   |-- MealDao.kt
|   |   |-- database/
|   |   |   |-- AppDatabase.kt
|   |   |   |-- DatabaseConfig.kt
|   |   |-- entity/
|   |   |   |-- MealEntity.kt
|   |   |-- MealLocalDataSource.kt
|   |-- mapper/
|   |   |-- MealMapper.kt
|   |-- remote/
|   |   |-- api/
|   |   |   |-- MealApiConfig.kt
|   |   |   |-- MealApiService.kt
|   |   |-- dto/
|   |   |   |-- AreaDto.kt
|   |   |   |-- CategoryDto.kt
|   |   |   |-- CategoryResponseDto.kt
|   |   |   |-- IngredientDto.kt
|   |   |   |-- MealDto.kt
|   |   |   |-- MealFilterDto.kt
|   |   |   |-- MealResponseDto.kt
|   |   |-- MealRemoteDataSource.kt
|   |-- repository/
|       |-- MealRepositoryImpl.kt
|-- domain/
|   |-- model/
|   |   |-- Area.kt
|   |   |-- Category.kt
|   |   |-- Ingredient.kt
|   |   |-- Meal.kt
|   |   |-- MealFilter.kt
|   |-- repository/
|   |   |-- MealRepository.kt
|   |-- usecase/
|       |-- MealInteractor.kt
|       |-- MealUseCase.kt
|-- feature/
    |-- about/
    |   |-- AboutUsActivity.kt
    |   |-- AboutUsScreen.kt
    |-- detail/
    |   |-- DetailActivity.kt
    |   |-- DetailScreen.kt
    |   |-- DetailUiState.kt
    |   |-- DetailViewModel.kt
    |-- favorite/
    |   |-- FavoriteScreen.kt
    |   |-- FavoriteUiState.kt
    |   |-- FavoriteViewModel.kt
    |-- game/
    |   |-- GameActivity.kt
    |   |-- GameScreen.kt
    |   |-- GameUiState.kt
    |   |-- GameViewModel.kt
    |-- main/
        |-- MainActivity.kt
        |-- MainScreen.kt
        |-- MainUiState.kt
        |-- MainViewModel.kt

Unit Test Source Tree (app/src/test/java/io/github/amirisback/androidapp/)

io.github.amirisback.androidapp/
|-- core/
|   |-- common/
|   |   |-- JsonExtTest.kt
|   |-- game/
|       |-- loop/
|       |   |-- FixedTimestepClockTest.kt
|       |   |-- GameLoopControllerTest.kt
|       |-- save/
|           |-- DataStoreGameSaveStoreTest.kt
|-- data/
|   |-- mapper/
|   |   |-- MealMapperTest.kt
|   |-- repository/
|       |-- MealRepositoryImplTest.kt
|-- feature/
|   |-- detail/
|   |   |-- DetailViewModelTest.kt
|   |-- favorite/
|   |   |-- FavoriteViewModelTest.kt
|   |-- game/
|   |   |-- GameViewModelTest.kt
|   |-- main/
|       |-- MainViewModelTest.kt
|-- testutil/
    |-- FakeGameAudioController.kt
    |-- FakeGameSaveStore.kt
    |-- FakeMealApiService.kt
    |-- FakeMealDao.kt
    |-- FakeMealUseCase.kt
    |-- FakeRewardedAdController.kt
    |-- MainDispatcherRule.kt

3. Layer Dependency Diagram & Import Boundaries

flowchart TD
    Feature["feature/* (Activities, Routes, Screens, ViewModels)"]
    CoreUI["core/ui (Theme & Shared Components)"]
    CoreGame["core/game (Opt-in 2D Loop, Canvas/Surface, Audio, Save, Ads, Immersive)"]
    CoreCommon["core/common (Resource, JsonExt, BuildConfigExt)"]
    CoreDI["core/di (Hilt Modules)"]
    Domain["domain/* (Pure Kotlin Models, Repository Contracts, UseCases)"]
    Data["data/* (Room, Retrofit, DTOs, Entities, Mappers, RepoImpl)"]

    Feature --> Domain
    Feature --> CoreUI
    Feature --> CoreGame
    Feature --> CoreCommon
    CoreUI --> Domain
    Data --> Domain
    Data --> CoreCommon
    CoreDI --> Data
    CoreDI --> Domain
    CoreDI --> CoreGame
    CoreDI --> CoreCommon

Import Boundary Matrix

Package Allowed Imports Forbidden Imports
domain/ Kotlin stdlib, kotlinx.coroutines.*, javax.inject.Inject, core.common.Resource android.*, androidx.*, retrofit2.*, com.google.gson.*, okhttp3.*, data.*, feature.*, core.game.*
data/ domain.*, core.common.*, core.di.IoDispatcher, androidx.room.*, retrofit2.*, com.google.gson.*, okhttp3.*, javax.inject.* feature.*, androidx.compose.*
feature/ domain.*, core.ui.*, core.game.*, core.common.*, androidx.compose.*, androidx.lifecycle.*, androidx.activity.*, com.frogobox.*, dagger.hilt.* data.* (never import DAO, Entity, DTO, API Service, or MealRepositoryImpl)
core/game/ Kotlin stdlib, kotlinx.coroutines.*, core.di.IoDispatcher, androidx.compose.*, androidx.lifecycle.*, androidx.datastore.*, com.google.android.gms.ads.*, Android media/window APIs, Hilt data.*, feature.*
core/di/ data.*, domain.*, core.game.*, core.common.*, Hilt, Room, DataStore, Retrofit, OkHttp, Chucker feature.*, androidx.compose.*
core/ui/ androidx.compose.*, coil3.*, domain.model.*, R data.*, feature.*

4. Naming Conventions

Artifact Suffix / Pattern Package
Pure domain entity Plain noun (Meal, Category) domain/model/
Repository contract *Repository domain/repository/
UseCase contract / implementation *UseCase / *Interactor domain/usecase/
Retrofit API interface *ApiService data/remote/api/
API URLs & query constants *ApiConfig data/remote/api/
Network JSON payload *Dto, *ResponseDto data/remote/dto/
Room table *Entity data/local/entity/
Room DAO *Dao (suspend fun queries/mutations) data/local/dao/
Room Database AppDatabase data/local/database/
Data sources *RemoteDataSource, *LocalDataSource data/remote/, data/local/
Model mappers *Mapper.kt (toDomain(), toEntity()) data/mapper/
Repository implementation *RepositoryImpl data/repository/
Feature UI state *UiState feature/<name>/
Feature ViewModel *ViewModel feature/<name>/
Feature Compose screen *Screen.kt (<Name>Route + stateless <Name>Screen + @Preview) feature/<name>/
Activity host *Activity feature/<name>/
Hilt DI module *Module core/di/

5. “Where to Put X” Quick Decision Table

If you need to add or modify… Put it here
A new pure business data class domain/model/<Name>.kt
A repository contract consumed by domain/UI domain/repository/<Name>Repository.kt
Business logic orchestrating one or more repositories domain/usecase/
A new Retrofit HTTP endpoint data/remote/api/<Name>ApiService.kt + URL constants in <Name>ApiConfig.kt
A Gson JSON request/response payload data/remote/dto/<Name>Dto.kt (covered by proguard-rules.pro keep rule)
A new Room table & queries data/local/entity/<Name>Entity.kt, data/local/dao/<Name>Dao.kt, register in AppDatabase.kt
DTO/Entity <-> Domain conversion logic data/mapper/<Name>Mapper.kt
Network/DB error handling & dispatcher switching data/remote/<Name>RemoteDataSource.kt or data/local/<Name>LocalDataSource.kt
A Hilt @Provides or @Binds wiring core/di/<Area>Module.kt
A coroutine dispatcher qualifier (@IoDispatcher, @DefaultDispatcher) core/di/DispatcherModule.kt
A shared Compose widget (e.g., top bar, card, loading/empty/error view) core/ui/component/
Material 3 colors, typography, or InitTheme core/ui/theme/
Reusable 2D game loop, Canvas/SurfaceView renderer, audio, save store, rewarded ads, or immersive mode core/game/ (loop/, render/, audio/, save/, ads/, window/)
Screen-specific state, ViewModel, Route, and stateless Screen feature/<featureName>/
App metadata, version, signing, or AdMob unit IDs build-logic/convention/src/main/kotlin/io/github/amirisback/androidapp/convention/ProjectSetting.kt or ProjectAds.kt
Reusable Gradle convention plugin or build-system configuration build-logic/convention/src/main/kotlin/io/github/amirisback/androidapp/convention/ (registered in build-logic/convention/build.gradle.kts & gradle/libs.versions.toml)
Fast JVM unit tests and test fakes app/src/test/java/io/github/amirisback/androidapp/ (mirroring main packages)

6. Unidirectional Data Flow (UDF) Pattern

  UI Event (onMealClick / onAddFavoriteClick / searchMeal)
        |
        v
  <Feature>ViewModel (extends androidx.lifecycle.ViewModel)
        |  calls MealUseCase -> MealRepository -> DataSources
        |  maps Resource<T> -> immutable *UiState
        v
  StateFlow<*UiState>
        |  collected via collectAsStateWithLifecycle()
        v
  <Feature>Route  --->  stateless <Feature>Screen(uiState, callbacks) + @Preview

7. How to Add a New Feature (Step-by-Step)

  1. Domain Layer: Define immutable Kotlin model(s) in domain/model/<Name>.kt, repository interface in domain/repository/<Name>Repository.kt, and use case contract/interactor in domain/usecase/.
  2. Data Layer: Add Retrofit DTOs in data/remote/dto/, endpoint methods in data/remote/api/, Room @Entity and suspend @Dao in data/local/ (if persisted; bump AppDatabase version and add migration), and bidirectional mappers in data/mapper/.
  3. Data Sources & Repository: Implement *RemoteDataSource / *LocalDataSource injecting @IoDispatcher and rethrowing CancellationException, then implement *RepositoryImpl in data/repository/.
  4. Dependency Injection: Wire providers/bindings in core/di/ (NetworkModule, DatabaseModule, RepositoryModule, UseCaseModule).
  5. Feature UI: Create feature/<name>/ with <Name>UiState.kt, <Name>ViewModel.kt (extending ViewModel), <Name>Screen.kt (<Name>Route using collectAsStateWithLifecycle() + stateless <Name>Screen + @Preview), and <Name>Activity.kt (extending FrogoComposeActivity with enableEdgeToEdge(), only if standalone).
  6. Manifest & Resources: Register any standalone Activity in app/src/main/AndroidManifest.xml with explicit android:exported="false" (unless launcher/deep-link), and place all UI strings/content descriptions in app/src/main/res/values/strings.xml.
  7. Unit Tests: Add JVM unit tests under app/src/test/java/io/github/amirisback/androidapp/ covering mappers, repository behavior, and ViewModel state transitions using hand-written fakes.
  8. Verify: Run ./gradlew assembleDebug testDebugUnitTest lintDebug, confirm zero @Suppress annotations, and commit updated Room schema JSON under app/schemas/ if the database schema changed.

8. Opt-In 2D Game Starter Mode (core/game/ & feature/game/)

This template works out of the box as either a standard Android app starter (MainActivity remains the default launcher) or a 2D casual/puzzle/board game starter (including native Kotlin rewrites of Unity 2D games):

Unity 2D Concept Native Replacement in core/game/
FixedUpdate(fixedDeltaTime) FixedTimestepClock + GameCanvas(onFixedUpdate = ...) or GameSurfaceHost(onFixedUpdate = ...)
Time.timeScale = 0 / pause state GameLoopController (IDLE / RUNNING / PAUSED)
2D Sprite / Canvas rendering GameCanvas (Compose DrawScope) or GameSurfaceHost (SurfaceView + Android Canvas)
PlayerPrefs / local save GameSaveStore (DataStoreGameSaveStore backed by DataStore Preferences)
AudioSource / AudioClip (SFX + BGM) GameAudioController (AndroidGameAudioManager via SoundPool + MediaPlayer) + GameAudioLifecycleEffect
Google Mobile Ads Rewarded RewardedAdController (AdMobRewardedAdController, configured in ProjectAds.kt)
Fullscreen + Screen.sleepTimeout ImmersiveModeEffect(enabled = true, keepScreenOn = true)

Pruning Checklist (Keep Only What You Need)

Both tracks share only ProjectApplication.kt, core/common/, core/di/DispatcherModule.kt, core/ui/theme/, and build-logic/convention/. Neither track imports the other, so either side can be removed cleanly:


9. Testing & Verification Commands

# Compile debug APK & generate KSP / Hilt / Room schema artifacts
./gradlew assembleDebug

# Execute JVM unit tests
./gradlew testDebugUnitTest

# Run Android Lint checks
./gradlew lintDebug