在Android上构建RustDesk

2024-07-23 189

如果大家已经熟悉安卓NDK、Flutter和Rust。本篇教程主要指导大家如何在Android上构建RustDesk,以便大家能够成功构建应用程序,并使自己能够通过Android Studio等方式处理代码。

一、环境准备

1、使用Ubuntu 20.04或更高版本。

2、拥有有效的互联网连接。

3、安装Flutter、Rust和vcpkg。

4、克隆RustDesk存储库。

二、在Android上构建RustDesk

1、生成Flutter Rust Bridge

由于RustDesk基于Flutter Rust Bridge,首先需要生成它。在rustdesk目录下运行以下命令:

cd rustdesk

可以通过运行以下命令来实现克隆RustDesk 存储库:

git clone https://github.com/rustdesk/rustdesk

将能够将rustdesk目录更改为:

cd rustdesk

如果是第一次执行此操作,还需要安装一些必备组件:

sudo apt install ca-certificates -y
sudo apt update -y
sudo apt install -y g++ gcc git curl wget nasm yasm libgtk-3-dev clang cmake libclang-dev ninja-build llvm-dev libclang-10-dev llvm-10-dev pkg-config rustfmt

2、安装依赖项

安装必要的依赖项,包括Android开发工具和RustDesk构建所需的库:

cargo install flutter_rust_bridge_codegen
pushd flutter && flutter pub get && popd

3、生成Flutter Rust Bridge文件

下载并解压vcpkg依赖项到指定目录(如/opt),确保vcpkg目录位于正确的位置,以便构建过程可以正确引用。

~/.cargo/bin/flutter_rust_bridge_codegen --rust-input ./src/flutter_ffi.rs --dart-output ./flutter/lib/generated_bridge.dart

成功后会在终端窗口中看到如下内容:

2023/02/27 20:44:39 [INFO] Success!
2023/02/27 20:44:39 [INFO] Now go and use it :)

三、构建适用于Android的RustDesk

1、安装依赖项

在此步骤中需要一些额外的依赖项,可以通过运行以下命令来安装它们:

sudo apt update -y
sudo apt-get -qq install -y git curl wget nasm yasm libgtk-3-dev clang libxcb-randr0-dev libxdo-dev libxfixes-dev libxcb-shape0-dev libxcb-xfixes0-dev libasound2-dev libpulse-dev cmake libclang-dev ninja-build libappindicator3-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libvdpau-dev libva-dev libclang-dev llvm-dev libclang-10-dev llvm-10-dev pkg-config tree g++ libc6-dev gcc-multilib g++-multilib openjdk-11-jdk-headless

需要激活ffigen:

dart pub global activate ffigen 5.0.1

2、下载其他依赖项

pushd /opt
sudo wget https://github.com/rustdesk/doc.rustdesk.com/releases/download/console/dep.tar.gz
sudo tar xzf dep.tar.gz
popd

可能出现的错误:

error: failed to run custom build command for `magnum-opus v0.4.0 (https://github.com/rustdesk/magnum-opus#79be072c)`

Caused by:
process didn't exit successfully: `/home/user/rustdesk/target/release/build/magnum-opus-05dc0023b86da8fc/build-script-build` (exit status: 101)
--- stdout
cargo:info=arm64-android
cargo:rustc-link-lib=static=opus
cargo:rustc-link-search=/home/user/vcpkg/installed/arm64-android/lib
cargo:include=/home/user/vcpkg/installed/arm64-android/include
rerun-if-changed=/home/user/.cargo/git/checkouts/magnum-opus-4bb999f3bcbf6ab0/79be072/opus_ffi.h
rerun-if-changed=/home/user/vcpkg/installed/arm64-android/include

考虑在主目录中安装,然后运行:

pushd $HOME
sudo wget https://github.com/rustdesk/doc.rustdesk.com/releases/download/console/dep.tar.gz
sudo tar xzf dep.tar.gz
popd

3、禁用Flutter Rust Bridge

sed -i "s/gen_flutter_rust_bridge();/\/\//g" build.rs

4、构建RustDesk库

rustup target add aarch64-linux-android

生成文件:

cargo install cargo-ndk

现在需要运行ndk_arm64.sh脚本为Flutter构建Rust的代码,设置ANDROID_NDK_HOME变量并检查它是否有效。

然后运行:

./flutter/ndk_arm64.sh

5、将生成的库移动到jniLibs目录中

mkdir -p ./flutter/android/app/src/main/jniLibs/arm64-v8a

复制:

cp ./target/aarch64-linux-android/release/liblibrustdesk.so ./flutter/android/app/src/main/jniLibs/arm64-v8a/librustdesk.so

6、构建RustDesk

接下来将构建RustDesk apk版本。

首先需要下载:

pushd flutter
sudo wget -O so.tar.gz https://github.com/rustdesk/doc.rustdesk.com/releases/download/console/so.tar.gz

然后打开包装:

tar xzvf so.tar.gz
popd
sed -i "s/signingConfigs.release/signingConfigs.debug/g" ./flutter/android/app/build.gradle

将文件复制到正确的目录中:

mkdir -p ./flutter/android/app/src/main/jniLibs/arm64-v8a
cp ./target/aarch64-linux-android/release/liblibrustdesk.so ./flutter/android/app/src/main/jniLibs/arm64-v8a/librustdesk.so

成功构建Flutter:

pushd flutter
flutter build apk --release --target-platform android-arm64 --split-per-abi
  • 广告合作

  • QQ群号:707632017

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。