{"id":13609594,"url":"https://github.com/facebookincubator/dataclassgenerate","last_synced_at":"2025-10-27T09:16:19.818Z","repository":{"id":136198629,"uuid":"602269371","full_name":"facebookincubator/dataclassgenerate","owner":"facebookincubator","description":"DataClassGenerate (or simply DCG) is a Kotlin compiler plugin that addresses an Android APK size overhead from Kotlin data classes. ","archived":false,"fork":false,"pushed_at":"2024-12-18T03:34:42.000Z","size":200,"stargazers_count":157,"open_issues_count":1,"forks_count":2,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-12-18T04:25:59.617Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/facebookincubator.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-15T21:15:02.000Z","updated_at":"2024-12-18T03:34:46.000Z","dependencies_parsed_at":"2024-05-03T16:12:51.678Z","dependency_job_id":"c79a302f-a1a8-43a5-a2be-61809ecc2c75","html_url":"https://github.com/facebookincubator/dataclassgenerate","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facebookincubator%2Fdataclassgenerate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facebookincubator%2Fdataclassgenerate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facebookincubator%2Fdataclassgenerate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facebookincubator%2Fdataclassgenerate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/facebookincubator","download_url":"https://codeload.github.com/facebookincubator/dataclassgenerate/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248629839,"owners_count":21136325,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T19:01:36.320Z","updated_at":"2025-10-27T09:16:19.799Z","avatar_url":"https://github.com/facebookincubator.png","language":"Kotlin","funding_links":[],"categories":["Kotlin"],"sub_categories":[],"readme":"![DCG Logo](media/logo-full-color.svg#gh-light-mode-only)\n![DCG Logo](media/logo-white.svg#gh-dark-mode-only)\n\n# DataClassGenerate\n_or simply DCG_\n\n\n## Motivation\nDataClassGenerate is a Kotlin compiler plugin that addresses APK size overhead.\nDCG brings data class’ app size contribution on-par with semantically identical Plain Java Objects.\nKotlin positions data class as an easy-to-use and low friction utility for representing plain-old data.\nDespite appearing compact in source code, the compiler generates multiple utility methods for each data class, which impact APK size e.g.: `hashCode`, `equals`, `toString`, `copy`, `componentN`.\nBefore an optimizer (like [Redex](https://github.com/facebook/redex/) or [R8](https://r8.googlesource.com/r8)) can strip these methods, it must prove that they are never used.\nSince methods like `toString`, `equals`, and `hashCode` are so fundamental (defined on `Object`/`Any`), calls to them will exist all over the app, and the optimizer must prove that a data class would not flow into any of those call sites, which is often not possible.\n\n## How to use in your project\n\nApply the gradle plugin.\n```kotlin\nplugins {\n  id(\"com.facebook.kotlin.compilerplugins.dataclassgenerate\") version \u003cversion\u003e\n}\n```\nAnd that's it! The default configuration will add the artifact which contains `@DataClassGenerate` automatically.\n\nIf needed, you can configure the DataClassGenerate plugin using `dataClassGenerate` block:\n```kotlin\nimport com.facebook.kotlin.compilerplugins.dataclassgenerate.gradle.DataClassGenerateMode\n\ndataClassGenerate {\n    enabled.set(true/false) // defaults to true\n    mode.set(DataClassGenerateMode.EXPLICIT/STRICT/IMPLICIT) // defaults to EXPLICIT\n    generateSuperClass.set(true/false) // defaults to false\n}\n```\n\n## How it works\nDataClassGenerate Kotlin Compiler Plugin processes `@DataClassGenerate` annotation.\n\n### `@DataClassGenerate` ANNOTATION\n_NOTE:_ DCG is applicable to Kotlin data classes only.\n\n_NOTE:_ DCG configures an intent to generate (KEEP) or skip (OMIT) `toString`, `equals`, and `hashCode` methods generation.\n\n_NOTE:_ DCG does not configure `copy`, `componentN`, and other methods, but [Redex](https://github.com/facebook/redex/) or R8 will take care of them.\n\n`@DataClassGenerate` annotation configures Kotlin data class code generation and bytecode optimizations.\n`@DataClassGenerate` is applicable only to Kotlin data classes, and will not make any change if applied to a regular class (*Unfortunately, Kotlin does not have a data class as an annotation target*).\n\n\n`@DataClassGenerate` annotation, together with applied DataClassGenerate compiler plugin, does 3 things:\n1. Configures code generation of `toString` method.\n    - `@DataClassGenerate(toString = Mode.KEEP)` will generate `toString` as in a usual data class.\n    - `@DataClassGenerate(toString = Mode.OMIT)` will NOT generate `toString` for an annotated data class.\n1. Configures code generation of `equals` and `hashCode` methods.\n    - `@DataClassGenerate(equalsHashCode = Mode.KEEP)` will generate `equals` and `hashCode` as in usual data class.\n    - `@DataClassGenerate(equalsHashCode = Mode.OMIT)` will NOT generate `equals` and `hashCode` for an annotated data class.\n1. Adds a marker super class `DataClassSuper` for suitable Data Classes to make them available for Redex [Class Merging Optimization](https://github.com/facebook/redex/blob/main/docs/passes.md#classmergingpass).\n    - Do not upcast data classes to `DataClassSuper`. There is no guarantee a concrete data class will get a marker super class.\n\n\n### Generation modes\n\nHow we can set `@DataClassGenerate (toString = ???, equalsHashCode = ???)` arguments?\n\n|  Mode | Explanation |\n|-------|-------|\n| `KEEP` | Express an intent to keep/generate a method(s) |\n| `OMIT` | Express an intent to omit method(s) generation. |\n\n### Annotation defaults\n`@DataClassGenerate` is declared with the following defaults:\n ```kotlin\n annotation class DataClassGenerate(\n    val toString: Mode = Mode.OMIT,\n    val equalsHashCode: Mode = Mode.KEEP\n)\n```\n\nKotlin allows annotation parameter name ommission. The table below explains `@DataClassGenerate` shortcuts:\n\n|  Declaration | Explicit Equivalent |\n|-------|-------|\n|`@DataClassGenerate` | `@DataClassGenerate(toString = Mode.OMIT, equalsHashCode = Mode.KEEP)`|\n|`@DataClassGenerate()` | `@DataClassGenerate(toString = Mode.OMIT, equalsHashCode = Mode.KEEP)`|\n|`@DataClassGenerate(Mode.KEEP)` | `@DataClassGenerate(toString = Mode.KEEP, equalsHashCode = Mode.KEEP)`|\n|`@DataClassGenerate(Mode.KEEP, Mode.OMIT)` | `@DataClassGenerate(toString = Mode.KEEP, equalsHashCode = Mode.OMIT)`|\n|`@DataClassGenerate(equalsHashCode = Mode.OMIT)` | `@DataClassGenerate(toString = Mode.OMIT, equalsHashCode = Mode.OMIT)`|\n\n## DCG  MODES\n`DataClassGenerate` Kotlin compiler plugin works in multiple modes, but the most important is a `STRICT` mode.\n\n### `STRICT` mode\nIn `STRICT` mode:\n- Plugin applies only to data classes annotated with `@DataClassGenerate(...)`.\n   - DCG will act following annotation instructions for method generation.\n   - DCG will add marker super classes.\n\n- Plugin reports a compilation error whenever it sees a data class without `@DataClassGenerate(...)` annotation.\n\nExample:\n\n```\n// Plugin will generate:\n// -`toString`\n// -`equals`, and `hashCode`\n@DataClassGenerate(toString = Mode.KEEP, equalsHashCode = Mode.KEEP)\ndata class A(val i:Int)\n\n// Plugin will report a compilation error\ndata class B(val l:Long)\n```\n\n### `EXPLICIT` mode (current default)\nIn `EXPLICIT` mode:\n- If a data class is annotated with `@DataClassGenerate`, DCG will act according to the annotation instructions for method generation.\n- If data class is NOT annotated with @DataClassGenerate, DCG will only add marker super classes (read previous section for details).\n\nExample:\n\n```\n// Plugin will generate:\n// -`toString`\n// -`equals`, and `hashCode`\n@DataClassGenerate(toString = Mode.KEEP, equalsHashCode = Mode.KEEP)\ndata class A(val i:Int)\n\n// Plugin will only create a marker super class\ndata class B(val l:Long)\n\n// Plugin will do nothing\ndata class C(val l:Long): SomeSuperClass()\n```\n\n\n## Releases\n\nComing soon\n\n## Compiler compatibility\n\n| K1        | K2                  |\n|-----------|---------------------|\n| 1.5x      | N/A                 |\n| 1.6x      | N/A                 |\n| 1.7x-1.9x | 1.7.21 + `-Xuse-k2` |\n| N/A       | 2.0+                |\n\n## Public talks\n- [FOSDEM'22. DataClassGenerate. Shrinking Kotlin data classes](https://archive.fosdem.org/2022/schedule/event/dataclassgenerate_shrinking_kotlin_data_classes/)\n- [Droidcon'21. Kotlin Adoption at Scale](https://www.droidcon.com/2021/11/17/kotlin-adoption-at-scale/)\n\n## Project team\nDCG was created by the Kotlin Foundation team @ Meta:\n- [Sergei Rybalkin](https://github.com/rybalkinsd/),\n- [Adrian Catana](https://github.com/adicatana/),\n- [Michal Zielinski](https://github.com/zielinskimz/),\n- [Hui Qin Ng](https://github.com/nghuiqin/),\n- [Nicola Corti](https://github.com/cortinico/)\n\n## License\nDataClassGenerate is [MIT-licensed](https://github.com/facebookincubator/dataclassgenerate/blob/main/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffacebookincubator%2Fdataclassgenerate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffacebookincubator%2Fdataclassgenerate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffacebookincubator%2Fdataclassgenerate/lists"}