Ente 移动应用自托管构建指南:使用 --dart-define 连接自定义服务器
【免费下载链接】ente💚 End-to-end encrypted cloud for everything.项目地址: https://gitcode.com/GitHub_Trending/en/ente
导读
本文是 Ente 自托管文档中《Building mobile apps》的完整实战指南,讲解如何从源码构建 Ente Photos 与 Ente Auth 移动应用,并通过--dart-define=endpoint参数让应用连接到你自己部署的服务器(而非官方api.ente.com)。读完本文,你将掌握从环境准备、源码构建到 release 包产出的完整流程,并理解 endpoint 参数在客户端源码中的解析机制。
一、核心原理:endpoint参数如何生效
自托管用户最大的诉求是:把应用指向自己的服务器。在 Ente 移动端,这个诉求通过 Flutter 的--dart-define编译期参数实现,其底层由 endpoint_config.dart 解析:
static const defaultEndpoint = String.fromEnvironment( "endpoint", defaultValue: kDefaultProductionEndpoint, );即:构建时传入--dart-define=endpoint=http://localhost:8080,该值会被编译进应用;若不传,则回退到生产默认值。生产默认值定义在 constants.dart:
const kDefaultProductionEndpoint = 'https://api.ente.com'; const kLegacyProductionEndpoint = 'https://api.ente.io';EndpointConfig还提供了运行时覆盖能力:存储在SharedPreferences的endpoint键会优先于编译期默认值(详见EndpointConfig.endpointgetter),这也是后文"开发设置"中手动填 endpoint 的原理。isProduction则依据 endpoint 是否等于生产值来判断当前构建是否连接官方服务。
二、环境准备
构建 Ente 移动应用前,请确认以下依赖:
- Flutter v3.47.2:Ente Photos 与 Ente Auth 的 README 均指定此版本(见 photos/README.md 与 auth/README.md);
- Rust 工具链:Photos 应用依赖 Rust 生成的 FRB 绑定(
rust/bindings/frb/); - Git submodule:Auth 应用构建前需初始化子模块。
Ente 采用 Flutter workspace 组织多应用,mobile/pubspec.yaml 中声明了 Dart SDK 版本约束sdk: ">=3.10.0 <4.0.0",并将apps/auth、apps/locker、apps/photos及大量内部包纳入同一个 workspace。因此依赖安装需在工作区内执行:
flutter pub get --enforce-lockfile说明:
--enforce-lockfile要求严格按pubspec.lock解析依赖,保证与官方 CI 构建一致。
对 Photos 应用,若 FRB 导出面(rust/bindings/frb/)有变更,还需重新生成绑定:
(cd "$(git rev-parse --show-toplevel)/rust" && cargo codegen frb)三、构建 Photos 应用并连接自托管服务器
在 mobile/apps/photos/README.md 的指导下,完整流程如下:
cd ente/mobile flutter pub get # Android flutter run --dart-define=endpoint=http://localhost:8080 --flavor independent --debug -t lib/main.dart # iOS flutter run --dart-define=endpoint=http://localhost:8080关键点拆解:
--dart-define=endpoint=http://localhost:8080:将 endpoint 编译进应用,替换默认的https://api.ente.com。http://localhost:8080是本地开发服务器的典型地址,实际部署时应替换为你的服务器地址(局域网 IP 或域名,如http://192.168.1.100:8080);--flavor independent(Android):必须指定 flavor。从 android/app/build.gradle 可见 Photos 定义了independent、dev、playstore、fdroid四个 flavor,其中independent对应 GitHub Releases 中的独立发行版(applicationIdSuffix ".independent");-t lib/main.dart:显式指定入口文件。
iOS 构建无需 flavor,flutter run即可。仓库中的性能测试脚本也印证了该参数的标准用法,例如 app_init_perf_test.sh 中:
export ENDPOINT="https://api.ente.com" flutter drive \ --driver=test_driver/perf_driver.dart \ --target=integration_test/app_init_test.dart \ --dart-define=endpoint=$ENDPOINT \ --profile --flavor independent四、构建 Auth 应用(2FA 应用)
Ente Auth 的构建流程与之类似,但多一步 submodule 初始化:
cd ente/auth git submodule update --init --recursive flutter pub get flutter run --dart-define=endpoint=http://localhost:8080 # Android flutter run --dart-define=endpoint=http://localhost:8080 --flavor independent --debug -t lib/main.dart # iOS flutter run --dart-define=endpoint=http://localhost:8080auth/README.md 同样要求 Flutter v3.47.2,并明确 Android 下flutter run --flavor independent、iOS 下直接flutter run、macOS 下flutter run -d macos。Auth 应用的开发设置界面也会读取当前 endpoint,并在非生产值(非kDefaultProductionEndpoint)时显示服务器地址提示,见 developer_settings_widget.dart。
五、构建 release(非 debug)包
调试完成后,产出正式安装包的命令如下:
Android APK
flutter build apk --release --flavor independent -t lib/main.dart构建前需先配置签名密钥库:
- 按 Flutter 官方文档创建 upload keystore;
- 在仓库根目录的
key.properties(或环境变量)中配置签名信息。Photos 的 Gradle 配置支持两种来源,见 build.gradle:优先读取key.properties,否则读取SIGNING_KEY_ALIAS、SIGNING_KEY_PASSWORD、SIGNING_STORE_PASSWORD环境变量; - 若需要连接自托管服务器,同样追加
--dart-define=endpoint=...。
iOS
flutter build ios提示:iOS 构建需在 macOS 上进行;更新 Flutter 依赖后,需在
ios/目录执行pod install并提交ios/Podfile.lock的变更(Photos 与 Auth 的 README 均有说明)。
六、运行期备用方案:开发设置切换服务器
--dart-define属于编译期配置,重新编译才能更改。如果只是临时体验自托管服务器,Ente 还提供了运行期方案:在应用启动/欢迎页连续点击 7 次进入开发设置(Developer Settings),直接填写服务器 endpoint,无需重新构建。该方案对应的完整说明见 post-install/index.md 的 "Step 6: Configure apps to use your server",其底层正是EndpointConfig.setEndpoint将值写入本地偏好并广播EndpointUpdatedEvent。
七、常见问题与注意事项
- flavor 缺失报错:Android 构建必须带
--flavor independent,否则无法匹配 Gradle 中定义的productFlavors; - submodule 未初始化:Auth 应用若跳过
git submodule update --init --recursive,flutter pub get会因缺失依赖包而失败; - endpoint 必须客户端可达:
localhost:8080仅适用于模拟器或同一台机器;真机调试请使用局域网 IP 或域名,并确认服务器已按 self-hosting 快速开始 启动。若自托管服务器使用对象存储(如 MinIO),还需注意 endpoint 需同时被 museum 与客户端解析,否则上传会静默失败(详见 post-install 文档中对 S3 endpoint 可达性的说明); - 生产默认值回退:不带
--dart-define=endpoint构建的应用默认连接https://api.ente.com,请勿将未定制的 debug 包分发为自托管版本; - 开发设置与编译参数的关系:开发设置(运行时写入)优先级高于编译期
--dart-define默认值,两者同时存在时以开发设置中的值为准。
参考文档
- Ente Photos 构建说明
- Ente Auth 构建说明
- 自托管后配置应用 endpoint
- 自托管快速开始
【免费下载链接】ente💚 End-to-end encrypted cloud for everything.项目地址: https://gitcode.com/GitHub_Trending/en/ente
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考