Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update Android Rust core build incremental and update vault merge kotlin implementation (#1473)

Leendert de Borst0d4857f940ef
diff --git a/apps/mobile-app/android/app/build.gradle b/apps/mobile-app/android/app/build.gradle
index f6b9d1dcd..4b873e49e 100644
--- a/apps/mobile-app/android/app/build.gradle
+++ b/apps/mobile-app/android/app/build.gradle
@@ -342,3 +342,29 @@ detekt {
sarif.required.set(false)
}
}
+
+// Rust Core build task - ensures native libraries and Kotlin bindings are up to date
+// Uses checksum-based incremental builds to skip if sources haven't changed
+task buildRustCore(type: Exec) {
+ description = "Build Rust Core native libraries and Kotlin bindings"
+ group = "build"
+
+ workingDir = file("${rootProject.projectDir}/scripts")
+
+ // Set up environment for cargo
+ environment "PATH", "${System.getProperty('user.home')}/.cargo/bin:/usr/local/bin:/opt/homebrew/bin:${System.getenv('PATH')}"
+
+ // Determine build mode based on variant
+ def isRelease = gradle.startParameter.taskNames.any { it.toLowerCase().contains('release') }
+ def buildArgs = isRelease ? ['./build-rust-core.sh', '--release'] : ['./build-rust-core.sh', '--debug']
+
+ commandLine buildArgs
+
+ // Only log if there are actual changes
+ ignoreExitValue = false
+}
+
+// Make Rust Core build run before Kotlin compilation
+tasks.matching { it.name.startsWith('compile') && it.name.contains('Kotlin') }.configureEach {
+ dependsOn buildRustCore
+}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt
index 73609c2a0..bc7cd7dd1 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/rustcore/VaultMergeService.kt
@@ -66,27 +66,64 @@ object VaultMergeService {
tempDir: File,
): String {
// Decrypt both vaults
+ // Vault format: base64(encrypted(base64(sqlite_bytes)))
+ // After AES decrypt we get base64(sqlite_bytes), need to decode again
val localDecrypted = try {
- VaultCrypto.decrypt(Base64.decode(localVaultBase64, Base64.DEFAULT), encryptionKey)
+ val decryptedBase64 = VaultCrypto.decrypt(Base64.decode(localVaultBase64, Base64.DEFAULT), encryptionKey)
+ Base64.decode(decryptedBase64, Base64.NO_WRAP)
} catch (e: Exception) {
Log.e(TAG, "Failed to decrypt local vault", e)
throw VaultMergeException.InvalidInput("Failed to decrypt local vault: ${e.message}")
}
val serverDecrypted = try {
- VaultCrypto.decrypt(Base64.decode(serverVaultBase64, Base64.DEFAULT), encryptionKey)
+ val decryptedBase64 = VaultCrypto.decrypt(Base64.decode(serverVaultBase64, Base64.DEFAULT), encryptionKey)
+ Base64.decode(decryptedBase64, Base64.NO_WRAP)
} catch (e: Exception) {
Log.e(TAG, "Failed to decrypt server vault", e)
throw VaultMergeException.InvalidInput("Failed to decrypt server vault: ${e.message}")
}
// Create temporary database files
+ // Ensure the temp directory exists (may not on first app run or after cache clear)
+ if (!tempDir.exists()) {
+ tempDir.mkdirs()
+ }
+
+ // Verify directory exists and is writable
+ if (!tempDir.exists() || !tempDir.isDirectory) {
+ throw VaultMergeException.DatabaseError("Temp directory does not exist or is not a directory: ${tempDir.absolutePath}")
+ }
+ if (!tempDir.canWrite()) {
+ throw VaultMergeException.DatabaseError("Temp directory is not writable: ${tempDir.absolutePath}")
+ }
+
val localDbFile = File(tempDir, "local_merge_temp.db")
val serverDbFile = File(tempDir, "server_merge_temp.db")
try {
- localDbFile.writeBytes(localDecrypted)
- serverDbFile.writeBytes(serverDecrypted)
+ // Write decrypted databases to temp files using FileOutputStream to ensure proper flushing
+ java.io.FileOutputStream(localDbFile).use { fos ->
+ fos.write(localDecrypted)
+ fos.fd.sync()
+ }
+
+ java.io.FileOutputStream(serverDbFile).use { fos ->
+ fos.write(serverDecrypted)
+ fos.fd.sync()
+ }
+
+ // Verify files were written
+ if (!localDbFile.exists() || localDbFile.length() == 0L) {
+ throw VaultMergeException.DatabaseError(
+ "Failed to write local temp db: exists=${localDbFile.exists()}, len=${localDbFile.length()}",
+ )
+ }
+ if (!serverDbFile.exists() || serverDbFile.length() == 0L) {
+ throw VaultMergeException.DatabaseError(
+ "Failed to write server temp db: exists=${serverDbFile.exists()}, len=${serverDbFile.length()}",
+ )
+ }
val localDb = SQLiteDatabase.openDatabase(localDbFile.absolutePath, null, SQLiteDatabase.OPEN_READWRITE)
val serverDb = SQLiteDatabase.openDatabase(serverDbFile.absolutePath, null, SQLiteDatabase.OPEN_READONLY)
@@ -136,8 +173,10 @@ object VaultMergeService {
serverDb.close()
// Encrypt and return
+ // Output format must match input: base64(encrypted(base64(sqlite_bytes)))
val mergedData = localDbFile.readBytes()
- val encrypted = VaultCrypto.encrypt(mergedData, encryptionKey)
+ val mergedBase64 = Base64.encodeToString(mergedData, Base64.NO_WRAP)
+ val encrypted = VaultCrypto.encrypt(mergedBase64.toByteArray(Charsets.UTF_8), encryptionKey)
return Base64.encodeToString(encrypted, Base64.DEFAULT)
} finally {
if (localDb.isOpen) localDb.close()
@@ -160,13 +199,20 @@ object VaultMergeService {
encryptionKey: ByteArray,
tempDir: File,
): Pair<String, Int> {
+ // Vault format: base64(encrypted(base64(sqlite_bytes)))
+ // After AES decrypt we get base64(sqlite_bytes), need to decode again
val decrypted = try {
- VaultCrypto.decrypt(Base64.decode(vaultBase64, Base64.DEFAULT), encryptionKey)
+ val decryptedBase64 = VaultCrypto.decrypt(Base64.decode(vaultBase64, Base64.DEFAULT), encryptionKey)
+ Base64.decode(decryptedBase64, Base64.NO_WRAP)
} catch (e: Exception) {
Log.e(TAG, "Failed to decrypt vault for pruning", e)
throw VaultMergeException.InvalidInput("Failed to decrypt vault: ${e.message}")
}
+ // Ensure the temp directory exists (may not on first app run or after cache clear)
+ if (!tempDir.exists()) {
+ tempDir.mkdirs()
+ }
val dbFile = File(tempDir, "prune_temp.db")
try {
@@ -215,8 +261,10 @@ object VaultMergeService {
db.close()
// Encrypt and return
+ // Output format must match input: base64(encrypted(base64(sqlite_bytes)))
val prunedData = dbFile.readBytes()
- val encrypted = VaultCrypto.encrypt(prunedData, encryptionKey)
+ val prunedBase64 = Base64.encodeToString(prunedData, Base64.NO_WRAP)
+ val encrypted = VaultCrypto.encrypt(prunedBase64.toByteArray(Charsets.UTF_8), encryptionKey)
return Pair(Base64.encodeToString(encrypted, Base64.DEFAULT), statements.size)
} finally {
if (db.isOpen) db.close()
diff --git a/apps/mobile-app/android/scripts/build-rust-core.sh b/apps/mobile-app/android/scripts/build-rust-core.sh
new file mode 100755
index 000000000..8b0567c90
--- /dev/null
+++ b/apps/mobile-app/android/scripts/build-rust-core.sh
@@ -0,0 +1,47 @@
+#!/bin/bash
+
+# Thin wrapper that calls the main Rust core build script
+# This is called by Gradle build phases
+#
+# Usage:
+# ./build-rust-core.sh [--force] [--release|--debug]
+#
+# The main build script lives at: /core/rust/build.sh
+
+set -e
+
+# Ensure cargo is in PATH (for Gradle build phases)
+export PATH="$HOME/.cargo/bin:/usr/local/bin:/opt/homebrew/bin:$PATH"
+
+# Script location
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+RUST_CORE_DIR="$(cd "$SCRIPT_DIR/../../../../core/rust" && pwd)"
+JNILIBS_DIR="$SCRIPT_DIR/../app/src/main/jniLibs"
+
+# Parse arguments to pass through
+EXTRA_ARGS=""
+FORCE_FLAG=""
+
+while [[ $# -gt 0 ]]; do
+ case $1 in
+ --force)
+ FORCE_FLAG="--force"
+ shift
+ ;;
+ --release)
+ EXTRA_ARGS=""
+ shift
+ ;;
+ --debug)
+ EXTRA_ARGS="--fast"
+ shift
+ ;;
+ *)
+ shift
+ ;;
+ esac
+done
+
+# Call the main build script with incremental mode
+cd "$RUST_CORE_DIR"
+exec ./build.sh --android --incremental $FORCE_FLAG $EXTRA_ARGS
diff --git a/core/rust/build.sh b/core/rust/build.sh
index f4231e9d0..ba12aba69 100755
--- a/core/rust/build.sh
+++ b/core/rust/build.sh
@@ -42,6 +42,28 @@ check_tool() {
fi
}
+# Portable checksum function (works on macOS and Linux)
+# Includes Rust sources AND Cargo.toml/Cargo.lock to detect dependency changes
+# that can cause UniFFI checksum mismatches
+compute_source_checksum() {
+ local dir="$1"
+ if [ -d "$dir" ]; then
+ # Combine checksums of:
+ # 1. All .rs source files
+ # 2. Cargo.toml and Cargo.lock (dependency changes affect UniFFI checksums)
+ {
+ find "$dir" -name "*.rs" -type f -print0 2>/dev/null | \
+ sort -z | \
+ xargs -0 shasum -a 256 2>/dev/null
+ # Include Cargo files from parent directory (where Cargo.toml lives)
+ local cargo_dir="$(dirname "$dir")"
+ shasum -a 256 "$cargo_dir/Cargo.toml" "$cargo_dir/Cargo.lock" 2>/dev/null
+ } | shasum -a 256 | cut -d' ' -f1
+ else
+ echo "unknown"
+ fi
+}
+
echo -e "${YELLOW}Checking prerequisites...${NC}"
check_tool "rustc" "Visit https://rustup.rs"
check_tool "cargo" "Visit https://rustup.rs"
@@ -319,9 +341,7 @@ build_ios() {
# Incremental build check
local checksum_file="$IOS_APP_DIST/.rust-core-checksum"
local current_checksum=""
- if [ -d "$SCRIPT_DIR/src" ]; then
- current_checksum=$(find "$SCRIPT_DIR/src" -name "*.rs" -type f -exec md5 -q {} \; 2>/dev/null | md5 -q || echo "unknown")
- fi
+ current_checksum=$(compute_source_checksum "$SCRIPT_DIR/src")
if $INCREMENTAL && [ "$FORCE_BUILD" = false ] && [ -f "$checksum_file" ] && [ -f "$IOS_APP_DIST/lib/device/libaliasvault_core.a" ]; then
local stored_checksum=$(cat "$checksum_file" 2>/dev/null || echo "")
@@ -448,9 +468,7 @@ EOF
# Save checksum for incremental builds
local current_checksum=""
- if [ -d "$SCRIPT_DIR/src" ]; then
- current_checksum=$(find "$SCRIPT_DIR/src" -name "*.rs" -type f -exec md5 -q {} \; 2>/dev/null | md5 -q || echo "unknown")
- fi
+ current_checksum=$(compute_source_checksum "$SCRIPT_DIR/src")
echo "$current_checksum" > "$IOS_APP_DIST/.rust-core-checksum"
# Create README
@@ -495,6 +513,21 @@ build_android() {
local start_time=$(date +%s)
+ # Incremental build check
+ local checksum_file="$ANDROID_APP_DIST/.rust-core-checksum"
+ local current_checksum=""
+ current_checksum=$(compute_source_checksum "$SCRIPT_DIR/src")
+
+ if $INCREMENTAL && [ "$FORCE_BUILD" = false ] && [ -f "$checksum_file" ] && [ -f "$ANDROID_APP_DIST/arm64-v8a/libaliasvault_core.so" ]; then
+ local stored_checksum=$(cat "$checksum_file" 2>/dev/null || echo "")
+ if [ "$current_checksum" = "$stored_checksum" ]; then
+ echo -e "${GREEN}Rust Core is up to date, skipping build${NC}"
+ ANDROID_BUILD_SKIPPED=true
+ return 0
+ fi
+ fi
+ ANDROID_BUILD_SKIPPED=false
+
# Check for Android NDK
if [ -z "${ANDROID_NDK_HOME:-}" ]; then
# Try to find NDK in common locations
@@ -676,6 +709,11 @@ distribute_android() {
echo -e "${RED}Warning: Kotlin bindings not found at $ANDROID_DIR/kotlin/uniffi${NC}"
fi
+ # Save checksum for incremental builds
+ local current_checksum=""
+ current_checksum=$(compute_source_checksum "$SCRIPT_DIR/src")
+ echo "$current_checksum" > "$ANDROID_APP_DIST/.rust-core-checksum"
+
echo -e "${GREEN}Distributed to: $ANDROID_APP_DIST${NC}"
}