Click here to Skip to main content
15,309,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on an android app which request Mars photos ans use it to display it on screen.
To make an request.And trying to use A public Api object that exposes the lazy-initialized Retrofit service.
below is source code with error
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.http.GET

class MarsApiService {
    public val retrofit = Retrofit.Builder()
    interface MarsApiService{
        fun getPhotos(): String
    object MarsApi {
        val retrofitService: MarsApiService by lazy { retrofit.create( }

    companion object {
        private const val BASE_URL = ""

17th line the code inside object MarsApi pops up errors Unresolved reference : retrofit. The call to create() function on a Retrofit object is expensive and the app needs only one instance of Retrofit API service. So, i exposed the service to the rest of the app using object declaration.

What I have tried:

The code is working if i bring code inside object MarsApi out but doing so may result in multiple instance of retrofit.
Updated 15-Sep-21 0:52am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900