skip to Main Content

I have a login flow in my app that uses Firebase Auth for authentication. I’m using both Google OAuth and Email based auth. The problem I’m facing is that login works just fine in debug build but not in release build. I get this error:

024-10-16 15:34:52.847 18432-18432 AuthViewModel           com.logomo.logomo              E  Error fetching API key: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType package com.logomo.logomo.data.login.  
private suspend fun loginAndGetApiKey(uid: String) {
    try {
        _isLoading.value = true
        logInResponse = logInRepository.signInUser(uid)
        // Suspends here until API call finishes
        logInResponse?.let {
            _apiKey.value = it.api_key
            Log.d(TAG, "getApiKey: ${it.status}")
            Log.d(TAG, "getApiKey: ${apiKey.value}")

            // Save the API key and retrieve it safely
            apiKey.value?.let { it1 -> saveApiKey(apiKey = it1) }
            val savedApiKey = getApiKey()
            Log.d(TAG, "getApiKeyFunc: $savedApiKey")
        } ?: run {
            Log.e(TAG, "Login response is null, failed to retrieve API key")
        }
    } catch (e: Exception) {
        signOut()

        Log.e(TAG, "Error fetching API key: ${e.message}")
    } finally {
        _isLoading.value = false
    }
}

I’m getting api key from the api end point and storing it in EncryptedSharedPreferences so that I can use it every subsequent api call for app functionalities.

//This is the ApiService class
import com.logomo.logomo.retrofit.SignInUserData
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.POST

interface LogInApiService {
    @FormUrlEncoded
    @POST("users/user_login.php")
    suspend fun signInUser(
        @Field("uid") uid: String,
    ): SignInUserData
}
//This is the Repository class
import com.logomo.logomo.retrofit.SignInUserData
import javax.inject.Singleton

@Singleton
class NetworkLogInRepository(
    private val logInApiService: LogInApiService
) {
    suspend fun signInUser(uid: String): SignInUserData {
        return logInApiService.signInUser(uid)
    }
}

I tried several proguard rules so that I can prevent obfuscation of retrofit2 classes. But nothing seems to work. I**’m sure it’s proguard issue since I tried switching isMinifyEnabled to false and it worked fine.** How can I change the rules or something so that I can get it to work?

2

Answers


  1. Chosen as BEST ANSWER

    I was able to solve this myself. The issue was with proguard-rules.pro. I was using keep rules for Retrofit, Gson and other crucial libraries but not for Hilt due to which it was having issues. When I added keep rules for Hilt, everything is working correctly.


  2. Did you add the pro-guard rules from Gson?

    ##---------------Begin: proguard configuration for Gson  ----------
    # Gson uses generic type information stored in a class file when working with fields. Proguard
    # removes such information by default, so configure it to keep all of it.
    -keepattributes Signature
    
    # For using GSON @Expose annotation
    -keepattributes *Annotation*
    
    # Gson specific classes
    -dontwarn sun.misc.**
    #-keep class com.google.gson.stream.** { *; }
    
    # Application classes that will be serialized/deserialized over Gson
    -keep class com.google.gson.examples.android.model.** { <fields>; }
    
    # Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
    # JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
    -keep class * extends com.google.gson.TypeAdapter
    -keep class * implements com.google.gson.TypeAdapterFactory
    -keep class * implements com.google.gson.JsonSerializer
    -keep class * implements com.google.gson.JsonDeserializer
    
    # Prevent R8 from leaving Data object members always null
    -keepclassmembers,allowobfuscation class * {
      @com.google.gson.annotations.SerializedName <fields>;
    }
    
    # Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
    -keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
    -keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
    
    # Keep all fields of classes that will be serialized/deserialized over Gson
    -keepclassmembers class * {
      @com.google.gson.annotations.SerializedName <fields>;
    }
    
    ##---------------End: proguard configuration for Gson  ----------
    

    Also there is something called

    #android.enableR8.fullMode=false
    

    You can try turning this off as well, in newer AGP and gradle versions this is enabled by default. You can find this gradle.properties.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search