1

I'm working on a multi-module project. In a module called network I'm providing the instance of Retrofit through Hilt dependency injection and works fine, I can compile the app, and run unit and instrumentation tests but when I try to execute all unit tests from the terminal as .\gradlew test then this error appears:

Unresolved reference: BASE_API_URL

Which is a key saved into BuildConfig

This is how looks like my debug build type:

buildTypes {
    debug {
        minifyEnabled = false
        debuggable = true
        buildConfigField "String", "BASE_API_URL", "\"https://gateway.marvel.com:443\""
        buildConfigField "String", "PUBLIC_API_KEY", "\"c3103d371d2sddd625sdsd4c9f12f0qq312dcbd5102165\""
        buildConfigField "String", "PRIVATE_API_KEY", "\"26121b5a8849ac897ace8d33a66a6f731ff7a93a\""
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }

    debugTesting {
        minifyEnabled = false
        debuggable = true
        buildConfigField "String", "BASE_API_URL", "\"http://localhost:8080\""
        buildConfigField "String", "PUBLIC_API_KEY", "\"abcde\""
        buildConfigField "String", "PRIVATE_API_KEY", "\"abcd\""
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

And this is how I'm retrieving the value:

@Provides
@Singleton
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder()
        .baseUrl(BuildConfig.BASE_API_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .build()
}

Maybe some of you have faced this problem before and now know how to fix it. Thanks in advance for the help

Oscar Ivan
  • 819
  • 1
  • 11
  • 21

1 Answers1

1

Finally, I found the solution to the issue, it was due because in my multimodule project when I ran the tests like .\gradlew test the project was built based on defaultConfig. One workaround that I found was to put the buildConfigField in the defaultConfig like: enter image description here

But later I noticed that we can specify over which build variant build the project in order to execute the unit tests, finally, I got the following solution:

./gradlew testDebugUnitTest
Oscar Ivan
  • 819
  • 1
  • 11
  • 21