1

I have enabled Proguard and configured the rules

            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  • For GSON I used these rules
  • added my models to exceptions (-keep class my.package.model.** { *; })
  • Also used other rules for various libraries

I have this error -> Registering an InstanceCreator with Gson for this type may fix this problem. the error occurred when switching to kotlin 1.6.0 and 1.6.10. There is no error on version Kotlin 1.5.21!

Error (I have a bug only on Kotlin 1.6.0/1.6.10. There is no error on kotlin 1.5.21) --->

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.project.main, PID: 2943
java.lang.RuntimeException: Unable to invoke no-args constructor for class com.project.main.domain.entity.MyEntityClass. Registering an InstanceCreator with Gson for this type may fix this problem.
    at com.google.gson.internal.c$e.a(SourceFile:228)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(SourceFile:212)
    at com.google.gson.Gson.h(SourceFile:963)
    at com.google.gson.Gson.j(SourceFile:928)
    at com.google.gson.Gson.l(SourceFile:877)
    at com.project.main.data.cache.converter.MyEntityClassConverter.toMyEntityClass(SourceFile:14)
    at com.project.main.data.cache.dao.MyProjectDao_Impl$21.call(SourceFile:947)
    at com.project.main.data.cache.dao.MyProjectDao_Impl$21.call(SourceFile:889)
    at k1.c$a$a$a$a.invokeSuspend(SourceFile:128)
    at uh.a.resumeWith(SourceFile:33)
    at uk.z0.run(SourceFile:106)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)
 Caused by: java.lang.UnsupportedOperationException: Abstract class can't be instantiated! Class name: com.project.main.domain.entity.MyEntityClass
    at com.google.gson.internal.l.a(SourceFile:120)
    at com.google.gson.internal.l$a.c(SourceFile:49)
    at com.google.gson.internal.c$e.a(SourceFile:225)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(SourceFile:212) 
    at com.google.gson.Gson.h(SourceFile:963) 
    at com.google.gson.Gson.j(SourceFile:928) 
    at com.google.gson.Gson.l(SourceFile:877) 
    at com.project.main.data.cache.converter.MyEntityClassConverter.toMyEntityClass(SourceFile:14) 
    at com.project.main.data.cache.dao.MyProjectDao_Impl$21.call(SourceFile:947) 
    at com.project.main.data.cache.dao.MyProjectDao_Impl$21.call(SourceFile:889) 
    at k1.c$a$a$a$a.invokeSuspend(SourceFile:128) 
    at uh.a.resumeWith(SourceFile:33) 
    at uk.z0.run(SourceFile:106) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at java.lang.Thread.run(Thread.java:923) 

I have rules for this error. -keep class com.project.main.domain.entity.MyEntityClass { ; } but I'm adding the whole package -keep class com.project.main.domain.entity.* { *; }

I also have all the other rules for GSON -> rules

  @TypeConverter
  fun toMyEntityClass(value: String): MyEntityClass = 
gson.fromJson(value, object : TypeToken<MyEntityClass>() {}.type) //Error

I also tried to prescribe just one rule. The rule helps to run the project on 1.5.21 and works. But it doesn't work on 1.6.0 - 1.6.10. I don't want to create an Instance Creator. I have too many models and I will waste a lot of time.

-keep class ** { *; }
Roman Irtov
  • 363
  • 3
  • 12
  • 1
    Could you please include the complete exception with its cause here? That should give a bit more information why Gson was unable to create the instance. – Marcono1234 Dec 23 '21 at 00:24
  • Marcono1234 I have no right to lay out the entire Proguard-rules. For GSON, the rules were taken from Git link provided above. # Application classes that will be serialized/deserialized over Gson -keep class com.google.gson.examples.android.model.** { ; } The part that requires improvement from me, as I have already noted above, has been finalized and all my data models have been added to the exception. – Roman Irtov Dec 23 '21 at 07:29
  • I meant the exception stack trace (e.g. `RuntimeException: ... at ... at ... Caused by: ...`); if you have not changed the ProGuard rules, then you most likely don't have to include them here. However, without complete exception stack trace it is difficult to tell what the reason for this could be. – Marcono1234 Dec 24 '21 at 00:09
  • - Marcono1234 The main problem is not in my rules, but that everything works on Kotlin 1.5.21. But it doesn't work on Kotlin 1.6.0 - 1.6.10 private val myEntryTypeToken= object : TypeToken() {}.type @TypeConverter fun toMyEntry(value: String): MyEntry= gson.fromJson(value, myEntryTypeToken) gson.fromJson(value, myEntryTypeToken) -> Does not work on kotlin 1.6.0-1.6.10. But it works on Kotlin 1.5.21 – Roman Irtov Dec 27 '21 at 14:56
  • As mentioned before, please provide the exception stack trace. Without it, it is not possible to tell what is causing the issue. – Marcono1234 Dec 27 '21 at 15:33
  • I have the same problem with Kotlin 1.6.0. After upgrading, my proguard rules stoped having effect. – noe Jan 03 '22 at 15:55
  • @Marcono1234 I have attached a part of the stack that has to do with the error. I have replaced the names of my models and the package name. I have rules for this error. -keep class com.model.MyClass { *; } but I'm adding the whole package -keep class com.model.**{ *; } – Roman Irtov Jan 04 '22 at 15:27
  • Thanks, could you please also include the "Caused by: ..." lines of the stack trace? – Marcono1234 Jan 04 '22 at 20:10
  • @Marcono1234 I have updated the trace stack . But I had to change the real package and the names. The error occurs on ->gson.fromJson(value, object : TypeToken() {}.type). At the same time, the rules work perfectly on Kotlin 1.5.21. – Roman Irtov Jan 05 '22 at 09:04
  • I think it's worth waiting for the update of GSON and other libraries. In the description of Kotlin in 1.6.0 there were serializable software updates – Roman Irtov Jan 05 '22 at 09:06
  • The exception cause ("Abstract class can't be instantiated") sounds like indeed the way Kotlin compiled the code has changed. I am not sure if there is something which Gson can do to solve this. Without knowing how exactly your `MyEntityClass` class looks like, it is difficult to tell how to solve this. But maybe registering a Gson `InstanceCreator` could indeed be the solution to this issue. – Marcono1234 Jan 06 '22 at 00:05
  • @Marcono1234 There is another project where Instance Creator is used. I can't be 100% sure, but there were no problems with Proguard. I have too many models, it will take a long time to create Instance Creator for all of them. – Roman Irtov Jan 06 '22 at 07:20
  • I tried the rules -> -keep class ** { *; } One rule and the whole project works on Kotlin 1.5.21. But does not work on 1.6.0-1.6.10. I hope that updates to Kotlin or GSON will solve my problem – Roman Irtov Jan 06 '22 at 07:22

2 Answers2

0

For any classes that interact with GSON you have to keep as follow :

-keep class "MODEL CLASSES"

for your issue you can do like :

-keep class com.project.main.domain.entity.** { *; }

It will keep all classes under the entity package.

Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
0

The problem was solved by itself, after updating the libraries gson 2.9.0 and kotlin 2.6.21 I didn't do anything, just updated to these versions I haven't checked 1.6.0 and 1.6.10 works in tandem with gson 2.9.0 but gson didn't work with the previous release version As a result, I upgraded Kotlin from 1.5.21 -> 1.6.21

Roman Irtov
  • 363
  • 3
  • 12