1

So, I have a Kotlin data class used as a data transfer object with the Retrofit library, as follows:

data class GenericPaginationResponse<T>(
@SerializedName("data")
val data: List<T>,
@SerializedName("pagination")
val pagination: PaginationResponse
)

However, when I minify the application, it encounters an error when executed with the following message:

Unable to invoke no-args constructor for class com.lelestacia.network.model.GenericPaginationResponse. Registering an InstanceCreator with Gson for this type may fix this problem.

I am using this data class in the network module of an Android project with a multi-module architecture. Here is the repository : Github Repository

Here is my proguard:

# If you keep the line number information, uncomment this to
# hide the original source file name.
-renamesourcefileattribute SourceFile

 # 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

 -if class * {
   @com.google.gson.annotations.SerializedName <fields>;
 }

 -keep class <1> {
   <init>();
 }
lelestacia
  • 201
  • 2
  • 9

2 Answers2

2

In your Network module there is file consumer-rules.pro

Add below code

-keep class com.lelestacia.network.model.*
-keep class com.lelestacia.network.model.anime.*

Also in your build.gradle of network module in release build type, add below code

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'consumer-rules.pro'

You can follow below link for consumer-rules vs proguard-rules https://stackoverflow.com/a/60862591/8007341

Kishan Maurya
  • 3,356
  • 8
  • 21
  • so, theres no way i could obfuscate this? – lelestacia Apr 12 '23 at 05:09
  • lets suppose you want to obfuscate network module code. All code/file/method will be obfuscated and everything will work seamlessly for that network module. This mean all code inside Network module will work after obfuscation. But problem aries when you want to share pojo/classes for network module to other modules. Now since all code is obfuscated, other module will not understand this and start throwing error at runtime. To resolve this problem, you have to use proguard rules which will obfuscate all code/files except mentioned files inside rules. – Kishan Maurya Apr 12 '23 at 19:36
  • If you write all your code in one single module( App) then obfuscation will properly. In multimodule project, if you want to obfuscate each and every module then you have to use proguard/consumer rules. Every module will work properly individually but when share data/use each other then problem will happen incase if you use obfuscation/minify – Kishan Maurya Apr 12 '23 at 19:42
  • do you have any reference to learn all this stuff? Im kinda lost here in your explanation – lelestacia Apr 18 '23 at 14:32
0

You need to mention below code in proguard rules.

-keepclasseswithmembers class com.lelestacia.network.model** { *; } 

Feel free to ask if something is unclear.

Kamal Nayan
  • 1,635
  • 1
  • 5
  • 19
  • so, theres no way i could obfuscate this? – lelestacia Apr 12 '23 at 05:09
  • Second option is to use annotation on every data class `@Keep`. For example `@Keep data class GenericPaginationResponse( @SerializedName("data") val data: List, @SerializedName("pagination") val pagination: PaginationResponse )` – Kamal Nayan Apr 12 '23 at 11:18
  • so, i dont really know about obfuscation in industry app since i never work at a real project like that. But, is this really okay to let data class like in this case to not get obfuscated? Isn't it become easier if someone wanted to do reverse engineer? or we don't actually need to obfuscate everything, like we can just let our DTO to not get obfuscated. I would really appreciate it if you want to explain it – lelestacia Apr 12 '23 at 15:32