<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://johnwick.cc/index.php?action=history&amp;feed=atom&amp;title=Comprehensive_Guide_to_Using_Rust_in_Android_Development</id>
	<title>Comprehensive Guide to Using Rust in Android Development - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://johnwick.cc/index.php?action=history&amp;feed=atom&amp;title=Comprehensive_Guide_to_Using_Rust_in_Android_Development"/>
	<link rel="alternate" type="text/html" href="https://johnwick.cc/index.php?title=Comprehensive_Guide_to_Using_Rust_in_Android_Development&amp;action=history"/>
	<updated>2026-05-06T23:58:04Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.1</generator>
	<entry>
		<id>https://johnwick.cc/index.php?title=Comprehensive_Guide_to_Using_Rust_in_Android_Development&amp;diff=1099&amp;oldid=prev</id>
		<title>PC: Created page with &quot;Introduction  500px  If you’ve been keeping an eye on modern systems programming, you’ve probably noticed Rust popping up everywhere — and Android development is no exception. Rust brings memory safety, strong performance, and a fresh way of thinking about low-level code. In this guide, I’ll walk you through how Rust fits into Android projects, why teams are adopting it for performance-critical...&quot;</title>
		<link rel="alternate" type="text/html" href="https://johnwick.cc/index.php?title=Comprehensive_Guide_to_Using_Rust_in_Android_Development&amp;diff=1099&amp;oldid=prev"/>
		<updated>2025-11-23T17:21:09Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Introduction  &lt;a href=&quot;/index.php?title=File:Comprehensive_Guide_to_Using_Rust_in_Android_Development.jpg&quot; title=&quot;File:Comprehensive Guide to Using Rust in Android Development.jpg&quot;&gt;500px&lt;/a&gt;  If you’ve been keeping an eye on modern systems programming, you’ve probably noticed Rust popping up everywhere — and Android development is no exception. Rust brings memory safety, strong performance, and a fresh way of thinking about low-level code. In this guide, I’ll walk you through how Rust fits into Android projects, why teams are adopting it for performance-critical...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Introduction&lt;br /&gt;
