Creating a "Hello World" Rust project for Android on Windows without using helper tools like cargo-ndk involves several steps. You'll need to manually configure the environment, cross-compile Rust code for Android's architecture targets, and then use the Android NDK to link your Rust code with an Android app. The steps below outline the process, explaining each stage in detail:
1. Install Prerequisites
a. Install Rust:
- Use
rustup to install Rust by downloading it from the official website.
- Run
rustup-init.exe and follow the on-screen instructions.
b. Install Android Studio:
- Download and install Android Studio from the Android developer site.
- The installation includes the Android SDK and Android NDK which are essential for Android development.
c. Install Java Development Kit (JDK):
- You need Java to run Gradle, which will build the Android app. Make sure you install JDK 8 or later.
2. Set Up Rust for Android Targets
Use rustup to add the Android architecture targets.
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android
3. Create Standalone NDK Toolchains
Inside Android Studio:
a. Open the SDK Manager:
- Configure the path to the Android SDK and NDK, found under "SDK Tools".
- Note down the NDK version, you will need it for the next step.
b. Create Standalone Toolchains:
- Use the
make_standalone_toolchain.py script provided by the NDK to create standalone toolchains for Rust to use. This needs to be done for each target architecture.
# Execute these commands in Command Prompt / PowerShell, one for each target architecture
# The paths below are dependent on the NDK version and SDK location
# You may need to update "ndk/21.3.6528147" with your specific NDK version and path
python %LOCALAPPDATA%\Android\Sdk\ndk\21.3.6528147\build\tools\make_standalone_toolchain.py --api 28 --arch arm64 --install-dir C:\NDK\arm64
python %LOCALAPPDATA%\Android\Sdk\ndk\21.3.6528147\build\tools\make_standalone_toolchain.py --api 28 --arch arm --install-dir C:\NDK\arm
# Repeat for `x86` and `x86_64` architectures with `--arch x86` and `--arch x86_64`
4. Configure Cargo for Cross-Compilation
Create a config file at .cargo/config in your Rust project or in the .cargo directory within your user home directory with the following content. Modify the linker paths based on where you created the standalone NDK toolchains:
[target.aarch64-linux-android]
linker = "C:\\NDK\\arm64\\bin\\aarch64-linux-android-gcc"
[target.armv7-linux-androideabi]
linker = "C:\\NDK\\arm\\bin\\arm-linux-androideabi-gcc"
# Add similar entries for `i686-linux-android` and `x86_64-linux-android` with appropriate paths
5. Write the Rust Code
Create a new Rust library with cargo new --lib rustlib and add the following to your src/lib.rs:
#[no_mangle]
pub extern "C" fn hello_from_rust() -> *const u8 {
b"Hello World from Rust!\0".as_ptr()
}
Add a Cargo.toml file if it doesn’t already exist and specify the crate type:
[lib]
crate-type = ["cdylib"]
6. Build the Rust Library for Android
Cross-compile the Rust library for Android targets:
# You will need to repeat this command for each target you're compiling for
# Replace `aarch64-linux-android` with appropriate target names (armv7, i686, x86_64)
cargo build --target aarch64-linux-android --release
7. Create an Android Project in Android Studio
Generate an Android app using the default "New Project" setup, or use an existing one. You'll be working mainly with MainActivity.java and build.gradle files.
8. Include the Rust Library in Your Android Project
Copy the compiled .so files from Rust's target/<arch>/release directory to your Android project's src/main/jniLibs/<arch> folder.
9. Call Rust Code from Java
Create the necessary JNI declarations in Java to interact with the Rust library:
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("rustlib");
}
public native String helloFromRust();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.sample_text);
tv.setText(helloFromRust());
}
}
10. Update build.gradle
Make sure Gradle includes the native libraries in the APK:
android {
// Existing configurations
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
}
11. Generate the JNI Header with javah (Optional)
This step is usually required if you have more complex interactions and need to ensure your JNI function signatures in Rust match those expected by Java:
# Navigate to the /app/build/intermediates/javac/debug/classes directory, then
javah -jni com.your.package.name.MainActivity
12. Test Your Android App
Run your app in an emulator or a real device, and it should display "Hello World from Rust!" using the Rust library.
This process is manual and requires understanding how to set up the Rust environment for cross-compilation, as well as integrating the resulting compiled libraries with an Android application through JNI. Although tools like cargo-ndk simplify the process, understanding these steps will give you a strong insight into how the low-level integration between Rust and Android works.