How to integrate Flutter app's build process with Rust code? i.e. when building Flutter code, how to automatically build its Rust code?

887

Android

Add the following to your build.gradle

[
        new Tuple2('Debug', '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") {
            println "hello let ${task.name} dependsOn cargoBuild$taskPostfix"
            task.dependsOn "cargoBuild$taskPostfix"
        }
    }
    tasks.register("cargoBuild$taskPostfix", Exec) {
        println "hello executing doLast for task cargoBuild$taskPostfix"
        commandLine 'sh', '-c', "build-your-rust-code"
    }
}

where build-your-rust-code is the command you use to build your Rust code. (For example, I use fastlane to wrap it (of course you can use others) and I put that in a rust folder, so I just call cd $projectDir/../rust/fastlane && bundle exec fastlane build_android profile:$profileMode.)

iOS

Method 1

The most manual way is as follows. Go to Build Phases, click + button and choose New Run Script Phase. Then enter the script you want to run to build your Rust code. (For example, for myself, I use Shell /bin/sh and call some fastlane commands which executes the actual commands.)

enter image description here

Method 2

A more automated approach is to use the CocoaPod files, which will automatically help you set up everything. For example, I add:

  s.script_phase = {
      :name => 'Compile Rust',
      :script => your-script-to-compile-it,
      :execution_position => :before_compile,
      :shell_path => '/bin/sh'
   }

(For example, my script is %q{env -i HOME="$HOME" PROJECT_DIR="$PROJECT_DIR" CONFIGURATION="$CONFIGURATION" /bin/bash -c 'source ~/.bashrc && cd $PROJECT_DIR/../.symlinks/plugins/vision_utils/rust/fastlane && LC_ALL= LANG=en_US.UTF-8 bundle exec fastlane build_ios profile:$CONFIGURATION'}. Notice that, at least in my case, I have to create a brand new shell. Otherwise the opencv in my Rust complains and fails to compile. But you may not have this problem if your Rust code does not contain c++ things.)

(Q&A style answer in order to help people who face the same situation as me, especially when not familiar with Android and iOS)

Share:
887
ch271828n
Author by

ch271828n

Hello, world :)

Updated on January 01, 2023

Comments

  • ch271828n
    ch271828n over 1 year

    I have a Flutter application that also has some Rust code, and they communicate via flutter_rust_bridge. There are already some tutorials talking about how to build such an app. However, they require me to manually trigger cargo build (or similar commands) and flutter run separately. You know, this is quite cumbersome, and if I forget to execute cargo, or that command fails, I run the risk of building a Flutter app with an old binary. Therefore, I wonder how I can automate that process. Thanks!