Organizing a Jetpack Compose project helps make it easier to read, maintain, and expand. Since Compose is a declarative UI framework, its structure is slightly different from traditional Android projects.
Here's a simplified guide with examples:
com.example.android
├── 📂 app
│ ├── 📂 android
│ └── 📂 host
│
├── 📂 data
│ ├── 📂 network
│ │ ├── 📂 service
│ │ ├── 📂 client
│ │ ├── 📂 model
│ │ └── 📂 error
│ │
│ ├── 📂 db
│ │ ├── 📂 entity
│ │ ├── 📂 dao
│ │ ├── 📂 database
│ │ ├── 📂 converter
│ │ └── 📂 migration
│ │
│ ├── 📂 pref
│ │
│ └── 📂 repository
│
├── 📂 domain
│ ├── 📂 model
│ └── 📂 repository
│
├── 📂 di
│ ├── 📂 modules
│ ├── 📂 scopes
│ └── 📂 components
│
├── 📂 presentation
│ ├── 📂 ui
│ │ ├── 📂 screen1
│ │ │ ├── 📄 Screen1.kt
│ │ │ ├── 📄 ViewModel1.kt
│ │ │ └── 📂 components
│ │ │ ├── 📄 Component1.kt
│ │ │ └── 📄 Component2.kt
│ │ │
│ │ └── 📂 screen2
│ │ ├── 📄 Screen2.kt
│ │ ├── 📄 ViewModel2.kt
│ │ └── 📂 components
│ │ ├── 📄 Component1.kt
│ │ └── 📄 Component2.kt
│ │
│ ├── 📂 common
│ │ ├── 📂 shared components
│ │ └── 📂 shared viewmodels
│ │
│ └── 📂 theme
│ │ ├── 📄 color
│ │ ├── 📄 shape
│ │ ├── 📄 theme
│ │ └── 📄 type
│ │
│ └── 📂 navigation
│
├── 📂 notification
├── 📂 workers
└── 📂 utils
Responsible for the main entry point and setup for the application.
Example:
app/android
→ Contains theApplication
class and custom components.app/host
→ Contains theMainActivity
or main entry point of the app.
Handles all data operations (API calls, database, preferences).
-
data/network
→ Manages API communication.service/
→ Contains API interfaces (e.g., Retrofit services).client/
→ Sets up network clients (like Retrofit).model/
→ Holds data models for API responses.error/
→ Handles API errors (e.g., custom exceptions).
Example:
data/network/service/WeatherService.kt
→ Defines weather API calls.
-
data/db
→ Manages local database with Room.entity/
→ Defines database tables (Room entities).dao/
→ Contains DAO interfaces for database operations.database/
→ Sets up the Room database class.converter/
→ Handles type conversions for custom data types.migration/
→ Manages database schema updates.
Example:
data/db/entity/User.kt
→ Defines user table.
-
data/pref
→ Manages key-value storage (e.g., SharedPreferences).Example:
data/pref/UserPreferences.kt
→ Handles user preferences.
-
data/repository
→ Acts as the single source of truth for data from APIs, DB, or preferences.Example:
data/repository/UserRepository.kt
→ Combines user data from API and local DB.
Contains business logic and domain-specific data.
domain/model
→ Holds core business models.domain/repository
→ Defines repository interfaces.
Example:
domain/model/User.kt
→ Core user model for the business logic.
Sets up dependency injection using Hilt or Dagger.
di/modules
→ Contains modules for providing dependencies.di/scopes
→ Manages custom scopes if needed.di/components
→ Sets up DI components.
Example:
di/modules/NetworkModule.kt
→ Provides network-related dependencies like Retrofit.
Manages the UI and related logic using Jetpack Compose.
-
presentation/ui
→ Organized by screens or features.screen1/
→ Contains the UI and ViewModel for the first screen.Screen1.kt
→ The UI for Screen 1.ViewModel1.kt
→ Manages Screen 1's logic.components/
→ Reusable UI components for Screen 1.
Example:
presentation/ui/screen1/Screen1.kt
→ Compose UI for Screen 1.presentation/ui/screen1/ViewModel1.kt
→ Logic for Screen 1.
-
presentation/common
→ Shared UI components and ViewModels across multiple screens.shared components/
→ Reusable UI components for any screen.shared viewmodels/
→ Common ViewModels for multiple screens.
Example:
presentation/common/sharedcomponents/Button.kt
→ Custom button used in multiple screens.
-
presentation/theme
→ Manages Compose themes (e.g., colors, shapes, typography).Example:
presentation/theme/Color.kt
→ Defines app colors.
presentation/navigation
→ Manages navigation between screens using Jetpack Navigation.
Manages app notifications.
Example:
notification/NotificationHelper.kt
→ Handles push notifications.
Contains background tasks using WorkManager.
Example:
workers/SyncWorker.kt
→ Syncs data in the background.
Holds utility functions and helper classes.
Example:
utils/DateUtils.kt
→ Common functions for date formatting.