Mastering Retrofit in Android: The Ultimate Guide for Jetpack Compose Apps

This codelab provides a complete, hands-on guide to mastering Retrofit in Android using Jetpack Compose and modern app architecture. You’ll learn how to set up Retrofit with OkHttp, configure it with multiple converters, and define flexible API interfaces using annotations like @GET, @POST, and @Body. You'll implement secure networking using interceptors, authentication headers, and token refresh with OkHttp’s Authenticator. The codelab also covers caching, error handling, and custom converters for advanced use cases. You'll integrate Retrofit cleanly into your MVVM architecture with Hilt, test API calls using MockWebServer, and make real-time calls from Composables using LaunchedEffect, Paging, and stream large files efficiently.

Codelab Objectives

In this codelab, you’ll explore:

  • Retrofit Setup & Configuration
    • Add core Retrofit and OkHttp libraries to a Compose project
    • Configure Retrofit with base URL, OkHttpClient, and Moshi converter
    • Add multiple converters for JSON, XML, or custom formats
    • Build configurable Retrofit instances using Retrofit.Builder()
    • Override base URLs using dynamic @Url in API calls

  • API Interface and Annotations
    • Define Retrofit interfaces with suspend functions and HTTP annotations
    • Use @GET, @POST, @PUT to create coroutine-compatible API calls
    • Pass data using @Path, @Query, and @Header annotations
    • Send request bodies using @Body, @Field, and @Part annotations
    • Upload files using @Part and RequestBody in multipart requests
  • Authentication, Headers, and Interceptors
    • Add authentication headers with OkHttp interceptors
    • Automatically attach access tokens to every request
    • Use Authenticator to refresh expired tokens and retry transparently
    • Implement global 401 error handling to trigger logout or refresh tokens
  • Caching, Timeouts, and Reliability
    • Configure connect, read, and write timeouts for requests
    • Enable offline caching using OkHttp’s cache layer
    • Enable automatic retries on network failures
  • Error Handling
    • Use Response<T> and HttpException for graceful error handling
    • Deserialize error responses into data classes using converters
  • Dependency Injection with Hilt
    • Provide Retrofit and API service as Hilt singletons
    • Inject Retrofit services into the Repository and ViewModel
  • Testing and Mocking
    • Inject fake Retrofit services for unit testing
    •  Simulate responses using OkHttp MockWebServer
  • Jetpack Compose Integration
    • Trigger API calls in Composables using LaunchedEffect
    • Load paginated data using Retrofit with PagingSource
    • Stream large files using ResponseBody.byteStream()
  • Limitations and Edge Cases
    • Understand Retrofit’s limitations for real-time streaming sockets

Codelab Content

Meet the instructor

James Cullimore

Content Author & Course Instructor

More courses by the instructor