How to make an Android NDK shared library with Rust?


#1

Hi,I want to write a shared library to boost the performance of my Android project,Can someone give an example how to write a shared library in Rust?Thanks。


#2

Hi,

First create a new project in Cargo (cargo new test_sharedlib) Modify the Cargo.toml file and add the following lines

   [lib]
   name = "mylib"
   crate-type = ["dylib"]

Run cargo build and you should now see a file in target/debug/ being called something like mylib.so/dll/etc depending on your platform.

Now in your src/lib.rs you likely want some entry symbol/data so you will need to mark that like this

 #[no_mangle]
 pub fn call_me() {
   ...
 }

Hopefully that should get you going.

Cheers!


#3

Android is running on different architectures, including ARM, Intel Atom, etc.

You have to cross compile your program for every arch.

Everything you need to know about cross compiling Rust programs!
rust-cross