Sitemap

Exploring Retrofit 3.0.0: What’s New, How to Use It with Jetpack Compose

4 min readJul 20, 2025
Press enter or click to view image in full size

Retrofit, the popular type-safe HTTP client for Android and Java developed by Square, has recently released version 3.0.0 in May 2025. This update brings exciting improvements, particularly for Kotlin developers, while maintaining compatibility with its predecessor, Retrofit 2.x. In this blog, we’ll dive into the key features of Retrofit 3.0.0, provide a step-by-step guide to using it in an Android app with Jetpack Compose, showcase a code example, and discuss its pros and cons.

What’s New in Retrofit 3.0.0?

Retrofit 3.0.0 introduces several enhancements that make it a compelling choice for modern Android development:

  • Kotlin-First Approach: Native support for Kotlin coroutines with suspend functions, eliminating the need for Call<T> when using coroutines, making asynchronous programming more idiomatic.
  • Upgraded OkHttp: Uses OkHttp 4.12 for improved performance, including better connection pooling and modern networking features.
  • Streamlined Converter Factory: The new Converter.Factory is more flexible, supporting custom converters and seamless integration with serialization libraries like Gson and Moshi.
  • Enhanced Error Handling: Suspend functions now throw HttpException directly, simplifying error management compared to Retrofit 2.x.
  • Backward Compatibility: Maintains binary compatibility with Retrofit 2.x, allowing seamless migration without rewriting existing interfaces using Call<T>.
  • Improved Testability: Cleaner mocking and better support for testing, reducing issues like null responses in mocked calls.

These updates make Retrofit 3.0.0 a modern, efficient, and developer-friendly choice for handling network requests.

Step-by-Step Guide to Using Retrofit 3.0.0 with Jetpack Compose

Let’s build a simple Android app that fetches a list of posts from the https://jsonplaceholder.typicode.com/posts API using Retrofit 3.0.0, Kotlin coroutines, and Jetpack Compose for the UI.

Step 1: Set Up Your Project

1. Create a New Android Project:

  • Open Android Studio and create a new project with an Empty Activity.
  • Choose Kotlin as the language and set the minimum API to 21 (Retrofit 3.0.0 requires Android API 21+).

2. Add Dependencies:

  • Open app/build.gradle.kts and add the following dependencies for Retrofit, Gson, and Jetpack Compose:
dependencies {
implementation("com.squareup.retrofit2:retrofit:3.0.0")
implementation("com.squareup.retrofit2:converter-gson:3.0.0")
implementation("com.google.code.gson:gson:2.11.0")
// Jetpack Compose dependencies
implementation("androidx.activity:activity-compose:1.9.2")
implementation("androidx.compose.material3:material3:1.3.0")
implementation("androidx.compose.ui:ui:1.7.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.4")
}
  • Sync your project to download the dependencies.

3. Add Internet Permission:

  • In AndroidManifest.xml, add the internet permission:
<uses-permission android:name="android.permission.INTERNET"/>

4. Enable Jetpack Compose:

  • In app/build.gradle.kts, add the following to enable Compose:
android {
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.14"
}
}

Step 2: Create the Data Model

Define a Kotlin data class to represent the JSON response from the API. For the JSONPlaceholder API, the response looks like:

[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere...",
"body": "quia et suscipit..."
},
...
]

Create a file named Post.kt:

data class Post(
val userId: Int,
val id: Int,
val title: String,
val body: String
)

Step 3: Define the API Interface

Create an interface to define the API endpoints using Retrofit annotations. Create a file named ApiService.kt:

import retrofit2.http.GET

interface ApiService {
@GET("posts")
suspend fun getPosts(): List<Post>
}

Note the use of suspend fun for coroutine support, a key feature of Retrofit 3.0.0.

Step 4: Set Up Retrofit

Create a singleton object to initialize the Retrofit instance. Create a file named RetrofitClient.kt:

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

object RetrofitClient {
private val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiService: ApiService = retrofit.create(ApiService::class.java)
}

Step 5: Create the UI with Jetpack Compose

Create a Composable function to display the list of posts in a LazyColumn (Compose’s equivalent of RecyclerView). Update MainActivity.kt to use Jetpack Compose and fetch data with Retrofit:

import android.osacyj
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import retrofit2.HttpException

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
PostListScreen()
}
}
}
}

@Composable
fun PostListScreen(viewModel: PostViewModel = viewModel()) {
var posts by remember { mutableStateOf<List<Post>>(emptyList()) }

LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
try {
posts = RetrofitClient.apiService.getPosts()
} catch (e: HttpException) {
// Handle HTTP errors
e.printStackTrace()
} catch (e: Exception) {
// Handle other errors
e.printStackTrace()
}
}
}

LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
items(posts) { post ->
Text(
text = post.title,
style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.padding(vertical = 8.dp)
)
}
}
}

class PostViewModel : ViewModel() {
// Add ViewModel logic if needed for more complex state management
}

Step 6: Run the App

  • Build and run the app. It will fetch posts from the API and display their titles in a scrollable list using Jetpack Compose’s LazyColumn.
  • Ensure your device or emulator has an internet connection.

Conclusion

Retrofit 3.0.0 is a significant step forward for Android networking, especially for Kotlin developers. Its coroutine support, improved performance, and backward compatibility make it an excellent choice for modern Android apps. The example above demonstrates how to integrate Retrofit 3.0.0 with Jetpack Compose to fetch and display API data in a sleek, modern UI. While there’s a slight learning curve for coroutines and error handling, the benefits of type safety, reduced boilerplate, and enhanced testability outweigh the drawbacks.

Ready to try Retrofit 3.0.0 with Jetpack Compose? Add it to your project and experience the power of modern Android networking! For more details, check out the official Retrofit GitHub page.

Happy coding!

--

--

No responses yet