file_operations/doc/RUST_MIGRATION.md
This document outlines the migration from C++ to Rust in the file_operations module using Mozilla's Rust Android Gradle plugin.
The migration replaces the C++ implementation in src/main/c/rootoperations.c with a Rust implementation while maintaining the same JNI interface for seamless integration with the existing Kotlin code.
src/main/c/rootoperations.cJava_com_amaze_filemanager_fileoperations_filesystem_root_NativeOperations_isDirectorystat() system callsrc/lib.rsstd::fs::metadata()ANDROID_HOME or ANDROID_NDK_ROOTThe project includes an automated setup script that detects your system configuration and generates the correct configuration files:
cd file_operations
./setup_rust_android.sh
This script will:
ANDROID_HOME, ANDROID_SDK_ROOT, or common locations)build.gradle.cargo/config.toml with correct linker pathsIf the automated setup doesn't work, you can manually configure:
Install Android Rust targets:
rustup target add aarch64-linux-android
rustup target add armv7-linux-androideabi
rustup target add i686-linux-android
rustup target add x86_64-linux-android
Create .cargo/config.toml based on your system:
.cargo/config.toml.template to .cargo/config.toml{{NDK_PATH}}: Path to your NDK toolchain binaries{{API_LEVEL}}: Your minimum API level (e.g., 21){{EXE_SUFFIX}}: .exe for Windows, empty for Unix systemsAndroid SDK/NDK:
ANDROID_HOME environment variableSupported Platforms:
darwin-x86_64)linux-x86_64)windows-x86_64)The build.gradle file now includes:
file_operations/
├── src/
│ └── main/
│ └── rust/
│ ├── lib.rs # Main library file (module organizer)
│ └── rootoperations.rs # Root operations functions
├── .cargo/
│ ├── config.toml # Auto-generated Cargo configuration for Android
│ └── config.toml.template # Template for generating config.toml
├── Cargo.toml # Rust project configuration
├── setup_rust_android.sh # Automated setup script
├── build.gradle # Updated with Rust plugin
└── RUST_MIGRATION.md # This file
externalNativeBuildjni, libc, android_logger, log cratesstat() system callstd::fs::metadata()Result type with proper error handlinglib.rs: Main library file that organizes and re-exports modulesrootoperations.rs: Root-specific operations (migrated from C)The Rust implementation includes unit tests:
cd file_operations
cargo test
The Rust code is automatically built when you build the Android project:
./gradlew :file_operations:build
First, try re-running the automated setup script:
cd file_operations
./setup_rust_android.sh
This will re-detect your system configuration and regenerate .cargo/config.toml.
If the automated setup fails to find your NDK, ensure your Android SDK is properly configured:
export ANDROID_HOME=/path/to/android/sdk
# The script will automatically find the NDK in $ANDROID_HOME/ndk/[version]
If you see linker errors, verify your NDK installation:
.cargo/config.toml paths existClean and rebuild:
cd file_operations
cargo clean
cd ..
./gradlew clean
./gradlew :file_operations:build
The setup script supports:
darwin-x86_64 platformlinux-x86_64 platformwindows-x86_64 platform and adds .exe suffixIf you encounter platform-specific issues, check the generated linker paths in .cargo/config.toml.
The modular structure makes it easy to add new functionality:
.rs file in src/main/rust/ (e.g., network_ops.rs)lib.rs:
pub mod network_ops;
lib.rs for easy access:
pub use network_ops::{
download_file,
upload_file,
};
To add new JNI functions that can be called from Kotlin:
Add the function in the appropriate module:
#[no_mangle]
pub extern "system" fn Java_com_amaze_filemanager_fileoperations_filesystem_root_NativeOperations_newFunction(
env: JNIEnv,
_class: JClass,
param: JString,
) -> jboolean {
// Implementation
}
Add the corresponding method in NativeOperations.kt:
@JvmStatic
external fun newFunction(param: String?): Boolean
// In src/main/rust/compression_ops.rs
pub mod compression_ops {
use jni::objects::{JClass, JString};
use jni::JNIEnv;
use jni::sys::jboolean;
#[no_mangle]
pub extern "system" fn Java_com_amaze_filemanager_fileoperations_filesystem_root_NativeOperations_compressFile(
env: JNIEnv,
_class: JClass,
source: JString,
destination: JString,
) -> jboolean {
// Implementation
}
}
For questions or issues with the migration, please refer to: