{"id":28088901,"url":"https://github.com/didi/unify","last_synced_at":"2025-05-13T12:52:00.038Z","repository":{"id":230174677,"uuid":"751170740","full_name":"didi/Unify","owner":"didi","description":"An efficient, flexible Flutter-Native hybrid communication framework for seamless module integration and automatic code generation.","archived":false,"fork":false,"pushed_at":"2025-04-26T02:32:24.000Z","size":11498,"stargazers_count":285,"open_issues_count":0,"forks_count":20,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-26T02:37:42.480Z","etag":null,"topics":["code-generation","cross-platform","dart","dartlang","flutter","hybrid-app"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/unify_flutter","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/didi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-01T04:14:11.000Z","updated_at":"2025-04-26T02:32:27.000Z","dependencies_parsed_at":"2024-04-12T10:43:46.041Z","dependency_job_id":null,"html_url":"https://github.com/didi/Unify","commit_stats":null,"previous_names":["didi/unify"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2FUnify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2FUnify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2FUnify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/didi%2FUnify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/didi","download_url":"https://codeload.github.com/didi/Unify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253947494,"owners_count":21988940,"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":["code-generation","cross-platform","dart","dartlang","flutter","hybrid-app"],"created_at":"2025-05-13T12:51:59.223Z","updated_at":"2025-05-13T12:52:00.024Z","avatar_url":"https://github.com/didi.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unify: Flutter-Native Hybrid Communication Framework\n\n[中文说明](./README.zh-CN.md)\n\nUnify is an efficient, flexible, and easy-to-use Flutter hybrid development framework designed to solve communication issues between Flutter and native modules. It supports platform-independent module abstraction, flexible implementation injection, automatic code generation, and other features, significantly improving the efficiency of hybrid development and reducing maintenance costs.\n\n\u003e [For relevant introduction, please refer to Didi official account.](https://mp.weixin.qq.com/s/Di8czdY3KCqDAYrzEvePrg)\n\nUnify is developed by Didi's international food delivery team and has been widely used in Didi's international food delivery and international travel business, effectively supporting the Flutter-based development process.\n\nKey features:\n\n- **Platform-independent module abstraction**: Allows developers to declare platform-independent module interfaces and entities using the Dart language.\n- **Flexible implementation injection**: Developers can flexibly choose to inject native implementations (Android/iOS) or Flutter implementations.\n- **Automatic code generation**: With a powerful code generation engine, Unify can automatically generate SDKs for unified calls across Flutter, Android, and iOS platforms.\n\nHere's an example of declaring a native module using Unify:\n\n```dart\n@UniNativeModule()\nabstract class DeviceInfoService {\n  Future\u003cDeviceInfoModel\u003e getDeviceInfo();\n}\n```\n\nThrough Unify, the above Dart interface can be automatically mapped to Android and iOS platforms, and developers only need to focus on the specific implementation on each platform. When used in Flutter, the calling method is as simple and intuitive as a regular Flutter module:\n\n```dart\nDeviceInfoService.getDeviceInfo().then((deviceInfoModel) {\n  print(\"${deviceInfoModel.encode()}\");\n});\n```\n\nThe overall principle of Unify is as follows:\n\n![](doc/public/unify-arch.png)\n\nUnify can effectively solve some common problems in Flutter hybrid development, such as:\n\n- Efficiently importing a large number of native modules into Flutter\n- Efficiently importing a large number of Flutter modules into native code\n- Solving the maintenance issue of a large number of channels\n- Hybrid architecture layering with coexistence of native and Flutter\n\n**Start using Unify now to make hybrid development more efficient!**\n\n## Installation\n\nUnify is a command developed using Dart.\n\nAdd `dev_dependencies` in the `pubspec.yaml` of your Flutter project:\n\n```yaml\ndev_dependencies:\n  unify_flutter: ^3.0.0\n```\n\n\u003e Note: pub.dev: https://pub.dev/packages/unify_flutter\n\nGit dependency:\n\n```yaml\ndev_dependencies:\n  unify_flutter:\n    git: git@github.com:maxiee/Unify.git\n```\n\nRun `flutter pub get` to pull the dependencies. Then you can run Unify:\n\n```sh\nflutter pub run unify_flutter api\n```\n\n\u003e Note: Running the Unify command usually requires a series of parameters. For specific usage, please refer to Getting Started.\n\n## Getting Started\n\nFollow these steps to quickly start using Unify to uniformly encapsulate a native SDK (including Android and iOS versions) and import it into Flutter.\n\n\u003e Reference example code: `example/01_uninativemodule_demo`\n\n### Prerequisites\n\nBefore getting started, make sure your development environment meets the following conditions:\n\n- Flutter 3 or above is installed\n- For Android development, the Android development environment is configured\n- For iOS development, the iOS development environment is configured\n\n### Step 1: Clone the Example Project\n\nFirst, clone the Unify project and enter the example directory:\n\n```sh\ngit clone git@github.com:didi/Unify.git\ncd ./Unify/01_uninativemodule_demo\n```\n\n`01_uninativemodule_demo` is a standard Flutter app project. Its features include:\n\n- The native side (Android/iOS) implements a system information module respectively\n- Use Unify to uniformly encapsulate native modules and import them into Flutter\n- Perform unified calls on the Flutter side\n\n### Step 2: Declare Unify Module\n\nNotice that there is an `interface` directory in the project root, which is where Unify modules are declared. It contains two files:\n\n1. `device_info_service.dart` - Declares the native module:\n\n```dart\n// device_info_service.dart\n@UniNativeModule()\nabstract class DeviceInfoService {\n  /// Get device information\n  Future\u003cDeviceInfoModel\u003e getDeviceInfo();\n}\n```\n\nThe `@UniNativeModule` annotation indicates that the implementation of this module is provided by the native side.\n\n2. `device_info_model.dart` - Declares the return value model:\n\n```dart\n// device_info_model.dart\n@UniModel()\nclass DeviceInfoModel {\n  /// Operating system version\n  String? osVersion;\n\n  /// Memory information\n  String? memory;\n\n  /// Device model\n  String? platform;\n}\n```\n\nThe `@UniModel` annotation indicates that this is a cross-platform data model.\n\n### Step 3: Generate Cross-Platform Code\n\nAfter completing the interface declaration, execute the following command to generate cross-platform code:\n\n```sh\nflutter pub run unify_flutter api\\\n  --input=`pwd`/interface \\\n  --dart_out=`pwd`/lib \\\n  --java_out=`pwd`/android/src/main/java/com/example/uninativemodule_demo \\\n  --java_package=com.example.uninativemodule_demo \\\n  --oc_out=`pwd`/ios/Classes \\\n  --dart_null_safety=true \\\n  --uniapi_prefix=UD\n```\n\nCommand option description:\n\n| Parameter          | Description                                                | Required |\n|--------------------|-----------------------------------------------------------|----------|\n| `input`            | Specifies the Unify interface declaration directory          | Y        |\n| `dart_out`         | Specifies the Dart code output directory                    | Y        |\n| `java_out`         | Specifies the Java code output directory                    | Android  |\n| `java_package`     | Specifies the package name of the generated Java code       | Android  |\n| `oc_out`           | Specifies the Objective-C code output directory             | iOS      |\n| `dart_null_safety` | Whether to generate null-safe Dart code                     | Y        |\n| `uniapi_prefix`    | Generated code prefix to avoid conflicts between libraries | N        |\n\nAfter execution, Unify generates code for each platform in the corresponding directory.\n\n### Step 4: Implement Native Modules\n\nWe need to provide the specific implementation for the generated native module interfaces:\n\n- Android platform implementation class: [DeviceInfoServiceImpl.java](https://github.com/didi/Unify/blob/master/example/01_uninativemodule_demo/example/android/app/src/main/java/com/example/uninativemodule_demo_example/DeviceInfoServiceImpl.java)\n- Android platform implementation registration: [MainActivity.java](https://github.com/didi/Unify/blob/master/example/01_uninativemodule_demo/example/android/app/src/main/java/com/example/uninativemodule_demo_example/MainActivity.java)\n\n- iOS platform implementation class: [DeviceInfoServiceVendor.h](https://github.com/didi/Unify/blob/master/example/01_uninativemodule_demo/example/ios/Runner/DeviceInfoServiceVendor.h), [DeviceInfoServiceVendor.m](https://github.com/didi/Unify/blob/master/example/01_uninativemodule_demo/example/ios/Runner/DeviceInfoServiceVendor.m)\n- iOS platform implementation registration: [AppDelegate.m](https://github.com/didi/Unify/blob/master/example/01_uninativemodule_demo/example/ios/Runner/AppDelegate.m)\n\nYou can refer to the example code for implementation.\n\n### Step 5: Call in Flutter\n\nEverything is ready! In the Flutter code, you can now directly call the native module encapsulated by Unify:\n\n```dart\nOutlinedButton(\n  child: const Text(\"Get Device Info\"),\n  onPressed: () {\n    DeviceInfoService.getDeviceInfo().then((deviceInfoModel) {\n      setState(() {\n        _platformVersion = \"\\n${deviceInfoModel.encode()}\";\n      });\n    });\n  },\n),\n```\n\n![](./doc/public/unify-demo.png)\n\nAt this point, you have successfully imported a native module using Unify and used it in Flutter. It's as simple and intuitive as calling a Flutter module!\n\n### Summary\n\nThrough this example, we experienced the value brought by Unify:\n\n1. `Unified Module Declaration`: Unified module interface declaration on any platform to avoid inconsistent implementations\n2. `UniModel`: Supports cross-platform transparent data model transmission\n3. Compared to Flutter's native channel approach:\n    1. Avoids errors caused by manual parameter parsing\n    2. Automatic alignment between Android and iOS\n    3. Automatic generation of a large number of channels, easy to maintain\n    4. Seamless serialization of complex entities, reducing management costs\n\n## Decision Tree\n\nWe have summarized the following decision-making process:\n\n![](./doc/public/unify-decision-tree.png)\n\n## More Examples\n\nIn Getting Started, we provided the most basic and commonly used scenario of importing native implementations into Flutter. Unify's capabilities go far beyond that. From simple single SDK encapsulation to complex enterprise-level app large-scale module export, Unify can support it all.\n\nWe introduce these typical scenarios and business models through example applications:\n\n| Case                                                                                                | Description                                                                                                  | Applicable Scenario                                                                                                        |\n|-----------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|\n| [01_uninativemodule_demo](https://github.com/didi/Unify/tree/master/example/01_uninativemodule_demo) | UniNativeModule demo                                                                                        | How to efficiently import a native module (Android/iOS dual-end implementation) into Flutter and achieve unified calling |\n| [02_unifluttermodule_demo](https://github.com/didi/Unify/tree/master/example/02_unifluttermodule_demo) | UniFlutterModule demo                                                                                       | How to efficiently import a Flutter module into native (Android/iOS) and achieve unified calling                        |\n\n## Documentation\n\nFor more advanced usage, please refer to the detailed documentation.\n\n* For documentation, please refer to [Unify Documentation](doc/README.md).\n* To quickly experience how to use it, please refer to [Getting Started](doc/02.快速开始/README.md).\n* To understand the capabilities provided by Unify, please refer to [Basic Capabilities](doc/06.基础能力/README.md).\n* To understand the design principles of Unify modules, please refer to [Principle Overview](doc/08.原理概述/README.md).\n* For more usage instructions of Unify CLI, please refer to [CLI Usage Tutorial](doc/04.CLI使用教程.md).\n\n\u003e Note: We are also actively organizing the documentation. If you have any questions regarding usage or understanding, please feel free to submit an issue for feedback and discussion!\n\n## WeChat Communication Group\n![](doc/public/wx.png)\n\n## License\n\n\u003cimg alt=\"Apache-2.0 license\" src=\"https://www.apache.org/img/ASF20thAnniversary.jpg\" width=\"128\"\u003e\n\nUnify is distributed and used under the Apache-2.0 license. For more information, see the [License File](LICENSE).\n\n## Members\n\nR\u0026D Team:\n\n[maxiee](https://github.com/maxiee),\n[zhugeafanti](https://github.com/zhugeafanti),\n[piglet696](https://github.com/piglet696),\n[zhaoxiaochun](https://github.com/zhaoxiaochun),\n[ChengCheng-Hello](https://github.com/ChengCheng-Hello),\n[windChaser618](https://github.com/windChaser618),\n[bql88601485](https://github.com/bql88601485),\n[newbiechen1024](https://github.com/newbiechen1024),\n[xizhilang66](https://github.com/xizhilang66),\n[UCPHszf](https://github.com/UCPHszf),\n[QianfeiSir](https://github.com/QianfeiSir),\n[jiawei1203](https://github.com/jiawei1203),\n[Whanter](https://github.com/Whanter)\n\n## Contribution\n\nIf you have any questions regarding usage or understanding, please feel free to submit an issue for feedback and discussion!\n\nYour communication and contributions are welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdidi%2Funify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdidi%2Funify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdidi%2Funify/lists"}