Skip to content

Commit 3d204bb

Browse files
Made Linux setup for Android alot easier and fixed the Asset path. Tested in a fresh Ubuntu VM.
1 parent 3798288 commit 3d204bb

File tree

4 files changed

+138
-4
lines changed

4 files changed

+138
-4
lines changed

README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ cmake --build build -j8 --target run
3131
Linux users will need to install some pre-requisites for this template to compile.
3232

3333
```shell
34-
sudo apt-get update
35-
sudo apt-get install cmake libx11-dev libxfixes-dev libegl-dev libgbm-dev libfontconfig-dev
34+
sudo apt update
35+
sudo apt install cmake libx11-dev libxfixes-dev libegl-dev libgbm-dev libfontconfig-dev
3636
```
3737

3838
# Android
@@ -45,6 +45,53 @@ This template also supports building and running APKs for Android! Setup for thi
4545

4646
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.
4747

48+
49+
### If building on Ubuntu 24.04 onwards (CLI): Quick setup
50+
If you prefer the command line on Linux, this is a minimal setup that matches the versions this template targets.
51+
52+
### Auto-Setup:
53+
54+
Just run `bash install_android_deps.sh`. Everything will be installed for you.
55+
56+
### Manual Setup:
57+
<details>
58+
59+
```bash
60+
# 1) Base tools
61+
sudo apt update
62+
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
63+
64+
# 2) to rule out potential errors, we explicitly tell sdkmanager where to install the Android SDK & NDK
65+
66+
export ANDROID_HOME="$HOME/Android/Sdk"
67+
68+
sdkmanager --sdk_root=$ANDROID_HOME \
69+
"platform-tools" \
70+
"platforms;android-32" \
71+
"build-tools;32.0.0" \
72+
"ndk;25.2.9519653"
73+
74+
# 3) More environment variable setup...:
75+
76+
export ANDROID_SDK_ROOT="$ANDROID_HOME"
77+
export PATH="$ANDROID_HOME/platform-tools:$PATH"
78+
79+
# 5) Point CMake to the NDK and set JAVA_HOME (OpenJDK 8 on Ubuntu)
80+
export ANDROID_NDK_HOME="$ANDROID_HOME/ndk/25.2.9519653"
81+
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
82+
83+
# (Optional) Persist these to your shell profile (after install)
84+
echo 'export ANDROID_HOME="$HOME/Android/Sdk"' >> "$HOME/.bashrc"
85+
echo 'export ANDROID_SDK_ROOT="$HOME/Android/Sdk"' >> "$HOME/.bashrc"
86+
echo 'export ANDROID_NDK_HOME="$HOME/Android/Sdk/ndk/25.2.9519653"' >> "$HOME/.bashrc"
87+
echo 'export PATH="$ANDROID_HOME/platform-tools:$PATH"' >> "$HOME/.bashrc"
88+
echo 'export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"' >> "$HOME/.bashrc"
89+
```
90+
</details>
91+
92+
### Now you can continue to Android Build section.
93+
94+
4895
### Android SDKs
4996
From [Android Studio](https://developer.android.com/studio), go to Tools->SDK Manager.
5097
- 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
94141

95142
## Android Build
96143

144+
### for automated Android Build on Linux just run `build_android.sh`
145+
146+
147+
97148
```shell
98149
# From the project root directory
99150

@@ -102,6 +153,7 @@ mkdir build-android
102153

103154
# Configure the build, I'll often make a .bat file for this configure command
104155
# just to make it easier to do!
156+
105157
cmake -B build-android ^
106158
-G Ninja ^
107159
-DCMAKE_ANDROID_NDK=%ANDROID_NDK_HOME% ^
@@ -110,6 +162,11 @@ cmake -B build-android ^
110162
-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a
111163
# Same, but as a single line. Nicer if not using a .bat
112164
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
165+
166+
#Or for Linux:
167+
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
168+
169+
113170
# Build an APK, install, and run it
114171
cmake --build build-android -j8 --target run
115172
# Or just build an APK

android/AndroidBuild.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ add_custom_command(
178178
# Assemble the base APK, resources and assets
179179
add_custom_command(
180180
DEPENDS
181-
${CMAKE_SOURCE_DIR}/assets
181+
${CMAKE_SOURCE_DIR}/Assets
182182
${APK_TEMP}/obj/classes.dex
183183
${APK_TEMP}/obj/apk_resources.zip
184184
${APK_TEMP}/obj/AndroidManifest.xml
@@ -188,7 +188,7 @@ add_custom_command(
188188
COMMAND ${AAPT2} link # Link all the files into an APK
189189
-o ${APK_BASE}
190190
--manifest ${APK_TEMP}/obj/AndroidManifest.xml
191-
-A ${CMAKE_SOURCE_DIR}/assets
191+
-A ${CMAKE_SOURCE_DIR}/Assets
192192
-I ${ANDROID_SDK_ROOT}/platforms/android-${CMAKE_SYSTEM_VERSION}/android.jar
193193
${APK_TEMP}/obj/apk_resources.zip
194194
COMMAND cd ${APK_TEMP}/obj

build_android.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
# build_android_apk.sh: Configure and build or run the Android APK for StereoKit
3+
4+
set -e
5+
6+
# Prevent running with sudo/root so the SDK paths and adb device access use the user account
7+
if [ "$(id -u)" -eq 0 ]; then
8+
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
9+
echo "This script should not be run with sudo. Re-running as $SUDO_USER..."
10+
exec sudo -u "$SUDO_USER" -H bash "$0" "$@"
11+
else
12+
echo "Please run this script without sudo." >&2
13+
exit 1
14+
fi
15+
fi
16+
17+
export ANDROID_HOME="$HOME/Android/Sdk"
18+
export ANDROID_SDK_ROOT="$ANDROID_HOME"
19+
export PATH="$ANDROID_HOME/platform-tools:$PATH"
20+
21+
export ANDROID_NDK_HOME="$ANDROID_HOME/ndk/25.2.9519653"
22+
export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
23+
export PATH="$JAVA_HOME/bin:$PATH"
24+
25+
echo "Configuring Android build..."
26+
cmake -B build-android \
27+
-G Ninja \
28+
-DCMAKE_ANDROID_NDK="$ANDROID_NDK_HOME" \
29+
-DCMAKE_SYSTEM_NAME=Android \
30+
-DCMAKE_SYSTEM_VERSION=32 \
31+
-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a \
32+
-DJAVAC="$JAVA_HOME/bin/javac" \
33+
-DJava_JAVAC_EXECUTABLE="$JAVA_HOME/bin/javac"
34+
35+
echo "Choose build option:"
36+
select opt in "Build and run APK on device" "Build APK only"; do
37+
case $REPLY in
38+
1)
39+
cmake --build build-android -j8 --target run
40+
break
41+
;;
42+
2)
43+
cmake --build build-android -j8 --target apk
44+
break
45+
;;
46+
*)
47+
echo "Invalid option. Please select 1 or 2."
48+
;;
49+
esac
50+
done

install_android_deps.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# install_android-deps.sh: Install all dependencies for Android build on Linux
3+
4+
set -e
5+
6+
# Prevent running the whole script with sudo/root.
7+
# We still use sudo for apt installs below, but the SDK should be under the user home, not /root.
8+
if [ "$(id -u)" -eq 0 ]; then
9+
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
10+
echo "This script should not be run with sudo. Re-running as $SUDO_USER..."
11+
exec sudo -u "$SUDO_USER" -H bash "$0" "$@"
12+
else
13+
echo "Please run this script without sudo (it will use sudo only where needed)." >&2
14+
exit 1
15+
fi
16+
fi
17+
18+
sudo apt update
19+
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
20+
21+
export ANDROID_HOME="$HOME/Android/Sdk"
22+
23+
sdkmanager --sdk_root=$ANDROID_HOME \
24+
"platform-tools" \
25+
"platforms;android-32" \
26+
"build-tools;32.0.0" \
27+
"ndk;25.2.9519653"

0 commit comments

Comments
 (0)