kotlin 在Gradle中启用minify时无法为类创建转换器

ktecyv1j  于 2023-01-31  发布在  Kotlin
关注(0)|答案(1)|浏览(125)

我尝试在Gradle文件中启用minifyEnabled true进行部署,但收到以下错误:

java.lang.IllegalArgumentException: Unable to create converter for class com.th3pl4gu3.mes.models.MesResponse
for method MesApiService.getServices

我的代码如下
AppContainer.kt

/**
 * Dependency Injection container at the application level.
 */
interface AppContainer {
    val onlineServiceRepository: ServiceRepository
}

/**
 * Implementation for the Dependency Injection container at the application level.
 *
 * Variables are initialized lazily and the same instance is shared across the whole app.
 */
class DefaultAppContainer(private val context: Context): AppContainer {
    private val BASE_URL = "https://..."

    /**
     * Use the Retrofit builder to build a retrofit object using a kotlinx.serialization converter
     */
    @OptIn(ExperimentalSerializationApi::class)
    private val retrofit: Retrofit = Retrofit.Builder()
        .addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
        .baseUrl(BASE_URL)
        .build()

    /**
     * Retrofit service object for creating api calls
     */
    private val retrofitService: MesApiService by lazy {
        retrofit.create(MesApiService::class.java)
    }
}

ServiceRepository.kt

/**
 * Repository that fetch service list from Mes API.
 */
interface ServiceRepository {
    /** Fetches list of services from Mes API */
    suspend fun getMesServices(): MesResponse
}

MesApiServiceRepository.kt

class MesApiServiceRepository(

    private val mesApiService: MesApiService

) : ServiceRepository {
    /** Fetches list of Services from Mes API */
    override suspend fun getMesServices(): MesResponse = mesApiService.getServices()
}

MesResponse.kt

@kotlinx.serialization.Serializable
data class MesResponse(
    val services: List<Service>,
    val message: String,
    val success: Boolean
)

proguard-rules.pro

#####################################
########## Kotlin Metadata ##########
#####################################
-dontwarn org.jetbrains.annotations.**
-keep class kotlin.Metadata { *; }

##################################
########### MES Models ###########
##################################
-keep class com.th3pl4gu3.mes.models.MesResponse
-keep class com.th3pl4gu3.mes.models.** {*;}
-keep class com.th3pl4gu3.mes.data.** {*;}
-keep class com.th3pl4gu3.mes.api.** {*;}
-keep class com.th3pl4gu3.mes.di.** {*;}

###############################
########## Retrofit2 ##########
###############################
# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod

# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations

# Keep annotation default values (e.g., retrofit2.http.Field.encoded).
-keepattributes AnnotationDefault

# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}

# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**

# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit

# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.KotlinExtensions
-dontwarn retrofit2.KotlinExtensions$*

# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>

# Keep inherited services.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface * extends <1>

# Keep generic signature of Call, Response (R8 full mode strips signatures from non-kept items).
-keep,allowobfuscation,allowshrinking interface retrofit2.Call
-keep,allowobfuscation,allowshrinking class retrofit2.Response

# With R8 full mode generic signatures are stripped for classes that are not
# kept. Suspend functions are wrapped in continuations where the type argument
# is used.
-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

有人能帮帮我吗?

mrwjdhj3

mrwjdhj31#

我可以通过向MesResponse类添加@Keep来修复它。

@Keep
@kotlinx.serialization.Serializable
data class MesResponse(
    val services: List<Service>,
    val message: String,
    val success: Boolean
)

相关问题