{"id":20592109,"url":"https://github.com/bennyhuo/mixin","last_synced_at":"2025-04-14T22:54:10.201Z","repository":{"id":57717764,"uuid":"426646493","full_name":"bennyhuo/Mixin","owner":"bennyhuo","description":"An annotation processor to mix Java or Kotlin Classes up into a single Class. Also a sample of X Processing which is an abstract layer of apt and ksp.","archived":false,"fork":false,"pushed_at":"2022-11-06T11:47:56.000Z","size":186,"stargazers_count":30,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-14T22:53:55.759Z","etag":null,"topics":["android","kapt","kotlin","ksp"],"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/bennyhuo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-10T14:10:03.000Z","updated_at":"2024-08-31T01:54:27.000Z","dependencies_parsed_at":"2022-09-18T08:31:07.562Z","dependency_job_id":null,"html_url":"https://github.com/bennyhuo/Mixin","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/bennyhuo%2FMixin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennyhuo%2FMixin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennyhuo%2FMixin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennyhuo%2FMixin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bennyhuo","download_url":"https://codeload.github.com/bennyhuo/Mixin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248975330,"owners_count":21192208,"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":["android","kapt","kotlin","ksp"],"created_at":"2024-11-16T07:42:46.125Z","updated_at":"2025-04-14T22:54:10.182Z","avatar_url":"https://github.com/bennyhuo.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mixin\n\nThis is an annotation processor to mix Java or Kotlin Classes up into a single Class. \n\nThis is also a sample of [X Processing](https://androidx.tech/artifacts/room/room-compiler-processing/) which is an abstract layer of apt and ksp.\n\n## Usage\n\nThe design of this project is very straightforward.\n\nSuppose I have two api classes for User:\n\n```kotlin\nclass UserApi {\n    fun getName(id: Long): String = TODO()\n\n    fun getAge(id: Long): Int = TODO()\n}\n```\n\n```java\npublic class GitHubUserApi {\n    public String getGitHubUrl(long id) {\n        throw new NotImplementedError();\n    }\n\n    public int getRepositoryCount(long id) {\n        throw new NotImplementedError();\n    }\n}\n```\n\nand I want to combine them into a single class `UserApis`, I will annotate these two classes with `@Mixin`:\n\n```kotlin\n@Mixin(\"com.bennyhuo.kotlin.mixin.sample\", \"UserApis\")\nclass UserApi { \n    ... \n}\n```\n\n```java\n@Mixin(packageName = \"com.bennyhuo.kotlin.mixin.sample\", name = \"UserApis\")\npublic class GitHubUserApi {\n    ...\n}\n```\n\nwith apt or ksp, `com.bennyhuo.kotlin.mixin.sample.UserApis` will be generated: \n\n```java\npackage com.bennyhuo.kotlin.mixin.sample;\n\nimport com.bennyhuo.kotlin.mixin.sample.library1.UserApi;\nimport com.bennyhuo.kotlin.mixin.sample.library2.GitHubUserApi;\nimport java.lang.String;\nimport org.jetbrains.annotations.NotNull;\n\npublic class UserApis {\n  private final UserApi userApi;\n\n  private final GitHubUserApi gitHubUserApi;\n\n  public UserApis() {\n    userApi = new UserApi();\n    gitHubUserApi = new GitHubUserApi();\n  }\n\n  public int getRepositoryCount(long id) {\n    return gitHubUserApi.getRepositoryCount(id);\n  }\n\n  public String getGitHubUrl(long id) {\n    return gitHubUserApi.getGitHubUrl(id);\n  }\n\n  @NotNull(\"\")\n  public String getName(long id) {\n    return userApi.getName(id);\n  }\n\n  public int getAge(long id) {\n    return userApi.getAge(id);\n  }\n}\n```\n\nThat's it. \n\nIt is pretty useful in some cases. \n\nFor example, the JavaScript bridge for Android WebView. You may want to separate different bridge functions into different modules and add them to the WebView as one bridge object, so you have to write a wrapper class in the main module to assemble all the bridge functions. With this library, you can easily achieve that.\n\n## Set up your project\n\nThe basic settings is shown as below:\n\n```kotlin\nplugins {\n    id(\"com.google.devtools.ksp\") version \"1.6.0-1.0.1\" // ksp\n    id(\"org.jetbrains.kotlin.kapt\") // kapt\n}\n\nrepositories {\n    mavenCentral()\n    google() \n}\n\ndependencies {\n    ksp(\"com.bennyhuo.kotlin:mixin-compiler:1.6.0.1\") // ksp\n    kapt(\"com.bennyhuo.kotlin:mixin-compiler:1.6.0.1\") // kapt\n    implementation(\"com.bennyhuo.kotlin:mixin-annotations:1.6.0.1\")\n}\n```\n\n### Multi-modules \n\nIf you want to mix class files from different modules, you should configure some of them as 'library' to generate index. \n\nFor example, we have \n\n* an `Api0.java` in module `a`\n* an `Api1.kt` in module `b`\n* an `ApiMain.kt` in module `main` \n\nand want to generate the mixed class `Api.java` into module `main`, we should add an argument `mixin.library` to module `a` and `b` in their build.gradle.kts files:\n\n```kotlin\n// for kapt\nkapt {\n    arguments {\n        arg(\"mixin.library\", \"true\")\n    }\n}\n\n// for ksp\nksp {\n    arg(\"mixin.library\", \"true\")\n}\n```\n\nThe default value is false, so that mixed classes are generated by default. \n\n# Change Logs\n\n## 1.6.0.1\n\n* Generate mixed classes by default.\n* Keep the method order stable.\n\n## 1.6.0.0\n\n* The first release.\n\n# License\n\n[MIT License](https://github.com/bennyhuo/Mixin/blob/master/LICENSE)\n\n    Copyright (c) 2021 Bennyhuo\n    \n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n    \n    The above copyright notice and this permission notice shall be included in all\n    copies or substantial portions of the Software.\n    \n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n    SOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennyhuo%2Fmixin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbennyhuo%2Fmixin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennyhuo%2Fmixin/lists"}