我的应用程序中有一个使用 Firebase Auth 进行身份验证的登录流程。我正在使用 Google OAuth 和基于电子邮件的身份验证。我面临的问题是登录在调试版本中工作得很好,但在发布版本中却不行。我收到此错误:
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
}
}
我从 api 端点获取 api 密钥并将其存储在 EncryptedSharedPreferences 中,以便我可以在每次后续 api 调用时使用它来实现应用程序功能。
//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)
}
}
我尝试了几个 proguard 规则,以便可以防止 Retrofit2 类的混淆。但似乎没有任何作用。我**确信这是混淆器问题,因为我尝试将 isMinifyEnabled 切换为 false 并且工作正常。** 我如何更改规则或其他内容以便我可以让它工作?
你添加了Gson的pro-guard规则吗?
##---------------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 ----------
还有一种东西叫
#android.enableR8.fullMode=false
您也可以尝试关闭此功能,在较新的 AGP 和 gradle 版本中,此功能默认启用。你可以找到这个
gradle.properties.