dna
中文文档
👉
相关文章
dart native access. A lightweight dart to native super channel plugin, You can use it to invoke any native code directly in dart code.
Supported Platform(Language):
- iOS(Objective-C)
- Android(Java)
The primary scenario:
- Implement some simple channels directly in dart code;
- Native code that are calling using dna can also be hot-reloaded.
Add dependency
-
Add folllowing code to the pubspec.yaml file in your flutter project:
dependencies: dna: git:[email protected]:Assuner-Lee/dna.git
Reference: https://flutter.dev/docs/development/packages-and-plugins/using-packages
-
import header file in dart code:
import 'package:dna/dna.dart';
-
add gradle dependency in Android project:
implementation 'me.ele:dna-annotations:1.2.0' annotationProcessor 'me.ele:dna-compiler:1.2.0'
-
add following conconfiguration in Android project's proguard-rules
-keep class **.Dna_Class_Proxy { *; } -keep class me.ele.dna_compiler.** { *; } -keep class me.ele.dna.** { *; }
Usage
Main class
-
NativeContext
: You can use it to describe Native code by Dart code, then callcontext.execute()
to execute the final Native code on associated platform and get the returned value. -
NativeObject
: Used to identify the native variable. The callerNativeObject
can call theinvoke
method to pass in the method name and the parameter array args list in the context of theNativeContext
to get the return valueNativeObject
object.
The API of NativeContext
is consistent. Now we will make a detailed introduction for call ObjC using ObjCContext
, Then call Java using JAVAContext
.
Call ObjC using Dart
ObjCContext
is the final executor on iOS platform.
Context call supported
Returned value as caller
ObjC code
NSString *versionString = [[UIDevice currentDevice] systemVersion];
// Return versionString using fluter channel
Dart code
ObjCContext context = ObjCContext();
NativeObject UIDevice = context.classFromString('UIDevice');
NativeObject device = UIDevice.invoke(method: 'currentDevice');
NativeObject version = device.invoke(method: 'systemVersion');
context.returnVar = version; // Can be omitted, See:Quick use of instantiated objects in JSON supported
// Get native execution results directly
var versionString = await context.execute();
Returned value as parameters
ObjC code
NSString *versionString = [[UIDevice currentDevice] systemVersion];
NSString *platform = @"iOS-";
versionString = [platform stringByAppendingString: versionString];
// Return versionString using fluter channel
Dart code
ObjCContext context = ObjCContext();
NativeClass UIDevice = context.classFromString('UIDevice');
NativeObject device = UIDevice.invoke(method: 'currentDevice');
NativeObject version = device.invoke(method: 'systemVersion');
NativeObject platform = context.classFromString("NSString").invoke(method: 'stringWithString:', args: ['iOS-']);
version = platform.invoke(method: 'stringByAppendingString:', args: [version]);
context.returnVar = version; // Can be omitted, See:Quick use of instantiated objects in JSON supported
// Get native execution results directly
var versionString = await context.execute();
Chaining calls supported
ObjC code
NSString *versionString = [[UIDevice currentDevice] systemVersion];
versionString = [@"iOS-" stringByAppendingString: versionString];
// Return versionString using fluter channel
Dart code
ObjCContext context = ObjCContext();
NativeObject version = context.classFromString('UIDevice').invoke(method: 'currentDevice').invoke(method: 'systemVersion');
version = context.classFromString("NSString").invoke(method: 'stringWithString:', args: ['iOS-']).invoke(method: 'stringByAppendingString:', args: [version]);
context.returnVar = version; // Can be omitted, See:Quick use of instantiated objects in JSON supported
// Get native execution results directly
var versionString = await context.execute();
Something about the final returned value of the
context
context.returnVar
is the marker of the final returned value ofcontext
.
- When setting
context.returnVar
, you can get the Native variable corresponding to theNativeObject
;- Without setting
context.returnVar
, execute to the lastinvoke
, if there is a return value, it will be the final returned value ofcontext
; if not, it will return anull
value.
ObjCContext context = ObjCContext(); context.classFromString('UIDevice').invoke(method: 'currentDevice').invoke(method: 'systemVersion'); // Get native execution results directly var versionString = await context.execute();
Quick use of instantiated objects in JSON supported
Sometimes, we need to directly instantiate an object with JSON
.
ObjC code
ClassA *objectA = [ClassA new];
objectA.a = 1;
objectA.b = @"sss";
Dart code
One way
ObjCContext context = ObjCContext();
NativeObject objectA = context.classFromString('ClassA').invoke(method: 'new');
objectA.invoke(method: 'setA:', args: [1]);
objectA.invoke(method: 'setB:', args: ['sss']);
The other way
ObjCContext context = ObjCContext();
NativeObject objectA = context.newNativeObjectFromJSON({'a':1,'b':'sss'}, 'ClassA');
Call Java using Dart
JAVAContext
is the final executor on Android platform, it has all the fetures that ObjCContext
have.
- Context call supported;
- Chaining calls supported;
- Quick use of instantiated objects in JSON supported.
In addition, it additionally supports the instantiation of an object from the constructor.
The instantiation of an object from the constructor supported
Java code
String platform = new String("android");
Dart code
NativeObject version = context
.newJavaObjectFromConstructor('java.lang.String', ["android "])
Fast organization of dual platform code
We provide you with a quick way to initialize and execute context:
static Future