From e0ed6ee986e6b68cacbfdaefd5bf306e935e73f6 Mon Sep 17 00:00:00 2001
From: Vasyl Gello <vasek.gello@gmail.com>
Date: Sat, 30 Nov 2024 03:24:45 +0200
Subject: [PATCH] Fix F-Droid build and bump Android NDK to r27c (#10105)

* Fix fdroid build

* Refactor recent @fufesou edits to reflect the fact that
  .gclient file is needed only on x86 to build jit-release
  version of flutter-engine and `flutter-sdk` directory is
  not affected by flutter version checkouts

* Install cargo-ndk and flutter-rust-codegen with `--locked`
  argument to avoid bumping `cargo-platform` to require
  newer Rust toolchain

Signed-off-by: Vasyl Gello <vasek.gello@gmail.com>

* Bump Android NDK to r27c

Signed-off-by: Vasyl Gello <vasek.gello@gmail.com>

---------

Signed-off-by: Vasyl Gello <vasek.gello@gmail.com>
---
 .github/workflows/flutter-build.yml |   2 +-
 flutter/build_fdroid.sh             | 141 +++++++++++++++++++---------
 2 files changed, 97 insertions(+), 46 deletions(-)

diff --git a/.github/workflows/flutter-build.yml b/.github/workflows/flutter-build.yml
index 555bcb96b..fd3930216 100644
--- a/.github/workflows/flutter-build.yml
+++ b/.github/workflows/flutter-build.yml
@@ -34,7 +34,7 @@ env:
   # vcpkg version: 2024.07.12
   VCPKG_COMMIT_ID: "1de2026f28ead93ff1773e6e680387643e914ea1"
   VERSION: "1.3.3"
-  NDK_VERSION: "r27b"
+  NDK_VERSION: "r27c"
   #signing keys env variable checks
   ANDROID_SIGNING_KEY: "${{ secrets.ANDROID_SIGNING_KEY }}"
   MACOS_P12_BASE64: "${{ secrets.MACOS_P12_BASE64 }}"
diff --git a/flutter/build_fdroid.sh b/flutter/build_fdroid.sh
index e0a4eb599..1821c529a 100755
--- a/flutter/build_fdroid.sh
+++ b/flutter/build_fdroid.sh
@@ -1,7 +1,5 @@
 #!/bin/bash
 
-set -x
-
 #
 # Script to build F-Droid release of RustDesk
 #
@@ -23,6 +21,43 @@ set -x
 #   + build: perform actual build of APK file
 #
 
+# Start of functions
+
+# Install Flutter of version `VERSION` from Github repository
+# into directory `FLUTTER_DIR` and apply patches if needed
+
+prepare_flutter() {
+	VERSION="${1}"
+	FLUTTER_DIR="${2}"
+
+	if [ ! -f "${FLUTTER_DIR}/bin/flutter" ]; then
+		git clone https://github.com/flutter/flutter "${FLUTTER_DIR}"
+	fi
+
+	pushd "${FLUTTER_DIR}"
+
+	git restore .
+	git checkout "${VERSION}"
+
+	# Patch flutter
+
+	if dpkg --compare-versions "${VERSION}" ge "3.24.4"; then
+		git apply "${ROOTDIR}/.github/patches/flutter_3.24.4_dropdown_menu_enableFilter.diff"
+	fi
+
+	flutter config --no-analytics
+
+	popd # ${FLUTTER_DIR}
+}
+
+# Start of script
+
+set -x
+
+# Note current working directory as root dir for patches
+
+ROOTDIR="${PWD}"
+
 # Parse command-line arguments
 
 VERNAME="${1}"
@@ -82,21 +117,6 @@ export PATH="${PATH}:${HOME}/flutter/bin:${HOME}/depot_tools"
 
 export VCPKG_ROOT="${HOME}/vcpkg"
 
-prepare_Flutter() {
-	version="${1}"
-
-	pushd "${HOME}"
-	if [ ! -f "${HOME}/flutter/bin/flutter" ]; then
-		git clone https://github.com/flutter/flutter
-	fi
-	pushd flutter
-	git restore .
-	git checkout "${version}"
-	flutter config --no-analytics
-	popd # flutter
-	popd # ${HOME}
-}
-
 # Now act depending on build step
 
 # NOTE: F-Droid maintainers require explicit declaration of dependencies
@@ -116,15 +136,20 @@ prebuild)
 		.env.CARGO_NDK_VERSION \
 		.github/workflows/flutter-build.yml)"
 
+	# Flutter used to compile main Rustdesk library
+
 	FLUTTER_VERSION="$(yq -r \
 		.env.ANDROID_FLUTTER_VERSION \
 		.github/workflows/flutter-build.yml)"
+
 	if [ -z "${FLUTTER_VERSION}" ]; then
 		FLUTTER_VERSION="$(yq -r \
 			.env.FLUTTER_VERSION \
 			.github/workflows/flutter-build.yml)"
 	fi
 
