From 3d204bbb212eafc79e0019718645f2a0e0f4548d Mon Sep 17 00:00:00 2001 From: Dennis <39258692+realThirdpartycookie@users.noreply.github.com> Date: Sun, 24 Aug 2025 21:44:11 +0000 Subject: [PATCH 1/2] Made Linux setup for Android alot easier and fixed the Asset path. Tested in a fresh Ubuntu VM. --- README.md | 61 ++++++++++++++++++++++++++++++++++++-- android/AndroidBuild.cmake | 4 +-- build_android.sh | 50 +++++++++++++++++++++++++++++++ install_android_deps.sh | 27 +++++++++++++++++ 4 files changed, 138 insertions(+), 4 deletions(-) create mode 100755 build_android.sh create mode 100755 install_android_deps.sh diff --git a/README.md b/README.md index 225447b..fa0529d 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,8 @@ cmake --build build -j8 --target run Linux users will need to install some pre-requisites for this template to compile. ```shell -sudo apt-get update -sudo apt-get install cmake libx11-dev libxfixes-dev libegl-dev libgbm-dev libfontconfig-dev +sudo apt update +sudo apt install cmake libx11-dev libxfixes-dev libegl-dev libgbm-dev libfontconfig-dev ``` # Android @@ -45,6 +45,53 @@ This template also supports building and running APKs for Android! Setup for thi To build for Android, you need a few SDKs! [Android Studio](https://developer.android.com/studio) has a good interface for grabbing these, and doubles as a nice tool for inspecting APKs. + +### If building on Ubuntu 24.04 onwards (CLI): Quick setup +If you prefer the command line on Linux, this is a minimal setup that matches the versions this template targets. + +### Auto-Setup: + +Just run `bash install_android_deps.sh`. Everything will be installed for you. + +### Manual Setup: +
+ +```bash +# 1) Base tools +sudo apt update +sudo apt install cmake libx11-dev libxfixes-dev libegl-dev libgbm-dev libfontconfig-dev unzip curl zip ninja-build openjdk-8-jdk adb google-android-cmdline-tools-13.0-installer + +# 2) to rule out potential errors, we explicitly tell sdkmanager where to install the Android SDK & NDK + +export ANDROID_HOME="$HOME/Android/Sdk" + +sdkmanager --sdk_root=$ANDROID_HOME \ + "platform-tools" \ + "platforms;android-32" \ + "build-tools;32.0.0" \ + "ndk;25.2.9519653" + +# 3) More environment variable setup...: + +export ANDROID_SDK_ROOT="$ANDROID_HOME" +export PATH="$ANDROID_HOME/platform-tools:$PATH" + +# 5) Point CMake to the NDK and set JAVA_HOME (OpenJDK 8 on Ubuntu) +export ANDROID_NDK_HOME="$ANDROID_HOME/ndk/25.2.9519653" +export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" + +# (Optional) Persist these to your shell profile (after install) +echo 'export ANDROID_HOME="$HOME/Android/Sdk"' >> "$HOME/.bashrc" +echo 'export ANDROID_SDK_ROOT="$HOME/Android/Sdk"' >> "$HOME/.bashrc" +echo 'export ANDROID_NDK_HOME="$HOME/Android/Sdk/ndk/25.2.9519653"' >> "$HOME/.bashrc" +echo 'export PATH="$ANDROID_HOME/platform-tools:$PATH"' >> "$HOME/.bashrc" +echo 'export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"' >> "$HOME/.bashrc" +``` +
+ +### Now you can continue to Android Build section. + + ### Android SDKs From [Android Studio](https://developer.android.com/studio), go to Tools->SDK Manager. - Under SDK Platforms, add **API Level 32** @@ -94,6 +141,10 @@ Ninja's [site is here](https://ninja-build.org/), but you can install it quite e ## Android Build +### for automated Android Build on Linux just run `build_android.sh` + + + ```shell # From the project root directory @@ -102,6 +153,7 @@ mkdir build-android # Configure the build, I'll often make a .bat file for this configure command # just to make it easier to do! + cmake -B build-android ^ -G Ninja ^ -DCMAKE_ANDROID_NDK=%ANDROID_NDK_HOME% ^ @@ -110,6 +162,11 @@ cmake -B build-android ^ -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a # Same, but as a single line. Nicer if not using a .bat cmake -B build-android -G Ninja -DCMAKE_ANDROID_NDK=%ANDROID_NDK_HOME% -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=32 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a + +#Or for Linux: +cmake -B build-android -G Ninja -DCMAKE_ANDROID_NDK=$ANDROID_NDK_HOME -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=32 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a + + # Build an APK, install, and run it cmake --build build-android -j8 --target run # Or just build an APK diff --git a/android/AndroidBuild.cmake b/android/AndroidBuild.cmake index 075dc97..5e11a82 100644 --- a/android/AndroidBuild.cmake +++ b/android/AndroidBuild.cmake @@ -178,7 +178,7 @@ add_custom_command( # Assemble the base APK, resources and assets add_custom_command( DEPENDS - ${CMAKE_SOURCE_DIR}/assets + ${CMAKE_SOURCE_DIR}/Assets ${APK_TEMP}/obj/classes.dex ${APK_TEMP}/obj/apk_resources.zip ${APK_TEMP}/obj/AndroidManifest.xml @@ -188,7 +188,7 @@ add_custom_command( COMMAND ${AAPT2} link # Link all the files into an APK -o ${APK_BASE} --manifest ${APK_TEMP}/obj/AndroidManifest.xml - -A ${CMAKE_SOURCE_DIR}/assets + -A ${CMAKE_SOURCE_DIR}/Assets -I ${ANDROID_SDK_ROOT}/platforms/android-${CMAKE_SYSTEM_VERSION}/android.jar ${APK_TEMP}/obj/apk_resources.zip COMMAND cd ${APK_TEMP}/obj diff --git a/build_android.sh b/build_android.sh new file mode 100755 index 0000000..93a5d27 --- /dev/null +++ b/build_android.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# build_android_apk.sh: Configure and build or run the Android APK for StereoKit + +set -e + +# Prevent running with sudo/root so the SDK paths and adb device access use the user account +if [ "$(id -u)" -eq 0 ]; then + if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then + echo "This script should not be run with sudo. Re-running as $SUDO_USER..." + exec sudo -u "$SUDO_USER" -H bash "$0" "$@" + else + echo "Please run this script without sudo." >&2 + exit 1 + fi +fi + +export ANDROID_HOME="$HOME/Android/Sdk" +export ANDROID_SDK_ROOT="$ANDROID_HOME" +export PATH="$ANDROID_HOME/platform-tools:$PATH" + +export ANDROID_NDK_HOME="$ANDROID_HOME/ndk/25.2.9519653" +export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" +export PATH="$JAVA_HOME/bin:$PATH" + +echo "Configuring Android build..." +cmake -B build-android \ + -G Ninja \ + -DCMAKE_ANDROID_NDK="$ANDROID_NDK_HOME" \ + -DCMAKE_SYSTEM_NAME=Android \ + -DCMAKE_SYSTEM_VERSION=32 \ + -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \ + -DJAVAC="$JAVA_HOME/bin/javac" \ + -DJava_JAVAC_EXECUTABLE="$JAVA_HOME/bin/javac" + +echo "Choose build option:" +select opt in "Build and run APK on device" "Build APK only"; do + case $REPLY in + 1) + cmake --build build-android -j8 --target run + break + ;; + 2) + cmake --build build-android -j8 --target apk + break + ;; + *) + echo "Invalid option. Please select 1 or 2." + ;; + esac +done diff --git a/install_android_deps.sh b/install_android_deps.sh new file mode 100755 index 0000000..4f677f6 --- /dev/null +++ b/install_android_deps.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# install_android-deps.sh: Install all dependencies for Android build on Linux + +set -e + +# Prevent running the whole script with sudo/root. +# We still use sudo for apt installs below, but the SDK should be under the user home, not /root. +if [ "$(id -u)" -eq 0 ]; then + if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then + echo "This script should not be run with sudo. Re-running as $SUDO_USER..." + exec sudo -u "$SUDO_USER" -H bash "$0" "$@" + else + echo "Please run this script without sudo (it will use sudo only where needed)." >&2 + exit 1 + fi +fi + +sudo apt update +sudo apt install -y cmake libx11-dev libxfixes-dev libegl-dev libgbm-dev libfontconfig-dev unzip curl zip ninja-build openjdk-8-jdk adb google-android-cmdline-tools-13.0-installer + +export ANDROID_HOME="$HOME/Android/Sdk" + +sdkmanager --sdk_root=$ANDROID_HOME \ + "platform-tools" \ + "platforms;android-32" \ + "build-tools;32.0.0" \ + "ndk;25.2.9519653" From 936940efc4dbd19499ae5d3473b1edbec30eb366 Mon Sep 17 00:00:00 2001 From: thirdpartycookie Date: Mon, 25 Aug 2025 23:54:37 +0200 Subject: [PATCH 2/2] Move the dependency installer script and remove build_android.sh script and update .gitignore to include shell files in the root dir. --- .gitignore | 3 ++ README.md | 10 ++--- android/scripts/install_android_deps.sh | 53 +++++++++++++++++++++++++ build_android.sh | 50 ----------------------- install_android_deps.sh | 27 ------------- 5 files changed, 59 insertions(+), 84 deletions(-) create mode 100755 android/scripts/install_android_deps.sh delete mode 100755 build_android.sh delete mode 100755 install_android_deps.sh diff --git a/.gitignore b/.gitignore index fdb4712..1910da0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Ignore all shell scripts in the root directory +/*.sh + .vs/ .vscode/ .idea/ diff --git a/README.md b/README.md index fa0529d..b702526 100644 --- a/README.md +++ b/README.md @@ -49,9 +49,9 @@ To build for Android, you need a few SDKs! [Android Studio](https://developer.an ### If building on Ubuntu 24.04 onwards (CLI): Quick setup If you prefer the command line on Linux, this is a minimal setup that matches the versions this template targets. -### Auto-Setup: +### (Optional) Auto-Setup: -Just run `bash install_android_deps.sh`. Everything will be installed for you. +Just run `bash install_android_deps.sh` located in android/scripts/. Everything will be installed for you! ### Manual Setup:
@@ -141,10 +141,6 @@ Ninja's [site is here](https://ninja-build.org/), but you can install it quite e ## Android Build -### for automated Android Build on Linux just run `build_android.sh` - - - ```shell # From the project root directory @@ -163,7 +159,7 @@ cmake -B build-android ^ # Same, but as a single line. Nicer if not using a .bat cmake -B build-android -G Ninja -DCMAKE_ANDROID_NDK=%ANDROID_NDK_HOME% -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=32 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a -#Or for Linux: +# Or for Linux (notice the $ instead of the %): cmake -B build-android -G Ninja -DCMAKE_ANDROID_NDK=$ANDROID_NDK_HOME -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=32 -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a diff --git a/android/scripts/install_android_deps.sh b/android/scripts/install_android_deps.sh new file mode 100755 index 0000000..3ff06c4 --- /dev/null +++ b/android/scripts/install_android_deps.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# install_android-deps.sh: Install all dependencies for Android build on Linux + +set -e + +# Prevent running the whole script with sudo/root. +# We still use sudo for apt installs below, but the SDK should be under the user home, not /root. +if [ "$(id -u)" -eq 0 ]; then + if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then + echo "This script should not be run with sudo. Re-running as $SUDO_USER..." + exec sudo -u "$SUDO_USER" -H bash "$0" "$@" + else + echo "Please run this script without sudo (it will use sudo only where needed)." >&2 + exit 1 + fi +fi + +sudo apt update +sudo apt install -y cmake libx11-dev libxfixes-dev libegl-dev libgbm-dev libfontconfig-dev unzip curl zip ninja-build openjdk-8-jdk adb google-android-cmdline-tools-13.0-installer + +export ANDROID_HOME="$HOME/Android/Sdk" +export ANDROID_SDK_ROOT="$ANDROID_HOME" +export PATH="$ANDROID_HOME/platform-tools:$PATH" + +export ANDROID_NDK_HOME="$ANDROID_HOME/ndk/25.2.9519653" +export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" +export PATH="$JAVA_HOME/bin:$PATH" + + +sdkmanager --sdk_root=$ANDROID_HOME \ + "platform-tools" \ + "platforms;android-32" \ + "build-tools;32.0.0" \ + "ndk;25.2.9519653" + + +echo +read -p "Add Android environment variables to your ~/.bashrc for future sessions? If not, you will need to set them manually. [y/N] " add_envs +if [[ "$add_envs" =~ ^[Yy]$ ]]; then + { + echo '' + echo '# Android SDK/NDK environment variables (added by install_android_deps.sh)' + echo 'export ANDROID_HOME="$HOME/Android/Sdk"' + echo 'export ANDROID_SDK_ROOT="$ANDROID_HOME"' + echo 'export ANDROID_NDK_HOME="$ANDROID_HOME/ndk/25.2.9519653"' + echo 'export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"' + echo 'export PATH="$ANDROID_HOME/platform-tools:$JAVA_HOME/bin:$PATH"' + } >> "$HOME/.bashrc" + echo "Variables added to ~/.bashrc. Please restart your terminal or run: source ~/.bashrc" +else + echo "Skipped adding environment variables to ~/.bashrc." +fi + diff --git a/build_android.sh b/build_android.sh deleted file mode 100755 index 93a5d27..0000000 --- a/build_android.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash -# build_android_apk.sh: Configure and build or run the Android APK for StereoKit - -set -e - -# Prevent running with sudo/root so the SDK paths and adb device access use the user account -if [ "$(id -u)" -eq 0 ]; then - if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then - echo "This script should not be run with sudo. Re-running as $SUDO_USER..." - exec sudo -u "$SUDO_USER" -H bash "$0" "$@" - else - echo "Please run this script without sudo." >&2 - exit 1 - fi -fi - -export ANDROID_HOME="$HOME/Android/Sdk" -export ANDROID_SDK_ROOT="$ANDROID_HOME" -export PATH="$ANDROID_HOME/platform-tools:$PATH" - -export ANDROID_NDK_HOME="$ANDROID_HOME/ndk/25.2.9519653" -export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" -export PATH="$JAVA_HOME/bin:$PATH" - -echo "Configuring Android build..." -cmake -B build-android \ - -G Ninja \ - -DCMAKE_ANDROID_NDK="$ANDROID_NDK_HOME" \ - -DCMAKE_SYSTEM_NAME=Android \ - -DCMAKE_SYSTEM_VERSION=32 \ - -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \ - -DJAVAC="$JAVA_HOME/bin/javac" \ - -DJava_JAVAC_EXECUTABLE="$JAVA_HOME/bin/javac" - -echo "Choose build option:" -select opt in "Build and run APK on device" "Build APK only"; do - case $REPLY in - 1) - cmake --build build-android -j8 --target run - break - ;; - 2) - cmake --build build-android -j8 --target apk - break - ;; - *) - echo "Invalid option. Please select 1 or 2." - ;; - esac -done diff --git a/install_android_deps.sh b/install_android_deps.sh deleted file mode 100755 index 4f677f6..0000000 --- a/install_android_deps.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash -# install_android-deps.sh: Install all dependencies for Android build on Linux - -set -e - -# Prevent running the whole script with sudo/root. -# We still use sudo for apt installs below, but the SDK should be under the user home, not /root. -if [ "$(id -u)" -eq 0 ]; then - if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then - echo "This script should not be run with sudo. Re-running as $SUDO_USER..." - exec sudo -u "$SUDO_USER" -H bash "$0" "$@" - else - echo "Please run this script without sudo (it will use sudo only where needed)." >&2 - exit 1 - fi -fi - -sudo apt update -sudo apt install -y cmake libx11-dev libxfixes-dev libegl-dev libgbm-dev libfontconfig-dev unzip curl zip ninja-build openjdk-8-jdk adb google-android-cmdline-tools-13.0-installer - -export ANDROID_HOME="$HOME/Android/Sdk" - -sdkmanager --sdk_root=$ANDROID_HOME \ - "platform-tools" \ - "platforms;android-32" \ - "build-tools;32.0.0" \ - "ndk;25.2.9519653"