Mobile Development 12 min read

Integrating Rust into Android: A Step-by-Step Guide

This article explains how to set up Rust for Android development, covering installation, creating a demo project, using JNI to call Rust functions, building the shared library, integrating it into an Android app, and designing a scalable architecture with event dispatch and Protobuf.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Integrating Rust into Android: A Step-by-Step Guide

This article introduces Rust's growing popularity and its suitability for performance‑critical and secure mobile applications, then provides a practical guide for integrating Rust into an Android project.

Initialization

Install Rust using the official script and create a new Cargo project:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Then run cargo new study_rust and open the project in an IDE (CLion, Android Studio, etc.).

Cross‑Platform Communication

Android calls Rust through the Java Native Interface (JNI). After adding the jni crate to Cargo.toml , you can write JNI functions in Rust that follow the same naming convention as C++ JNI functions.

Creating a JNI Function

Define a simple add function in lib.rs and expose it via JNI:

#[no_mangle]
pub extern "C" fn Java_com_example_myapp_RustBridge_add(env: JNIEnv, _: JClass, a: jint, b: jint) -> jint {
    a + b
}

Building the .so Library

Create .cargo/config to point to the Android NDK toolchain (linker and ar). Add the target platform, e.g., rustup target add aarch64-linux-android , then build:

cargo build --target aarch64-linux-android --release

The resulting libstudy_rust.so appears in target/aarch64-linux-android/release .

Android Integration

Load the generated .so in the Android app and declare the native method. When the app calls the native add method, the Rust implementation is executed and the result is returned.

Application Scenarios

Replace C++ native code with Rust for better safety and comparable performance.

Handle data‑intensive logic (network, database) in Rust using its async runtime.

Protect critical business logic from reverse engineering by shipping it as a Rust‑compiled .so .

Use Rust for cross‑platform libraries shared across iOS, Android, and other platforms.

Architecture Design

For large projects, expose a single generic JNI entry point (e.g., invoke ) that receives a command ID and Protobuf‑encoded payload. Inside Rust, a looper dispatches the command to the appropriate module, keeping the JNI surface small.

Example flow:

Java calls invoke(command, data) .

Rust decodes the Protobuf payload and routes it via a service map.

Result is encoded back into Protobuf and returned (or sent via a callback for async calls).

Asynchronous Callback Example

// RustCallback.java
package com.example.myapp;

public interface RustCallback {
    void onResponse(byte[] response);
}
pub extern "C" fn Java_com_example_myapp_RustBridge_invokeAsync(
    env: JNIEnv,
    _: JClass,
    command: jint,
    data: jbyteArray,
    java_callback: jobject,
) {
    let env = env.clone();
    let java_callback = env.new_global_ref(java_callback).unwrap();
    let runtime = RUNTIME.clone();
    runtime.lock().unwrap().spawn(async move {
        // ...process logic and produce result_data...
        let method_id = env.get_method_id(
            "com/example/myapp/RustCallback",
            "onResponse",
            "([B)V",
        ).unwrap();
        env.call_method(java_callback.as_obj(), method_id, result_data).unwrap();
    });
}

By storing the callback method ID globally, subsequent async calls avoid repeated look‑ups, simplifying the flow.

Overall, the guide demonstrates that integrating Rust into Android is straightforward and provides a solid foundation for building performant, safe, and maintainable mobile applications.

Mobile DevelopmentarchitectureandroidFFIRustJNIso
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.