+	# Flutter used to compile Flutter<->Rust bridge files
+
 	FLUTTER_BRIDGE_VERSION="$(yq -r \
 		.env.FLUTTER_VERSION \
 		.github/workflows/bridge.yml)"
@@ -207,14 +232,16 @@ prebuild)
 
 	cargo install \
 		cargo-ndk \
-		--version "${CARGO_NDK_VERSION}"
+		--version "${CARGO_NDK_VERSION}" \
+		--locked
 
 	# Install rust bridge generator
 
 	cargo install cargo-expand
 	cargo install flutter_rust_bridge_codegen \
 		--version "${FLUTTER_RUST_BRIDGE_VERSION}" \
-		--features "uuid"
+		--features "uuid" \
+		--locked
 
 	# Populate native vcpkg dependencies
 
@@ -277,46 +304,66 @@ prebuild)
 
 	git apply res/fdroid/patches/*.patch
 
-	# Backup .gclient file, for later restore
+	# If Flutter version used to generate bridge files differs from Flutter
+	# version used to compile Rustdesk library, generate bridge using the
+	# `FLUTTER_BRIDGE_VERSION` an restore the pubspec later
 
-	cp flutter-sdk/.gclient flutter-sdk/.gclient.bak
+	if [ "${FLUTTER_VERSION}" != "${FLUTTER_BRIDGE_VERSION}" ]; then
+		# Install Flutter bridge version
 
-	# For FLUTTER_BRIDGE_VERSION
-	sed \
-		-i \
-		-e 's/extended_text: 14.0.0/extended_text: 13.0.0/g' \
-		flutter/pubspec.yaml
+		prepare_flutter "${FLUTTER_BRIDGE_VERSION}" "${HOME}/flutter"
 
-	# Install Flutter bridge version
-	prepare_Flutter "${FLUTTER_BRIDGE_VERSION}"
-	cp flutter-sdk/.gclient.bak flutter-sdk/.gclient
-	sed -i "s/FLUTTER_VERSION_PLACEHOLDER/${FLUTTER_BRIDGE_VERSION}/" flutter-sdk/.gclient
+		# Save changes
 
-	# Download Flutter dependencies
-	pushd flutter
-	flutter clean && flutter packages pub get
-	popd # flutter
+		git add .
 
-	# Generate FFI bindings
-	flutter_rust_bridge_codegen \
-		--rust-input ./src/flutter_ffi.rs \
-		--dart-output ./flutter/lib/generated_bridge.dart
+		# Edit pubspec to make flutter bridge version work
 
-	git restore flutter/pubspec.*
+		sed \
+			-i \
+			-e 's/extended_text: 14.0.0/extended_text: 13.0.0/g' \
+			flutter/pubspec.yaml
 
-	# Install Flutter
-	prepare_Flutter "${FLUTTER_VERSION}"
-	cp flutter-sdk/.gclient.bak flutter-sdk/.gclient
-	sed -i "s/FLUTTER_VERSION_PLACEHOLDER/${FLUTTER_VERSION}/" flutter-sdk/.gclient
+		# Download Flutter dependencies
+
+		pushd flutter
+
+		flutter clean
+		flutter packages pub get
+
+		popd # flutter
+
+		# Generate FFI bindings
+
+		flutter_rust_bridge_codegen \
+			--rust-input ./src/flutter_ffi.rs \
+			--dart-output ./flutter/lib/generated_bridge.dart
+
+		# Add bridge files to save-list
+
+		git add -f ./flutter/lib/generated_bridge.* ./src/bridge_generated.*
+
+		# Restore everything
+
+		git checkout '*'
+		git clean -dffx
+		git reset
+	fi
+
+	# Install Flutter version for RustDesk library build
+
+	prepare_flutter "${FLUTTER_VERSION}" "${HOME}/flutter"
 
 	# gms is not in thoes files now, but we still keep the following line for future reference(maybe).
+
 	sed \
 		-i \
 		-e '/gms/d' \
 		flutter/android/build.gradle \
 		flutter/android/app/build.gradle
 
-	# `firebase_analytics`` is not in thoes files now, but we still keep the following lines.
+	# `firebase_analytics` is not in these files now, but we still keep the following lines.
+
 	sed \
 		-i \
 		-e '/firebase_analytics/d' \
@@ -343,9 +390,12 @@ build)
 	# '.github/workflows/flutter-build.yml'
 	#
 
+	# Flutter used to compile main Rustdesk library
+
 	FLUTTER_VERSION="$(yq -r \
 		.env.ANDROID_FLUTTER_VERSION \
 		.github/workflows/flutter-build.yml)"
+
 	if [ -z "${FLUTTER_VERSION}" ]; then
 		FLUTTER_VERSION="$(yq -r \
 			.env.FLUTTER_VERSION \
@@ -381,7 +431,8 @@ build)
 
 	pushd flutter
 
-	flutter clean && flutter packages pub get
+	flutter clean
+	flutter packages pub get
 
 	popd # flutter