&lt;br /&gt;
[[file:Comprehensive_Guide_to_Using_Rust_in_Android_Development.jpg|500px]]&lt;br /&gt;
&lt;br /&gt;
If you’ve been keeping an eye on modern systems programming, you’ve probably noticed Rust popping up everywhere — and Android development is no exception. Rust brings memory safety, strong performance, and a fresh way of thinking about low-level code. In this guide, I’ll walk you through how Rust fits into Android projects, why teams are adopting it for performance-critical components, and what you need to know to start integrating it into your own apps.&lt;br /&gt;
Methods for Using Rust in Android&lt;br /&gt;
&lt;br /&gt;
1. Native Development Kit (NDK) Integration&lt;br /&gt;
&lt;br /&gt;
Rust can be utilized to write native code for Android through the Native Development Kit (NDK). Rust produces libraries compatible with C and C++, enabling you to invoke Rust code from your Android application via the Java Native Interface (JNI).&lt;br /&gt;
&lt;br /&gt;
2. Gradle Integration&lt;br /&gt;
&lt;br /&gt;
Rust code can be integrated into your Android project using Gradle. The Rust code is compiled as a shared library (.so file), which is then linked with your Android application. Cargo, Rust’s package manager, works alongside Gradle to manage Rust dependencies.&lt;br /&gt;
&lt;br /&gt;
3. Cross-Compilation&lt;br /&gt;
&lt;br /&gt;
Rust supports multiple architectures, allowing you to cross-compile code for different Android architectures (ARM, x86, etc.). The Rust compiler includes built-in support for Android targets, streamlining this process.&lt;br /&gt;
&lt;br /&gt;
4. Foreign Function Interface (FFI)&lt;br /&gt;
&lt;br /&gt;
Rust can interface with existing C or C++ libraries in your Android project, or expose Rust functionality to your Java/Kotlin code through FFI.&lt;br /&gt;
Benefits of Using Rust in Android Development&lt;br /&gt;
&lt;br /&gt;
Memory Safety&lt;br /&gt;
&lt;br /&gt;
Rust’s ownership system guarantees memory safety without garbage collection, preventing common bugs such as null pointer dereferencing, buffer overflows, and use-after-free errors.&lt;br /&gt;
&lt;br /&gt;
Performance&lt;br /&gt;
&lt;br /&gt;
Rust delivers performance comparable to C or C++, making it an excellent choice for performance-critical components of your application, including computational tasks, data processing, and real-time operations.&lt;br /&gt;
Concurrency&lt;br /&gt;
&lt;br /&gt;
Rust provides robust support for concurrent programming with compile-time guarantees that prevent data races, enabling safe parallel execution.&lt;br /&gt;
Zero-Cost Abstractions&lt;br /&gt;
&lt;br /&gt;
Rust offers high-level abstractions without runtime overhead, allowing you to write expressive code that compiles to efficient machine code.&lt;br /&gt;
&lt;br /&gt;
Practical Use Cases&lt;br /&gt;
* 		Cryptography: Implementing cryptographic operations where Rust’s safety guarantees are particularly valuable&lt;br /&gt;
* 		Game Development: Low-level graphics rendering, physics engines, and game logic&lt;br /&gt;
* 		Audio Processing: Real-time audio processing and digital signal processing&lt;br /&gt;
* 		Image Processing: High-performance image manipulation and computer vision tasks&lt;br /&gt;
* 		Network Protocols: Custom protocol implementations requiring high throughput&lt;br /&gt;
* 		Data Parsing: Efficient parsing of complex data formats&lt;br /&gt;
&lt;br /&gt;
Step-by-Step Implementation&lt;br /&gt;
&lt;br /&gt;
Step 1: Create a Rust Library&lt;br /&gt;
&lt;br /&gt;
First, create a new Rust library project:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cargo new --lib myrustlib&lt;br /&gt;
cd myrustlib&lt;br /&gt;
Edit the Cargo.toml file to configure the crate type for dynamic library generation:&lt;br /&gt;
[package]&lt;br /&gt;
name = &amp;quot;myrustlib&amp;quot;&lt;br /&gt;
version = &amp;quot;0.1.0&amp;quot;&lt;br /&gt;
edition = &amp;quot;2021&amp;quot;&lt;br /&gt;
[lib]&lt;br /&gt;
name = &amp;quot;myrustlib&amp;quot;&lt;br /&gt;
crate-type = [&amp;quot;cdylib&amp;quot;]&lt;br /&gt;
[dependencies]&lt;br /&gt;
jni = &amp;quot;0.21&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create the Rust implementation in src/lib.rs:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
use jni::objects::{JClass, JString};&lt;br /&gt;
use jni::sys::jstring;&lt;br /&gt;
use jni::JNIEnv;&lt;br /&gt;
/// JNI function called from Android&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn Java_com_example_myrustapp_MainActivity_helloFromRust(&lt;br /&gt;
    mut env: JNIEnv,&lt;br /&gt;
    _class: JClass,&lt;br /&gt;
) -&amp;gt; jstring {&lt;br /&gt;
    let message = get_hello_string();&lt;br /&gt;
    &lt;br /&gt;
    // Convert Rust string to Java string&lt;br /&gt;
    let output = env.new_string(message)&lt;br /&gt;
        .expect(&amp;quot;Failed to create Java string&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
    output.into_raw()&lt;br /&gt;
}&lt;br /&gt;
/// JNI function that performs a calculation&lt;br /&gt;
#[no_mangle]&lt;br /&gt;
pub extern &amp;quot;C&amp;quot; fn Java_com_example_myrustapp_MainActivity_calculateFibonacci(&lt;br /&gt;
    mut env: JNIEnv,&lt;br /&gt;
    _class: JClass,&lt;br /&gt;
    n: i32,&lt;br /&gt;
) -&amp;gt; jstring {&lt;br /&gt;
    let result = fibonacci(n);&lt;br /&gt;
    let message = format!(&amp;quot;Fibonacci({}) = {}&amp;quot;, n, result);&lt;br /&gt;
    &lt;br /&gt;
    let output = env.new_string(message)&lt;br /&gt;
        .expect(&amp;quot;Failed to create Java string&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
    output.into_raw()&lt;br /&gt;
}&lt;br /&gt;
/// Core Rust function&lt;br /&gt;
pub fn get_hello_string() -&amp;gt; &amp;amp;&amp;#039;static str {&lt;br /&gt;
    &amp;quot;Hello from Rust! Memory-safe and blazingly fast!&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
/// Example computation function&lt;br /&gt;
fn fibonacci(n: i32) -&amp;gt; u64 {&lt;br /&gt;
    match n {&lt;br /&gt;
        0 =&amp;gt; 0,&lt;br /&gt;
        1 =&amp;gt; 1,&lt;br /&gt;
        _ =&amp;gt; {&lt;br /&gt;
            let mut a: u64 = 0;&lt;br /&gt;
            let mut b: u64 = 1;&lt;br /&gt;
            for _ in 2..=n {&lt;br /&gt;
                let temp = a + b;&lt;br /&gt;
                a = b;&lt;br /&gt;
                b = temp;&lt;br /&gt;
            }&lt;br /&gt;
            b&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
#[cfg(test)]&lt;br /&gt;
mod tests {&lt;br /&gt;
    use super::*;&lt;br /&gt;
    #[test]&lt;br /&gt;
    fn test_fibonacci() {&lt;br /&gt;
        assert_eq!(fibonacci(0), 0);&lt;br /&gt;
        assert_eq!(fibonacci(1), 1);&lt;br /&gt;
        assert_eq!(fibonacci(10), 55);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 2: Configure Cross-Compilation&lt;br /&gt;
&lt;br /&gt;
Create a .cargo/config.toml file in your Rust project root:&lt;br /&gt;
&lt;br /&gt;
[target.aarch64-linux-android]&lt;br /&gt;
linker = &amp;quot;path/to/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang&amp;quot;&lt;br /&gt;
[target.armv7-linux-androideabi]&lt;br /&gt;
linker = &amp;quot;path/to/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi30-clang&amp;quot;&lt;br /&gt;
[target.i686-linux-android]&lt;br /&gt;
linker = &amp;quot;path/to/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android30-clang&amp;quot;&lt;br /&gt;
[target.x86_64-linux-android]&lt;br /&gt;
linker = &amp;quot;path/to/android-ndk/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android30-clang&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Note: Replace path/to/android-ndk with your actual NDK installation path. On macOS, use darwin-x86_64 instead of linux-x86_64.&lt;br /&gt;
&lt;br /&gt;
Step 3: Install Android Targets&lt;br /&gt;
&lt;br /&gt;
Install the required Rust targets for Android:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
rustup target add aarch64-linux-android&lt;br /&gt;
rustup target add armv7-linux-androideabi&lt;br /&gt;
rustup target add i686-linux-android&lt;br /&gt;
rustup target add x86_64-linux-android&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 4: Build the Rust Library&lt;br /&gt;
&lt;br /&gt;
Build the library for all Android architectures:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cargo build --target aarch64-linux-android --release&lt;br /&gt;
cargo build --target armv7-linux-androideabi --release&lt;br /&gt;
cargo build --target i686-linux-android --release&lt;br /&gt;
cargo build --target x86_64-linux-android --release&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or build all targets in one command:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
cargo build --target aarch64-linux-android --release &amp;amp;&amp;amp; \&lt;br /&gt;
cargo build --target armv7-linux-androideabi --release &amp;amp;&amp;amp; \&lt;br /&gt;
cargo build --target i686-linux-android --release &amp;amp;&amp;amp; \&lt;br /&gt;
cargo build --target x86_64-linux-android --release&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 5: Integrate with Android Project&lt;br /&gt;
&lt;br /&gt;
Create the JNI libraries directory structure in your Android project:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
app/src/main/jniLibs/&lt;br /&gt;
├── arm64-v8a/&lt;br /&gt;
├── armeabi-v7a/&lt;br /&gt;
├── x86/&lt;br /&gt;
└── x86_64/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Copy the compiled .so files to the corresponding directories:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# From your Rust project directory&lt;br /&gt;
cp target/aarch64-linux-android/release/libmyrustlib.so \&lt;br /&gt;
   ../AndroidProject/app/src/main/jniLibs/arm64-v8a/&lt;br /&gt;
cp target/armv7-linux-androideabi/release/libmyrustlib.so \&lt;br /&gt;
   ../AndroidProject/app/src/main/jniLibs/armeabi-v7a/&lt;br /&gt;
cp target/i686-linux-android/release/libmyrustlib.so \&lt;br /&gt;
   ../AndroidProject/app/src/main/jniLibs/x86/&lt;br /&gt;
cp target/x86_64-linux-android/release/libmyrustlib.so \&lt;br /&gt;
   ../AndroidProject/app/src/main/jniLibs/x86_64/&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 6: Implement Android Code with Jetpack Compose&lt;br /&gt;
&lt;br /&gt;
First, ensure you have Jetpack Compose dependencies in your app/build.gradle:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
dependencies {&lt;br /&gt;
    implementation platform(&amp;#039;androidx.compose:compose-bom:2024.02.00&amp;#039;)&lt;br /&gt;
    implementation &amp;#039;androidx.compose.ui:ui&amp;#039;&lt;br /&gt;
    implementation &amp;#039;androidx.compose.material3:material3&amp;#039;&lt;br /&gt;
    implementation &amp;#039;androidx.compose.ui:ui-tooling-preview&amp;#039;&lt;br /&gt;
    implementation &amp;#039;androidx.activity:activity-compose:1.8.2&amp;#039;&lt;br /&gt;
    implementation &amp;#039;androidx.lifecycle:lifecycle-runtime-ktx:2.7.0&amp;#039;&lt;br /&gt;
    debugImplementation &amp;#039;androidx.compose.ui:ui-tooling&amp;#039;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create or update your MainActivity.kt:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package com.example.myrustapp&lt;br /&gt;
import android.os.Bundle&lt;br /&gt;
import androidx.activity.ComponentActivity&lt;br /&gt;
import androidx.activity.compose.setContent&lt;br /&gt;
import androidx.compose.foundation.layout.*&lt;br /&gt;
import androidx.compose.foundation.text.KeyboardOptions&lt;br /&gt;
import androidx.compose.material3.*&lt;br /&gt;
import androidx.compose.runtime.*&lt;br /&gt;
import androidx.compose.ui.Alignment&lt;br /&gt;
import androidx.compose.ui.Modifier&lt;br /&gt;
import androidx.compose.ui.text.input.KeyboardType&lt;br /&gt;
import androidx.compose.ui.text.style.TextAlign&lt;br /&gt;
import androidx.compose.ui.tooling.preview.Preview&lt;br /&gt;
import androidx.compose.ui.unit.dp&lt;br /&gt;
class MainActivity : ComponentActivity() {&lt;br /&gt;
    companion object {&lt;br /&gt;
        init {&lt;br /&gt;
            // Load the native Rust library&lt;br /&gt;
            System.loadLibrary(&amp;quot;myrustlib&amp;quot;)&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    // Declare native functions&lt;br /&gt;
    private external fun helloFromRust(): String&lt;br /&gt;
    private external fun calculateFibonacci(n: Int): String&lt;br /&gt;
    override fun onCreate(savedInstanceState: Bundle?) {&lt;br /&gt;
        super.onCreate(savedInstanceState)&lt;br /&gt;
        setContent {&lt;br /&gt;
            RustAppTheme {&lt;br /&gt;
                Surface(&lt;br /&gt;
                    modifier = Modifier.fillMaxSize(),&lt;br /&gt;
                    color = MaterialTheme.colorScheme.background&lt;br /&gt;
                ) {&lt;br /&gt;
                    RustInteractionScreen(&lt;br /&gt;
                        onHelloClick = { helloFromRust() },&lt;br /&gt;
                        onCalculateClick = { n -&amp;gt; calculateFibonacci(n) }&lt;br /&gt;
                    )&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
@Composable&lt;br /&gt;
fun RustInteractionScreen(&lt;br /&gt;
    onHelloClick: () -&amp;gt; String,&lt;br /&gt;
    onCalculateClick: (Int) -&amp;gt; String&lt;br /&gt;
) {&lt;br /&gt;
    var resultText by remember { mutableStateOf(&amp;quot;Result will appear here&amp;quot;) }&lt;br /&gt;
    var inputNumber by remember { mutableStateOf(&amp;quot;&amp;quot;) }&lt;br /&gt;
    Column(&lt;br /&gt;
        modifier = Modifier&lt;br /&gt;
            .fillMaxSize()&lt;br /&gt;
            .padding(16.dp),&lt;br /&gt;
        horizontalAlignment = Alignment.CenterHorizontally,&lt;br /&gt;
        verticalArrangement = Arrangement.Center&lt;br /&gt;
    ) {&lt;br /&gt;
        // Result Display Card&lt;br /&gt;
        Card(&lt;br /&gt;
            modifier = Modifier&lt;br /&gt;
                .fillMaxWidth()&lt;br /&gt;
                .padding(bottom = 24.dp),&lt;br /&gt;
            elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)&lt;br /&gt;
        ) {&lt;br /&gt;
            Text(&lt;br /&gt;
                text = resultText,&lt;br /&gt;
                modifier = Modifier&lt;br /&gt;
                    .padding(16.dp)&lt;br /&gt;
                    .fillMaxWidth(),&lt;br /&gt;
                style = MaterialTheme.typography.bodyLarge,&lt;br /&gt;
                textAlign = TextAlign.Center&lt;br /&gt;
            )&lt;br /&gt;
        }&lt;br /&gt;
        // Input Field&lt;br /&gt;
        OutlinedTextField(&lt;br /&gt;
            value = inputNumber,&lt;br /&gt;
            onValueChange = { inputNumber = it },&lt;br /&gt;
            label = { Text(&amp;quot;Enter a number&amp;quot;) },&lt;br /&gt;
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),&lt;br /&gt;
            modifier = Modifier&lt;br /&gt;
                .fillMaxWidth()&lt;br /&gt;
                .padding(bottom = 16.dp),&lt;br /&gt;
            singleLine = true&lt;br /&gt;
        )&lt;br /&gt;
        // Hello Button&lt;br /&gt;
        Button(&lt;br /&gt;
            onClick = {&lt;br /&gt;
                resultText = onHelloClick()&lt;br /&gt;
            },&lt;br /&gt;
            modifier = Modifier&lt;br /&gt;
                .fillMaxWidth()&lt;br /&gt;
                .padding(bottom = 8.dp)&lt;br /&gt;
        ) {&lt;br /&gt;
            Text(&amp;quot;Say Hello from Rust&amp;quot;)&lt;br /&gt;
        }&lt;br /&gt;
        // Fibonacci Button&lt;br /&gt;
        Button(&lt;br /&gt;
            onClick = {&lt;br /&gt;
                val number = inputNumber.toIntOrNull() ?: 0&lt;br /&gt;
                resultText = onCalculateClick(number)&lt;br /&gt;
            },&lt;br /&gt;
            modifier = Modifier.fillMaxWidth()&lt;br /&gt;
        ) {&lt;br /&gt;
            Text(&amp;quot;Calculate Fibonacci&amp;quot;)&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
@Composable&lt;br /&gt;
fun RustAppTheme(content: @Composable () -&amp;gt; Unit) {&lt;br /&gt;
    MaterialTheme(&lt;br /&gt;
        colorScheme = lightColorScheme(&lt;br /&gt;
            primary = MaterialTheme.colorScheme.primary,&lt;br /&gt;
            secondary = MaterialTheme.colorScheme.secondary,&lt;br /&gt;
            background = MaterialTheme.colorScheme.background&lt;br /&gt;
        ),&lt;br /&gt;
        content = content&lt;br /&gt;
    )&lt;br /&gt;
}&lt;br /&gt;
@Preview(showBackground = true)&lt;br /&gt;
@Composable&lt;br /&gt;
fun RustInteractionScreenPreview() {&lt;br /&gt;
    RustAppTheme {&lt;br /&gt;
        RustInteractionScreen(&lt;br /&gt;
            onHelloClick = { &amp;quot;Hello from Rust! Memory-safe and blazingly fast!&amp;quot; },&lt;br /&gt;
            onCalculateClick = { n -&amp;gt; &amp;quot;Fibonacci($n) = 55&amp;quot; }&lt;br /&gt;
        )&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternative Implementation with ViewModel (Recommended for Production):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// RustViewModel.kt&lt;br /&gt;
package com.example.myrustapp&lt;br /&gt;
import androidx.lifecycle.ViewModel&lt;br /&gt;
import kotlinx.coroutines.flow.MutableStateFlow&lt;br /&gt;
import kotlinx.coroutines.flow.StateFlow&lt;br /&gt;
import kotlinx.coroutines.flow.asStateFlow&lt;br /&gt;
class RustViewModel : ViewModel() {&lt;br /&gt;
    companion object {&lt;br /&gt;
        init {&lt;br /&gt;
            System.loadLibrary(&amp;quot;myrustlib&amp;quot;)&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    private external fun helloFromRust(): String&lt;br /&gt;
    private external fun calculateFibonacci(n: Int): String&lt;br /&gt;
    private val _resultText = MutableStateFlow(&amp;quot;Result will appear here&amp;quot;)&lt;br /&gt;
    val resultText: StateFlow&amp;lt;String&amp;gt; = _resultText.asStateFlow()&lt;br /&gt;
    fun sayHello() {&lt;br /&gt;
        _resultText.value = helloFromRust()&lt;br /&gt;
    }&lt;br /&gt;
    fun calculateFib(input: String) {&lt;br /&gt;
        val number = input.toIntOrNull() ?: 0&lt;br /&gt;
        _resultText.value = calculateFibonacci(number)&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
// MainActivity.kt&lt;br /&gt;
package com.example.myrustapp&lt;br /&gt;
import android.os.Bundle&lt;br /&gt;
import androidx.activity.ComponentActivity&lt;br /&gt;
import androidx.activity.compose.setContent&lt;br /&gt;
import androidx.activity.viewModels&lt;br /&gt;
import androidx.compose.foundation.layout.*&lt;br /&gt;
import androidx.compose.foundation.text.KeyboardOptions&lt;br /&gt;
import androidx.compose.material3.*&lt;br /&gt;
import androidx.compose.runtime.*&lt;br /&gt;
import androidx.compose.ui.Alignment&lt;br /&gt;
import androidx.compose.ui.Modifier&lt;br /&gt;
import androidx.compose.ui.text.input.KeyboardType&lt;br /&gt;
import androidx.compose.ui.text.style.TextAlign&lt;br /&gt;
import androidx.compose.ui.unit.dp&lt;br /&gt;
import androidx.lifecycle.compose.collectAsStateWithLifecycle&lt;br /&gt;
class MainActivity : ComponentActivity() {&lt;br /&gt;
    private val viewModel: RustViewModel by viewModels()&lt;br /&gt;
    override fun onCreate(savedInstanceState: Bundle?) {&lt;br /&gt;
        super.onCreate(savedInstanceState)&lt;br /&gt;
        setContent {&lt;br /&gt;
            RustAppTheme {&lt;br /&gt;
                Surface(&lt;br /&gt;
                    modifier = Modifier.fillMaxSize(),&lt;br /&gt;
                    color = MaterialTheme.colorScheme.background&lt;br /&gt;
                ) {&lt;br /&gt;
                    RustInteractionScreen(viewModel = viewModel)&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
@Composable&lt;br /&gt;
fun RustInteractionScreen(viewModel: RustViewModel) {&lt;br /&gt;
    val resultText by viewModel.resultText.collectAsStateWithLifecycle()&lt;br /&gt;
    var inputNumber by remember { mutableStateOf(&amp;quot;&amp;quot;) }&lt;br /&gt;
    Column(&lt;br /&gt;
        modifier = Modifier&lt;br /&gt;
            .fillMaxSize()&lt;br /&gt;
            .padding(16.dp),&lt;br /&gt;
        horizontalAlignment = Alignment.CenterHorizontally,&lt;br /&gt;
        verticalArrangement = Arrangement.Center&lt;br /&gt;
    ) {&lt;br /&gt;
        Card(&lt;br /&gt;
            modifier = Modifier&lt;br /&gt;
                .fillMaxWidth()&lt;br /&gt;
                .padding(bottom = 24.dp),&lt;br /&gt;
            elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)&lt;br /&gt;
        ) {&lt;br /&gt;
            Text(&lt;br /&gt;
                text = resultText,&lt;br /&gt;
                modifier = Modifier&lt;br /&gt;
                    .padding(16.dp)&lt;br /&gt;
                    .fillMaxWidth(),&lt;br /&gt;
                style = MaterialTheme.typography.bodyLarge,&lt;br /&gt;
                textAlign = TextAlign.Center&lt;br /&gt;
            )&lt;br /&gt;
        }&lt;br /&gt;
        OutlinedTextField(&lt;br /&gt;
            value = inputNumber,&lt;br /&gt;
            onValueChange = { inputNumber = it },&lt;br /&gt;
            label = { Text(&amp;quot;Enter a number&amp;quot;) },&lt;br /&gt;
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),&lt;br /&gt;
            modifier = Modifier&lt;br /&gt;
                .fillMaxWidth()&lt;br /&gt;
                .padding(bottom = 16.dp),&lt;br /&gt;
            singleLine = true&lt;br /&gt;
        )&lt;br /&gt;
        Button(&lt;br /&gt;
            onClick = { viewModel.sayHello() },&lt;br /&gt;
            modifier = Modifier&lt;br /&gt;
                .fillMaxWidth()&lt;br /&gt;
                .padding(bottom = 8.dp)&lt;br /&gt;
        ) {&lt;br /&gt;
            Text(&amp;quot;Say Hello from Rust&amp;quot;)&lt;br /&gt;
        }&lt;br /&gt;
        Button(&lt;br /&gt;
            onClick = { viewModel.calculateFib(inputNumber) },&lt;br /&gt;
            modifier = Modifier.fillMaxWidth()&lt;br /&gt;
        ) {&lt;br /&gt;
            Text(&amp;quot;Calculate Fibonacci&amp;quot;)&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
@Composable&lt;br /&gt;
fun RustAppTheme(content: @Composable () -&amp;gt; Unit) {&lt;br /&gt;
    MaterialTheme(content = content)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step 7: Build and Run&lt;br /&gt;
&lt;br /&gt;
Build and run your Android application. The app should successfully call the Rust functions and display the results.&lt;br /&gt;
&lt;br /&gt;
Best Practices&lt;br /&gt;
* 		Error Handling: Always handle potential errors in JNI calls using Result types and proper error propagation.&lt;br /&gt;
* 		Memory Management: Be cautious with memory management across the JNI boundary. Ensure proper cleanup of JNI references.&lt;br /&gt;
* 		Testing: Write comprehensive unit tests for your Rust code before integration.&lt;br /&gt;
* 		Build Automation: Consider using tools like cargo-ndk to automate the build process:&lt;br /&gt;
cargo install cargo-ndk cargo ndk --target aarch64-linux-android --android-platform 30 build --release&lt;br /&gt;
&lt;br /&gt;
5. Documentation: Document the interface between Rust and Java/Kotlin code clearly.&lt;br /&gt;
&lt;br /&gt;
6. Performance Profiling: Use Android Profiler to measure the performance impact of your Rust code.&lt;br /&gt;
&lt;br /&gt;
Troubleshooting&lt;br /&gt;
&lt;br /&gt;
Common Issues&lt;br /&gt;
&lt;br /&gt;
Library Not Found: Ensure the .so files are in the correct jniLibs subdirectories and match the architecture naming conventions.&lt;br /&gt;
&lt;br /&gt;
UnsatisfiedLinkError: Verify that the JNI function names match exactly, including the package name in the function signature.&lt;br /&gt;
&lt;br /&gt;
NDK Version Mismatch: Ensure your NDK version is compatible with the Android API level you’re targeting.&lt;br /&gt;
&lt;br /&gt;
Linker Errors: Check that the NDK linker paths in .cargo/config.toml are correct for your system.&lt;br /&gt;
&lt;br /&gt;
Conclusion&lt;br /&gt;
&lt;br /&gt;
Integrating Rust into Android development provides significant advantages in terms of memory safety, performance, and concurrency. While the initial setup requires careful configuration, the benefits of using Rust for performance-critical components make it a valuable addition to your Android development toolkit. As the ecosystem matures, tooling and integration processes continue to improve, making Rust an increasingly attractive option for Android developers.&lt;br /&gt;
&lt;br /&gt;
Read the full article here: https://medium.com/@ali.alacan/comprehensive-guide-to-using-rust-in-android-development-aae291005334&lt;/div&gt;</summary>
		<author><name>PC</name></author>
	</entry>
</feed>