Android Architecture with Kotlin Tutorial: MVVM, LiveData, ViewModel, Room, Retrofit, and Coroutines Integration


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

  1. Model: Data layer (API, database)
  2. View: UI (Activity/Fragment)
  3. ViewModel: Business logic and state

Benefits

  1. Clear separation of concerns
  2. Easy testing
  3. 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

  1. Never reference Views in ViewModel
  2. Use ViewModel for state management
  3. Keep business logic out of Activities

LiveData

LiveData is lifecycle-aware observable data holder.

Example


viewModel.notes.observe(viewLifecycleOwner) { notes ->
println(notes)
}

Best Practices

  1. Observe LiveData in Fragments
  2. Avoid mutable LiveData exposure
  3. Use StateFlow for complex state

Room Database

Room provides an abstraction layer over SQLite.

Components

  1. Entity
  2. DAO
  3. 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

  1. Use suspend functions in DAO
  2. Avoid database access on Main thread
  3. 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

  1. Use suspend functions
  2. Handle errors with sealed classes
  3. 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

  1. Use viewModelScope
  2. Switch dispatchers appropriately
  3. Handle exceptions gracefully

Android Projects

Notes Application

  1. MVVM architecture
  2. Room for offline storage
  3. LiveData / StateFlow for UI updates

Expense Tracker

  1. CRUD operations
  2. Charts and analytics
  3. Local database with Room

Weather Application

  1. API integration with Retrofit
  2. Coroutines and Flow
  3. Location-based weather updates

News Application

  1. Pagination
  2. Offline caching
  3. 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.