Skip to content

Commit

Permalink
feat: Update Flutter Rust bridge to V2
Browse files Browse the repository at this point in the history
  • Loading branch information
4o3F committed Jan 18, 2024
1 parent a2e34f0 commit cf14a90
Show file tree
Hide file tree
Showing 18 changed files with 808 additions and 276 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.flutter-plugins-dependencies
.idea/
build/
.dart_tool/
.dart_tool/
rust_builder
30 changes: 0 additions & 30 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,3 @@ flutter {
}

dependencies {}

[
new Tuple2('Debug', ''),
new Tuple2('Profile', '--release'),
new Tuple2('Release', '--release')
].each {
def taskPostfix = it.first
def profileMode = it.second
tasks.whenTaskAdded { task ->
if (task.name == "javaPreCompile$taskPostfix") {
task.dependsOn "cargoBuild$taskPostfix"
}
}
tasks.register("cargoBuild$taskPostfix", Exec) {
// Until https://github.com/bbqsrc/cargo-ndk/pull/13 is merged,
// this workaround is necessary.

def ndk_command = """cargo ndk \
-t armeabi-v7a -t arm64-v8a -t x86_64 -t x86 \
-o ../android/app/src/main/jniLibs build $profileMode"""

workingDir "../../native"
environment "ANDROID_NDK_HOME", "$ANDROID_NDK"
if (org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.currentOperatingSystem.isWindows()) {
commandLine 'cmd', '/C', ndk_command
} else {
commandLine 'sh', '-c', ndk_command
}
}
}
2 changes: 2 additions & 0 deletions flutter_rust_bridge.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rust_input: rust/src/api/**/*.rs
dart_output: lib/src/rust
14 changes: 14 additions & 0 deletions integration_test/simple_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ascent/main.dart';
import 'package:ascent/src/rust/frb_generated.dart';
import 'package:integration_test/integration_test.dart';

void main() {
if (kIsWeb) IntegrationTestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() async => await RustLib.init());
testWidgets('Can call rust function', (WidgetTester tester) async {
await tester.pumpWidget(const MyApp());
expect(find.textContaining('Result: `Hello, Tom!`'), findsOneWidget);
});
}
2 changes: 1 addition & 1 deletion lib/ffi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ final _dylib = io.Platform.isWindows ? '$_base.dll' : 'lib$_base.so';

final Native api = NativeImpl(io.Platform.isIOS || io.Platform.isMacOS
? DynamicLibrary.executable()
: DynamicLibrary.open(_dylib));
: DynamicLibrary.open(_dylib));
2 changes: 1 addition & 1 deletion lib/global_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ class GlobalState {

rootEnabled.value = await Root.isRooted() ?? false;
}
}
}

0 comments on commit cf14a90

Please sign in to comment.