Compiling and Linking Rust Code into an Android APK

Compiling and Linking Rust Code into an Android APK

This article explains how to compile and link Rust code into an Android APK using the popular toolchain: Android Studio, Gradle, and Cargo.

Prerequisites

  • Android Studio installed with the Android SDK.
  • Rust installed on your system (Rustup is recommended).
  • Android NDK installed and configured in Android Studio.
  • Basic knowledge of Rust and Android development.

Setting up the Project

1. Create a New Android Studio Project

Launch Android Studio and create a new project. Choose an Empty Compose Activity or Empty Activity template.

2. Configure Gradle Build Script

Modify the build script (build.gradle) to include Rust compilation and linking.

android {
  // ... other configurations
  externalNativeBuild {
    cmake {
      path "src/main/cpp/CMakeLists.txt"
    }
  }
}

dependencies {
  // ... other dependencies
  implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4" // Example dependency
}

3. Create a CMake File (CMakeLists.txt)

Create a CMakeLists.txt file in the src/main/cpp directory to manage Rust compilation and linking.

cmake_minimum_required(VERSION 3.10)

add_library(my_rust_library SHARED src/main/rust/my_rust_library)
target_include_directories(my_rust_library PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/main/rust)

target_link_libraries(my_rust_library
  ${ANDROID_LOG_LIBRARY}
  ${CMAKE_ANDROID_STL_STATIC}
)

4. Configure Cargo Project (Cargo.toml)

Create a Cargo.toml file in the src/main/rust directory to manage the Rust project dependencies.

[package]
name = "my_rust_library"
version = "0.1.0"
authors = ["Your Name"]
edition = "2021"

[dependencies]
# Add any necessary Rust dependencies here.

Writing Rust Code

Create a .rs file in the src/main/rust directory, containing your Rust code. Make sure to expose functions for interaction with the Java layer.

#[no_mangle]
pub extern "C" fn rust_function(value: i32) -> i32 {
    value + 10
}

Java Interaction

Implement a Java class to interact with the Rust code.

package com.example.myandroidapp;

public class RustWrapper {
  static {
    System.loadLibrary("my_rust_library");
  }

  public static native int rustFunction(int value);
}

Building the APK

Once you’ve set up the project, run the following command in the terminal to build your APK:

./gradlew assembleDebug

Running the Application

Install the APK on your Android device or emulator, and run the application. You should now be able to interact with your Rust code from Java.

Considerations

  • Dependency Management: Carefully manage Rust dependencies to avoid conflicts with Android NDK libraries.
  • JNI: Use Java Native Interface (JNI) to call Rust functions from Java.
  • Memory Management: Handle memory management in Rust carefully to prevent memory leaks.
  • Android Architecture: Consider how to integrate Rust code with your Android application’s architecture.

Example Project

For a complete example, refer to the Android NDK samples available on the Android Developers website.

Summary

By leveraging Android Studio, Gradle, CMake, and Cargo, you can effectively compile and link Rust code into an Android APK, enabling you to utilize Rust’s performance and safety features in your Android applications.


Leave a Reply

Your email address will not be published. Required fields are marked *