fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
4.3k stars 301 forks source link

Clearer instruction for Android config is needed #2242

Closed dbsxdbsx closed 3 months ago

dbsxdbsx commented 3 months ago

Issue

With frb v2.2.0, I inited a new frb project following https://cjycode.com/flutter_rust_bridge/quickstart, The new project is ok fro building for windows, but for android, it failed with

Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
INFO: Precompiled binaries are disabled
INFO: Building rust_lib_tst_frb for x86_64-linux-android
INFO: Installing NDK 23.1.7779620
SEVERE: ================================================================================
SEVERE: Cargokit BuildTool failed with error:
SEVERE: --------------------------------------------------------------------------------
SEVERE: External Command: C:\Users\Administrator\AppData\Local\Android\Sdk\cmdline-tools\latest\bin\sdkmanager.bat "--install" "ndk;23.1.7779620"
SEVERE: Returned Exit Code: 1
SEVERE: --------------------------------------------------------------------------------
SEVERE: STDOUT:
SEVERE: Loading package information...
Loading local repository...
[                                       ] 3% Loading local repository...        
[                                       ] 3% Fetch remote repository...
[=                                      ] 3% Fetch remote repository...
[=                                      ] 4% Fetch remote repository...
[=                                      ] 5% Fetch remote repository...
[==                                     ] 5% Fetch remote repository...
[==                                     ] 6% Fetch remote repository...
[==                                     ] 7% Fetch remote repository...
[==                                     ] 7% Computing updates...
[===                                    ] 8% Computing updates...
[===                                    ] 10% Computing updates...
[===                                    ] 10% Installing NDK (Side by side) 23.1
[===                                    ] 10% Downloading android-ndk-r23b-windo
[====                                   ] 10% Downloading android-ndk-r23b-windo
[====                                   ] 11% Downloading android-ndk-r23b-windo
[====                                   ] 12% Downloading android-ndk-r23b-windo
[=====                                  ] 13% Downloading android-ndk-r23b-windo
[=====                                  ] 14% Downloading android-ndk-r23b-windo
[=====                                  ] 15% Downloading android-ndk-r23b-windo
[======                                 ] 15% Downloading android-ndk-r23b-windo
[======                                 ] 16% Downloading android-ndk-r23b-windo
[============                           ] 33% Downloading android-ndk-r23b-windo
[============                           ] 33% Unzipping...

[============                           ] 33% Unzipping...
SEVERE: --------------------------------------------------------------------------------
SEVERE: STDERR:
SEVERE: Warning: Observed package id 'emulator' in inconsistent location 'C:\Users\Administrator\AppData\Local\Android\Sdk\emulator.backup' (Expected 'C:\Users\Administrator\AppData\Local\Android\Sdk\emulator')
SEVERE: Warning: Already observed package id 'emulator' in 'C:\Users\Administrator\AppData\Local\Android\Sdk\emulator'. Skipping duplicate at 'C:\Users\Administrator\AppData\Local\Android\Sdk\emulator.backup'
SEVERE: Warning: Observed package id 'emulator' in inconsistent location 'C:\Users\Administrator\AppData\Local\Android\Sdk\emulator.backup' (Expected 'C:\Users\Administrator\AppData\Local\Android\Sdk\emulator')
SEVERE: Warning: Already observed package id 'emulator' in 'C:\Users\Administrator\AppData\Local\Android\Sdk\emulator'. Skipping duplicate at 'C:\Users\Administrator\AppData\Local\Android\Sdk\emulator.backup'
SEVERE: Warning: An error occurred while preparing SDK package NDK (Side by side) 23.1.7779620: Error on ZipFile unknown archive.
SEVERE: --------------------------------------------------------------------------------
SEVERE: #0      runCommand (package:build_tool/src/util.dart:121:5)
SEVERE: #1      AndroidEnvironment.installNdk (package:build_tool/src/android_environment.dart:76:5)
SEVERE: #2      RustBuilder._buildEnvironment (package:build_tool/src/builder.dart:193:13)
SEVERE: #3      RustBuilder.build (package:build_tool/src/builder.dart:160:26)
SEVERE: #4      ArtifactProvider.getArtifacts (package:build_tool/src/artifacts_provider.dart:71:39)
SEVERE: <asynchronous suspension>
SEVERE: #5      BuildGradle.build (package:build_tool/src/build_gradle.dart:35:23)
SEVERE: <asynchronous suspension>
SEVERE: #6      BuildGradleCommand.runBuildCommand (package:build_tool/src/build_tool.dart:65:5)
SEVERE: <asynchronous suspension>
SEVERE: #7      BuildCommand.run (package:build_tool/src/build_tool.dart:37:5)
SEVERE: <asynchronous suspension>
SEVERE: #8      CommandRunner.runCommand (package:args/command_runner.dart:212:13)
SEVERE: <asynchronous suspension>
SEVERE: #9      runMain (package:build_tool/src/build_tool.dart:251:5)
SEVERE: <asynchronous suspension>
SEVERE: --------------------------------------------------------------------------------
SEVERE: BuildTool arguments: [build-gradle]
SEVERE: ================================================================================
Running Gradle task 'assembleDebug'...                             65.9s
√ Built build\app\outputs\flutter-apk\app-debug.apk
E/flutter ( 4825): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Failed to load dynamic library 'librust_lib_tst_frb.so': dlopen failed: library "librust_lib_tst_frb.so" not found        
E/flutter ( 4825): #0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:43)
E/flutter ( 4825): #1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:22:12)
E/flutter ( 4825): #2      new ExternalLibrary.open (package:flutter_rust_bridge/src/platform_types/_io.dart:42:47)  
E/flutter ( 4825): #3      loadExternalLibraryRaw (package:flutter_rust_bridge/src/loader/_io.dart:56:28)
E/flutter ( 4825): #4      loadExternalLibrary (package:flutter_rust_bridge/src/loader/_io.dart:14:10)
E/flutter ( 4825): #5      BaseEntrypoint._loadDefaultExternalLibrary (package:flutter_rust_bridge/src/main_components/entrypoint.dart:129:13)
E/flutter ( 4825): #6      BaseEntrypoint.initImpl (package:flutter_rust_bridge/src/main_components/entrypoint.dart:48:31)
E/flutter ( 4825): #7      RustLib.init (package:tst_frb/rust/frb_generated.dart:27:20)
E/flutter ( 4825): #8      main (package:tst_frb/main.dart:6:17)
E/flutter ( 4825): #9      _runMain.<anonymous closure> (dart:ui/hooks.dart:301:23)
E/flutter ( 4825): #10     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
E/flutter ( 4825): #11     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
E/flutter ( 4825):
Syncing files to device sdk gphone64 x86 64...                      62ms

