Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Retrofit instance created through Singleton pattern in Kotlin using lazy initialization
package com.andresjakl.partslist
import android.util.Log
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
// Singleton pattern in Kotlin:
object WebAccess {
val partsApi : PartsApiClient by lazy {
Log.d("WebAccess", "Creating retrofit client")
val retrofit = Retrofit.Builder()
// The address routes request from the Android emulator
// to the localhost / of the host PC
// Moshi maps JSON to classes
// The call adapter handles threads
// Create Retrofit client
return@lazy retrofit.create(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment