This Android Architecture with Kotlin tutorial explains the MVVM pattern and modern Jetpack components such as ViewModel, LiveData, Room database, Retrofit, and Coroutines. It focuses on clean architecture, separation of concerns, and scalable Android app development using Kotlin, with real-world examples and end-to-end projects.
Android Architecture with Kotlin (Complete Tutorial)
MVVM Pattern
MVVM stands for Model, View, and ViewModel.
Responsibilities
- Model: Data layer (API, database)
- View: UI (Activity/Fragment)
- ViewModel: Business logic and state
Benefits
- Clear separation of concerns
- Easy testing
- Lifecycle awareness
ViewModel
ViewModel stores and manages UI-related data.
Example
class NotesViewModel : ViewModel() {
private val _notes = MutableLiveData<List<String>>()
val notes: LiveData<List<String>> = _notes
fun loadNotes() {
_notes.value = listOf("Note 1", "Note 2")
}
}
Best Practices
- Never reference Views in ViewModel
- Use ViewModel for state management
- Keep business logic out of Activities
LiveData
LiveData is lifecycle-aware observable data holder.
Example
viewModel.notes.observe(viewLifecycleOwner) { notes ->
println(notes)
}
Best Practices
- Observe LiveData in Fragments
- Avoid mutable LiveData exposure
- Use StateFlow for complex state
Room Database
Room provides an abstraction layer over SQLite.
Components
- Entity
- DAO
- Database
Entity Example
@Entity
data class Note(
@PrimaryKey(autoGenerate = true) val id: Int,
val text: String
)
DAO Example
@Dao
interface NoteDao {
@Query("SELECT * FROM Note")
fun getAllNotes(): LiveData<List<Note>>
@Insert
suspend fun insert(note: Note)
}
Best Practices
- Use suspend functions in DAO
- Avoid database access on Main thread
- Use migrations properly
Retrofit
Retrofit is used for API communication.
Example
interface ApiService {
@GET("notes")
suspend fun getNotes(): List<Note>
}
Retrofit Setup
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
Best Practices
- Use suspend functions
- Handle errors with sealed classes
- Use interceptors for logging
Coroutines Integration
Coroutines handle background tasks efficiently.
Repository Example
class NotesRepository(
private val api: ApiService,
private val dao: NoteDao
) {
suspend fun fetchNotes() = api.getNotes()
}
ViewModel Coroutine
viewModelScope.launch {
repository.fetchNotes()
}
Best Practices
- Use
viewModelScope - Switch dispatchers appropriately
- Handle exceptions gracefully
Android Projects
Notes Application
- MVVM architecture
- Room for offline storage
- LiveData / StateFlow for UI updates
Expense Tracker
- CRUD operations
- Charts and analytics
- Local database with Room
Weather Application
- API integration with Retrofit
- Coroutines and Flow
- Location-based weather updates
News Application
- Pagination
- Offline caching
- Search and filters
Chapter Summary
This chapter covered Android architecture using Kotlin, including MVVM, ViewModel, LiveData, Room, Retrofit, and Coroutines. These tools form the backbone of modern, scalable, and maintainable Android applications.