Flutter run key commands.
r Hot reload.
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on sdk gphone64 x86 64 is available at: http://127.0.0.1:10710/3qo6Q4q3Hbc=/
D/ProfileInstaller( 4825): Installing profile for com.example.tst_frb

after flutter run.

I guess it is somewhat related to the android config file android\app\build.gradle with default content:

plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id "dev.flutter.flutter-gradle-plugin"
}

android {
    namespace = "com.example.tst_frb"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "com.example.tst_frb"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig = signingConfigs.debug
        }
    }
}

flutter {
    source = "../.."
}

And from the log, it is easy to see that it is issued with NDK (Side by side) 23.1, while I did install NDK and put the NDK path in C:\Users\Administrator\.gradle\gradle.properties with content ANDROID_NDK=C:\Users\Administrator\AppData\Local\Android\Sdk\ndk. And in this path, the NDK 27.0.12077973 is already installed. So I now strongly suspect that this compiling failed issue is due to the wrong config for the default build.gradle content. And after checking doc, I still don't know how to integrate what it said into the default build.gradle.

What I want

For Frb v2, I hope the default built build.gradle could be refined so that the issue won't occur. And I hope the doc for the Android config could be refined so that users won't be at a loss--- currently, except for the one built defaultly, and the template one are different. And I've also checked other build.gradle from other users, they are usually different, by which at least puts me at a loss.

fzyzcjy commented 3 months ago

frb v2 uses Cargokit by default, and it should work without any extra configuration (indeed the CI has such test IIRC and it works well). I guess your question is not related to frb core, but related to Cargokit, thus it would be great to ask there.

dbsxdbsx commented 3 months ago

I guess your question is not related to frb core, but related to Cargokit,

But what I tested is almost the new frb project initialized by command flutter_rust_bridge_codegen create my_app, I only added little things for the Cargo.toml:

[package]
name = "rust_lib_tst_frb"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "staticlib"]

[profile.release]
lto = true
codegen-units = 1
strip = "debuginfo"

[profile.dev.package."*"]
opt-level = 3

[dev-dependencies]

[dependencies]
flutter_rust_bridge = "2.2.0"

Indeed, I did got similar issue before which is due to some rust crate not compatible for Android, but this times it seems not to due to this reason.

dbsxdbsx commented 3 months ago

I forgot to mention that, it is a run time panic issue: image

fzyzcjy commented 3 months ago

I only added little things for the Cargo.toml

Have you tried to create a blank project without any modifications - does it work or not?

fzyzcjy commented 3 months ago

Oh I checked the error logs

SEVERE: Warning: An error occurred while preparing SDK package NDK (Side by side) 23.1.7779620: Error on ZipFile unknown archive.

Looks like something related to NDK setup, maybe google such errors and see whether there are some solutions

dbsxdbsx commented 3 months ago

I only added little things for the Cargo.toml

Have you tried to create a blank project without any modifications - does it work or not?

Not worked, just as what I mentioned. But maybe you are right for that it is related to cargokit, which deals NDK automatically.

My Dart SDK version: 3.5.0 and Rust is 1.80.0.

dbsxdbsx commented 3 months ago

Now this issue is worked around by manually installing NDK 23.1.7779620 from Android Studio, though I don't know why cargokit would insist on installing this version when there is an upper version already installed.

fzyzcjy commented 3 months ago

I don't know as well... Reporting this to cargokit may be helpful.

wxitcode commented 3 months ago

this line #L63

    public final String ndkVersion = "23.1.7779620"

You can specify the version of NDK instead of using the default NDK version of the Flutter tool.

android/app/build.gradle

...
android {
     ...
     compileSdk = flutter.compileSdkVersion
     // ndkVersion = flutter.ndkVersion
     ndkVersion = "26.1.10909125"
...
github-actions[bot] commented 2 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new issue.