1

I've been handling a response that has a large scale of fields particularly almost 7000 fields. I used robopojo to generate a data class out of it. Now i'am getting errors such as overflow so i have to lessen the fields. Eventually the error changed to this

04-15 14:25:19.823 17933-17933/com.example.cbc_appraisal D/androidruntime: java.lang.VerifyError: Rejecting class com.example.cbc_appraisal.model.revisedresponse.vacantlot.ValrepLandimpValuationGroupingItem because it failed compile-time verification (declaration of 'com.example.cbc_appraisal.model.revisedresponse.vacantlot.ValrepLandimpValuationGroupingItem' appears in /data/app/com.example.cbc_appraisal-1/base.apk:classes2.dex)

this is my code snip

@Parcelize data class ValrepLandimpValuationGroupingItem (

@field:SerializedName("valrep_landimp_dos_cml3_total_adjustment")
val valrepLandimpDosCml3TotalAdjustment: String? = null,

@field:SerializedName("valrep_landimp_dos_cml4_contact_no")
val valrepLandimpDosCml4ContactNo: String? = null,

@field:SerializedName("valrep_landimp_dos_dsl1_source")
val valrepLandimpDosDsl1Source: String? = null,

@field:SerializedName("valrep_landimp_dos_dsl3_source")
val valrepLandimpDosDsl3Source: String? = null,

 ... imagine 800 more

Im starting to think that kotlin cannot handle many fields. Any suggestion regarding this issue? thank you in advance!!

Amoure
  • 13
  • 4
  • 1. Do you *need* `@Parcelize`? That will add a lot of generated code for such a big class and it might be too big to put in a parcel anyway. May be better to just save it as json to a file and parse it later. – RobCo Apr 15 '20 at 07:19
  • 2. Which json parser do you use for constructing this class from the json data? [Moshi](https://github.com/square/moshi) with a `@JsonClass(generateAdapter = true)` may be a good fit since it doesn't use any reflection. – RobCo Apr 15 '20 at 07:22
  • Sir I'm using @parcelize because im passing it to another intent. If this is the reason why i'm encountering this error, can u suggest me of another work around to pass a big class to another activity thank you! – Amoure Apr 15 '20 at 07:30
  • since you are already converting to and from json, you can save it as a json string in a file. In the intent then pass only the path to the file, and in the new activity read that file again and construct this object. If you get the json from a network request it is easiest to just save that response to a file directly. – RobCo Apr 15 '20 at 07:37
  • hi sir, i already removed the @parcelize and planning to save in in a json file however, im still encountering the Rejected class because it failed compile-time verification. During testing , i tried to lessen this 800 fields to 700 and it worked. I tried adding some more and by the time i reach 750, I encountered again the same error that's why i thought kotlin cannot satisfy having too many fields. this is the behavior that ive been experiencing sir. – Amoure Apr 15 '20 at 08:04

2 Answers2

1

No, there's not limit for constructors anywhere, if a limit was present it would violet the concept of constructor overloading.

Ref this for further understanding and syntax:

https://proandroiddev.com/creating-multiple-constructors-for-data-classes-in-kotlin-32ad27e58cac

Also, Welcome to StackOverflow.

Update

To handle large amount of fields you can make groups of your fields in JSON format and form a tree structure. That way you can use as many fields as you want with clear conception and code quality.

Saswata
  • 1,290
  • 2
  • 14
  • 28
  • Hello sir @Saswata! I just read the documentation you send here and I tried to add the Annotation JVMOverloads and i get this error, Backend Internal error: Exception during code generation btw thank you for entertaining my question. Ive been looking for answers for a month and still cant look for an accurate one. Maybe my question is incorrect. However, i added a code snip on my question sir. – Amoure Apr 15 '20 at 07:10
  • Hello, due to lack of code previously I had provided you the answer of constructors. As it seems you are dealing with too many fields, refer to @gidds answer. Also you can use Objects to group your fields and then use them (Ref: the updated answer). And am no Sir. :D – Saswata Apr 16 '20 at 05:14
  • Hello sir! What I did was i have to split the objects into 2 and make 2 api class with different responses so that i can get the whole response. That's the best solution i did to deal with calls with large responses – Amoure Apr 16 '20 at 07:03
  • Yes exactly @Amoure – Saswata Apr 16 '20 at 07:11
1

According to this blog entry:

255 parameters […] is the practical maximum number of parameters a method can have on the JVM

and (since v1.3) Kotlin can support that number.

The JVM spec confirms that limit.

I'm not sure whether that limit applies directly to Android too, but this question suggests it does.  (It also suggests that trying to have anywhere near that number of parameters is a very strong code smell…)

gidds
  • 16,558
  • 2
  • 19
  • 26
  • thank you for the reply, i knew it. however do have any ideas how can i handle this kind of situation? i really have to fetch that data with more than 255 parameters :( its from my company, i already suggested them to optimize the response but they said that its not the best solution they can do right now :( – Amoure Apr 15 '20 at 09:05
  • Is there no structure within that mass of data?  If there are repeated fields or sections, then they could be broken down to Lists or Arrays; and even if not, I'm sure there's scope for splitting out groups to subobjects. – gidds Apr 15 '20 at 09:59
  • @Amoure you don't need a data class where each value is a separate field. It could be stored as a `Map` and looked up by key in runtime. For a HashMap such amounts are absolutely no problem. – RobCo Apr 15 '20 at 10:45
  • Thank you for your answer sirs. I guess i have to test your best suggestions. it did gave me a peace of mind heheh – Amoure Apr 15 '20 at 14:44
  • On Practice we faced a limit of 124 parameters inside construcor. – Vetalll Jul 07 '23 at 09:42