Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
<details>

```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"
```
</details>

### 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**
Expand Down Expand Up @@ -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

Expand All @@ -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% ^
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions android/AndroidBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
50 changes: 50 additions & 0 deletions build_android.sh
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions install_android_deps.sh
Original file line number Diff line number Diff line change
@@ -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"