Search code examples
androidapkandroid-manifestandroid-developer-api

After i created apk my android app doesn't fetch any data from api


When i run my app on my emulator or my physical device it works fine but when i create an apk and send to someone elses device it doesnt work.

That's how my build.gradle (app) file looks.


android {
    namespace 'com.example.movielibrary'
    compileSdk 34

    defaultConfig {
        applicationId "com.movielib.movielibrary"
        minSdk 24
        targetSdk 33
        versionCode 6
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
    buildFeatures {
        viewBinding true
        buildConfig true
    }
}



object Constants {

const val BASE_URL = "https://api.themoviedb.org/3/"
const val API_KEY = "12345656987123456123"
const val IMAGE_BASE_URL = "https://image.tmdb.org/t/p/w500"
const val FAV_TABLE= "fav_table"
const val FAV_DATABASE= "fav_database"

const val NETWORK_TIMEOUT = 60L }
 @Module
@InstallIn(SingletonComponent::class)
object ApiModule {

    @Provides
    @Singleton
    fun provideBaseUrl() = BASE_URL

    @Provides
    @Singleton
    fun connectionTimeout() = NETWORK_TIMEOUT


    @Provides
    @Singleton
    fun provideGson(): Gson = GsonBuilder().setLenient().create()

    @Singleton
    @Provides
    fun provideOkHttpClient() = if (BuildConfig.DEBUG) {
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS)
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)

        val requestInterceptor = Interceptor { chain ->
            val url = chain.request()
                .url
                .newBuilder()
                .addQueryParameter("api_key", API_KEY)
                .build()

            val request = chain.request()
                .newBuilder()
                .url(url)
                .build()
            return@Interceptor chain.proceed(request)
        }

        OkHttpClient
            .Builder()
            .addInterceptor(requestInterceptor)
            .addInterceptor(loggingInterceptor)
            .build()
    } else {
        OkHttpClient
            .Builder()
            .build()
    }


    @Provides
    @Singleton
    fun provideRetrofit(baseUrl: String, gson: Gson, client: OkHttpClient): ApiServices =
        Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build()
            .create(ApiServices::class.java)
}

Im trying to generate signed apk or signed bundle but api doesn't fetch any data i don't use local.properties to hide my api key or anything else how to solve this problem ?


Solution

  • You should remove the BuildConfig.DEBUG check for adding requestInterceptor.