Skip to content

Instantly share code, notes, and snippets.

@gauravssnl
Last active November 16, 2024 12:47
Show Gist options
  • Save gauravssnl/44173389b662f654721eba704c998389 to your computer and use it in GitHub Desktop.
Save gauravssnl/44173389b662f654721eba704c998389 to your computer and use it in GitHub Desktop.

Revisions

  1. gauravssnl revised this gist Nov 16, 2024. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion install.sh
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,18 @@
    TARGET_FILE=hello
    ABI=arm64-v8a
    ANDROID_TARGET_DIR=target/android
    ADB_FOLDER_NAME=gauravssnl
    PHONE_TARGET_DIR=/data/adb/$ADB_FOLDER_NAME/
    PHONE_TARGET_FILE=$PHONE_TARGET_DIR/$TARGET_FILE
    PHONE_TEMP_FILE=/sdcard/$TARGET_FILE

    echo "Executing commands on Android phone via ADB"
    adb devices -l

    echo "Reading device ABI"
    ABI=$(adb shell getprop ro.product.cpu.abi)
    echo "Device ABI : $ABI"

    echo "Pushing the files to $PHONE_TARGET_DIR"
    adb shell "su -c 'mkdir $PHONE_TARGET_DIR'"
    adb push $ANDROID_TARGET_DIR/$ABI/$TARGET_FILE $PHONE_TEMP_FILE
    adb shell "su -c 'mv $PHONE_TEMP_FILE $PHONE_TARGET_DIR'"
  2. gauravssnl revised this gist Nov 16, 2024. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,6 @@ PHONE_TEMP_FILE=/sdcard/$TARGET_FILE
    echo "Executing commands on Android phone via ADB"

    adb shell "su -c 'mkdir $PHONE_TARGET_DIR'"


    adb push $ANDROID_TARGET_DIR/$ABI/$TARGET_FILE $PHONE_TEMP_FILE
    adb shell "su -c 'mv $PHONE_TEMP_FILE $PHONE_TARGET_DIR'"
    adb shell "su -c 'chmod a+x $PHONE_TARGET_FILE && $PHONE_TARGET_FILE'"
  3. gauravssnl revised this gist Nov 16, 2024. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,16 @@
    TARGET_FILE=hello
    ABI=arm64-v8a
    ANDROID_TARGET_DIR=target/android
    PHONE_TARGET_DIR=/data/adb/gauravssnl
    ADB_FOLDER_NAME=gauravssnl
    PHONE_TARGET_DIR=/data/adb/$ADB_FOLDER_NAME/
    PHONE_TARGET_FILE=$PHONE_TARGET_DIR/$TARGET_FILE
    PHONE_TEMP_FILE=/sdcard/$TARGET_FILE

    echo "Executing commands on Android phone via ADB"

    adb shell "su -c 'mkdir $PHONE_TARGET_DIR'"

    PHONE_TEMP_FILE=/sdcard/$TARGET_FILE

    adb push $ANDROID_TARGET_DIR/$ABI/$TARGET_FILE $PHONE_TEMP_FILE
    adb shell "su -c 'mv $PHONE_TEMP_FILE $PHONE_TARGET_FILE'"
    adb shell "su -c 'mv $PHONE_TEMP_FILE $PHONE_TARGET_DIR'"
    adb shell "su -c 'chmod a+x $PHONE_TARGET_FILE && $PHONE_TARGET_FILE'"
  4. gauravssnl revised this gist Nov 16, 2024. 2 changed files with 26 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions build_release.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    export NDK_HOME_TOOLCHAIN_BIN="$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin"

    cargo clean

    RUSTFLAGS="-C linker=${NDK_HOME_TOOLCHAIN_BIN}/aarch64-linux-android30-clang" cargo build --release --target aarch64-linux-android
    RUSTFLAGS="-C linker=${NDK_HOME_TOOLCHAIN_BIN}/armv7a-linux-androideabi30-clang" cargo build --release --target armv7-linux-androideabi
    RUSTFLAGS="-C linker=${NDK_HOME_TOOLCHAIN_BIN}/x86_64-linux-android30-clang" cargo build --release --target x86_64-linux-android
    RUSTFLAGS="-C linker=${NDK_HOME_TOOLCHAIN_BIN}/i686-linux-android30-clang" cargo build --release --target i686-linux-android

    ANDROID_TARGET_DIR=target/android
    TARGET_FILE_NAME=hello

    mkdir -p $ANDROID_TARGET_DIR/arm64-v8a/ && cp target/aarch64-linux-android/release/$TARGET_FILE_NAME $ANDROID_TARGET_DIR/arm64-v8a/$TARGET_FILE_NAME
    mkdir -p $ANDROID_TARGET_DIR/armeabi-v7a/ && cp target/armv7-linux-androideabi/release/$TARGET_FILE_NAME $ANDROID_TARGET_DIR/armeabi-v7a/$TARGET_FILE_NAME
    mkdir -p $ANDROID_TARGET_DIR/x86-64/ && cp target/x86_64-linux-android/release/$TARGET_FILE_NAME $ANDROID_TARGET_DIR/x86-64/$TARGET_FILE_NAME
    mkdir -p $ANDROID_TARGET_DIR/x86/ && cp target/i686-linux-android/release/$TARGET_FILE_NAME $ANDROID_TARGET_DIR/x86/$TARGET_FILE_NAME
    10 changes: 10 additions & 0 deletions install.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    TARGET_FILE=hello
    ABI=arm64-v8a
    ANDROID_TARGET_DIR=target/android
    PHONE_TARGET_DIR=/data/adb/gauravssnl
    PHONE_TARGET_FILE=$PHONE_TARGET_DIR/$TARGET_FILE
    adb shell "su -c 'mkdir $PHONE_TARGET_DIR'"

    PHONE_TEMP_FILE=/sdcard/$TARGET_FILE
    adb push $ANDROID_TARGET_DIR/$ABI/$TARGET_FILE $PHONE_TEMP_FILE
    adb shell "su -c 'mv $PHONE_TEMP_FILE $PHONE_TARGET_FILE'"
  5. gauravssnl revised this gist Nov 15, 2024. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions steps.md
    Original file line number Diff line number Diff line change
    @@ -28,12 +28,19 @@ d. Use build.sh script in each project as provided above.

    References :
    https://gendignoux.com/blog/2022/10/24/rust-library-android.html

    https://github.com/bbqsrc/cargo-ndk

    https://maciejglowka.com/blog/building-games-for-android-with-rust/

    https://github.com/mozilla/rust-android-gradle

    https://medium.com/@1ok1/android-rust-ndk-starter-dfb6f739ceed

    https://www.reddit.com/r/rust/comments/16ngvhx/how_to_contol_the_link_stage_of_rust_build_system/

    https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#target-applies-to-host

    https://github.com/rust-lang/cargo/issues/1109

    There were many others pages which I read.
  6. gauravssnl renamed this gist Nov 15, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. gauravssnl revised this gist Nov 15, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion steps.md
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ c. Add the target linker paths for targets in ~/.cargo/config.toml. (Easiest way
    Now, we can compile programs directl with cargo , Example :
    ```bash
    cargo build --target aarch64-linux-android --target armv7-linux-androideabi --target x86_64-linux-android --target i686-linux-android --target x86_64-unknown-linux-gnu
    ```bash
    ```

    I found this step easier and simpler as we need not install anything for this and only one time config step is needed.

  8. gauravssnl created this gist Nov 15, 2024.
    6 changes: 6 additions & 0 deletions build.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    export NDK_HOME_TOOLCHAIN_BIN="$NDK_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin"

    RUSTFLAGS="-C linker=${NDK_HOME_TOOLCHAIN_BIN}/aarch64-linux-android30-clang" cargo build --target aarch64-linux-android
    RUSTFLAGS="-C linker=${NDK_HOME_TOOLCHAIN_BIN}/armv7a-linux-androideabi30-clang" cargo build --target armv7-linux-androideabi
    RUSTFLAGS="-C linker=${NDK_HOME_TOOLCHAIN_BIN}/x86_64-linux-android30-clang" cargo build --target x86_64-linux-android
    RUSTFLAGS="-C linker=${NDK_HOME_TOOLCHAIN_BIN}/i686-linux-android30-clang" cargo build --target i686-linux-android
    15 changes: 15 additions & 0 deletions config.toml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    # ~/.cargo/config.toml

    # target-applies-to-host = false

    [target.aarch64-linux-android]
    linker = "/home/gauravssnl/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android30-clang"

    [target.armv7-linux-androideabi]
    linker = "/home/gauravssnl/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi30-clang"

    [target.i686-linux-android]
    linker = "/home/gauravssnl/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android30-clang"

    [target.x86_64-linux-android]
    linker = "/home/gauravssnl/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android30-clang"
    41 changes: 41 additions & 0 deletions steps.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    1. Install Android SDK & NDK. Set Up ANDROID_HOME & NDK_HOME environment variables.
    2. We need to use nightly Rust & add targest related to Android. Run the below comamnds :

    ```bash
    rustup target add armv7-linux-androideabi # for arm
    rustup target add i686-linux-android # for x86
    rustup target add aarch64-linux-android # for arm64
    rustup target add x86_64-linux-android # for x86_64

    rustup override set nightly
    ```

    Now, we can follow miltiple ways to cross compile Rust code for Android OS.

    a. Use cargo-ndk : https://github.com/bbqsrc/cargo-ndk ( simple & easy)

    b. Use Mozilla gradle plugin(suitable for Android project) : https://github.com/mozilla/rust-android-gradle

    c. Add the target linker paths for targets in ~/.cargo/config.toml. (Easiest way for anyone; now we need to configure anything in actual project config).
    Now, we can compile programs directl with cargo , Example :
    ```bash
    cargo build --target aarch64-linux-android --target armv7-linux-androideabi --target x86_64-linux-android --target i686-linux-android --target x86_64-unknown-linux-gnu
    ```bash
    I found this step easier and simpler as we need not install anything for this and only one time config step is needed.
    d. Use build.sh script in each project as provided above.
    References :
    https://gendignoux.com/blog/2022/10/24/rust-library-android.html
    https://github.com/bbqsrc/cargo-ndk
    https://maciejglowka.com/blog/building-games-for-android-with-rust/
    https://github.com/mozilla/rust-android-gradle
    https://medium.com/@1ok1/android-rust-ndk-starter-dfb6f739ceed
    https://www.reddit.com/r/rust/comments/16ngvhx/how_to_contol_the_link_stage_of_rust_build_system/
    https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#target-applies-to-host
    https://github.com/rust-lang/cargo/issues/1109
    There were many others pages which I read.