Last active
November 16, 2024 12:47
-
-
Save gauravssnl/44173389b662f654721eba704c998389 to your computer and use it in GitHub Desktop.
Revisions
-
gauravssnl revised this gist
Nov 16, 2024 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,18 @@ TARGET_FILE=hello 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'" -
gauravssnl revised this gist
Nov 16, 2024 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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'" -
gauravssnl revised this gist
Nov 16, 2024 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,16 @@ 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 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'" -
gauravssnl revised this gist
Nov 16, 2024 . 2 changed files with 26 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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'" -
gauravssnl revised this gist
Nov 15, 2024 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. -
gauravssnl renamed this gist
Nov 15, 2024 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
gauravssnl revised this gist
Nov 15, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 ``` I found this step easier and simpler as we need not install anything for this and only one time config step is needed. -
gauravssnl created this gist
Nov 15, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.