Skip to content

Commit 2ae3704

Browse files
Merge pull request #64 from trlste/patch-1
Update documentation
2 parents 5dcb037 + 7bca5fe commit 2ae3704

File tree

1 file changed

+101
-29
lines changed

1 file changed

+101
-29
lines changed

README.md

+101-29
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,109 @@
1-
# dart_native
1+
# Dart_Native
22

3-
Write native code using Dart. This package liberates you from native code and low performance channel.
3+
Dart_Native operates as both a code generator tool and a bridge to communicate between Dart and native APIs.
44

5-
Still under development!!!
5+
Replaces the low-performing Flutter channel with faster and more concise code.
6+
7+
* Under development
68

79
[![pub package](https://img.shields.io/pub/v/dart_native.svg)](https://pub.dev/packages/dart_native)
810
[![Build Status](https://travis-ci.org/dart-native/dart_native.svg?branch=master)](https://travis-ci.org/dart-native/dart_native)
911

10-
This is the blue part(DartNative Bridge) in the picture below:
12+
This package is the blue part(DartNative Bridge):
1113

1214
![](images/dartnative.png)
1315

14-
## 📲 Requirement
16+
## Requirements
1517

16-
| DartNative Version | Requirements |
18+
| Dart_Native Version | Requirements |
1719
| --- | --- |
1820
| 0.3.0 | Flutter 1.20.0 (Dart 2.9.1) |
1921
| 0.2.0 | Flutter 1.12.13 (Dart 2.7) |
2022

21-
## 🌟 Feature
23+
## Supported Platforms
24+
25+
iOS & Android
26+
27+
## Usage
28+
29+
1. Add ```dart_native``` to dependencies and ```build_runner``` to dev_dependencies.
2230

23-
- [x] Support iOS & Android platform.
24-
- [x] Sync/Async channel with high performance.
25-
- [x] Generate Dart bridging code from native code.
26-
- [x] Automatic object marshalling between Dart and native.
31+
2. Generate Dart wrapper code with [@dartnative/codegen](https://www.npmjs.com/package/@dartnative/codegen) or write Dart code manually.
2732

28-
## 🔮 Getting Started
33+
3. Generate code for automatic type conversion using [dart_native_gen](https://pub.dev/packages/dart_native_gen) with the following steps (3.1-3.3):
34+
35+
3.1 Annotate a Dart wrapper class with `@native`.
36+
```dart
37+
@native
38+
class RuntimeSon extends RuntimeStub {
39+
RuntimeSon([Class isa]) : super(Class('RuntimeSon'));
40+
RuntimeSon.fromPointer(Pointer<Void> ptr) : super.fromPointer(ptr);
41+
}
42+
```
43+
44+
3.2 Annotate your own entry (such as`main()`) with `@nativeRoot`.
45+
46+
```dart
47+
@nativeRoot
48+
void main() {
49+
runApp(App());
50+
}
51+
```
52+
53+
3.3 Run
54+
```bash
55+
flutter packages pub run build_runner build --delete-conflicting-outputs
56+
```
57+
to generate files into your source directory.
58+
59+
Note: we recommend running `clean` first:
60+
61+
```bash
62+
flutter packages pub run build_runner clean
63+
```
64+
65+
4. Call autogenerated function in `<generated-name>.dn.dart` in 3.3. The function name is determined by `name` in `pubspec.yaml`.
66+
67+
```dart
68+
@nativeRoot
69+
void main() {
70+
// Function name is generated by name in pubspec.yaml.
71+
runDartNativeExample();
72+
runApp(App());
73+
}
74+
```
2975
76+
## Features
77+
78+
### High-performance synchronous & asynchronous channeling
79+
80+
Dart_Native costs significantly less time than the Flutter channel and supports both synchronous and asynchronous channeling. A comparison of two native channeling tasks using Flutter channel and Dart_Native is shown below.
81+
82+
Both tasks were executed 10,000 times in the same environment using either Flutter channel or Dart_Native:
83+
84+
| Task | Total time cost (ms) (Channel/Dart_Native) | Channeling time cost (ms) (Channel/Dart_Native) |
85+
| --- | --- | --- |
86+
| Checking if an app needs to be installed | 5202/4166 | 919/99 |
87+
| Logging | 2480/2024 | 1075/432 |
88+
89+
90+
### Autogenerate succinct bridging code
91+
92+
Dart_Native supports automatic type conversion so its bridging code is shorter & simpler than the Flutter channel.
93+
94+
A comparison of the task of "checking if an app needs to be installed" is shown below:
95+
96+
| | # of lines of bridging code | Coding complexity |
97+
| --- | --- | --- |
98+
| Dart_Native | Dart 1 + Native 1 | Autogenerated code returns BOOL directly |
99+
| Channel | Dart 15 + Native 30 | Needs to manually define return format, convert INT to BOOL, determine channel & methodName |
100+
101+
### Automatic object marshalling between Dart and native
102+
103+
## Examples
30104
##### iOS:
31105
32-
Dart code:
106+
Dart code (generated):
33107
34108
```dart
35109
// new Objective-C object.
@@ -46,7 +120,7 @@ CGRect rect = stub.fooCGRect(CGRect(4, 3, 2, 1));
46120
print(rect);
47121
48122
```
49-
Objective-C code:
123+
Corresponding Objective-C code:
50124

51125
```objc
52126
typedef int(^BarBlock)(NSObject *a);
@@ -59,11 +133,11 @@ typedef int(^BarBlock)(NSObject *a);
59133
@end
60134
```
61135
62-
More ios example see: [ios_unit_test.dart](/dart_native/example/lib/ios/unit_test.dart)
136+
More iOS examples see: [ios_unit_test.dart](/dart_native/example/lib/ios/unit_test.dart)
63137
64138
##### Android:
65139
66-
Dart code:
140+
Dart code (generated):
67141
```dart
68142
// new Java object.
69143
RuntimeStub stub = RuntimeStub();
@@ -75,7 +149,7 @@ List list = stub.getList([1, 2, 3, 4]);
75149
stub.setDelegateListener(DelegateStub());
76150
77151
```
78-
Java code:
152+
Corresponding Java code:
79153

80154
```java
81155
public class RuntimeStub {
@@ -92,15 +166,15 @@ public class RuntimeStub {
92166
}
93167
}
94168
```
95-
More android example see: [android_unit_test.dart](/dart_native/example/lib/android/unit_test.dart)
96-
## 📚 Document
169+
More android examples see: [android_unit_test.dart](/dart_native/example/lib/android/unit_test.dart)
170+
## Documentation
97171

98172
### Readme
99173

100174
1. [dart_native README.md](/dart_native/README.md)
101175
2. [dart_native_gen README.md](/dart_native_gen/README.md)
102176

103-
### Blog
177+
### Further reading
104178

105179
- [告别 Flutter Channel,调用 Native API 仅需一行代码!](http://yulingtianxia.com/blog/2020/06/25/Codegen-for-DartNative/)
106180
- [如何实现一行命令自动生成 Flutter 插件](http://yulingtianxia.com/blog/2020/07/25/How-to-Implement-Codegen/)
@@ -112,23 +186,21 @@ More android example see: [android_unit_test.dart](/dart_native/example/lib/andr
112186
- [在 Flutter 中玩转 Objective-C Block](http://yulingtianxia.com/blog/2020/03/28/Using-Objective-C-Block-in-Flutter/)
113187
- [Passing Out Parameter in DartNative](http://yulingtianxia.com/blog/2020/04/25/Passing-Out-Parameter-in-DartNative/)
114188

115-
## 🐒 Q&A
189+
## FAQs
116190

117191
Q: Failed to lookup symbol (dlsym(RTLD_DEFAULT, InitDartApiDL): symbol not found) on iOS archive.
118192

119-
There are two Workarounds:
120-
121-
1. Use dynamic library: Add `use_frameworks!` in Podfile.
122-
2. Select Target Runner -> Build Settings -> Strip Style -> change from "All
123-
Symbols" to "Non-Global Symbols"
193+
A: Select one solution:
194+
1. Use dynamic library: Add `use_frameworks!` in Podfile.
195+
2. Select Target Runner -> Build Settings -> Strip Style -> change from "All Symbols" to "Non-Global Symbols"
124196

125-
## ❤️ Contributed
197+
## Contribution
126198

127199
- If you **need help** or you'd like to **ask a general question**, open an issue.
128200
- If you **found a bug**, open an issue.
129201
- If you **have a feature request**, open an issue.
130202
- If you **want to contribute**, submit a pull request.
131203

132-
## 👮🏻 License
204+
## License
133205

134-
DartNative is available under the BSD 3-Clause License. See the LICENSE file for more info.
206+
DartNative is available under the BSD 3-Clause License. See the LICENSE file for more info.

0 commit comments

Comments
 (0)