Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Kotlin reflection sealedSubClasses property in Android release build

When I am trying to use the sealedSubClasses attribute of a reified class in Kotlin, it only works in my debug and not in my release build. I guess this is a problem with ProGuard, but I don't know how to fix this. I already tried keeping all classes in the module where the sealed class is, but I am having no luck with this. The sealedSubClasses property always returns an empty list.

like image 705
sunilson Avatar asked Dec 16 '19 16:12

sunilson


2 Answers

Found two ways to fix this issue:

  1. Add rule to your proguard-rules.pro file:
    -keep class com.example.ClassName { *; }
    -keep class com.example.ClassName$* { *; }
  1. Use @Keep annotation:
    @Keep
    sealed class ClassName{
        @Keep
        object A : ClassName()
        @Keep
        object B : ClassName()
    }

This one didn't work for me:

-keep class kotlin.Metadata { *; }

Also there is a bug https://issuetracker.google.com/issues/169264693 that could be the part of your problem.

like image 104
Viktor Yakunin Avatar answered Nov 06 '22 20:11

Viktor Yakunin


You can try

-keep class kotlin.Metadata { *; }

to get missing attributes from your class

like image 1
user8842561 Avatar answered Nov 06 '22 20:11

user8842561