Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Ignore all shell scripts in the root directory
/*.sh

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably shouldn't be in the template by default. You've actually still got install_android_deps.sh in the root of this commit, likely because of this line!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to effectively prevent new sh files to be added to the main directory, but not the current one - that has been deleted now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, looks like you got install_android_deps.sh now.

Keeping .sh files out of the root directory is only to keep the template tidy for developers! IMO, templates should always have pretty clear and universally relevant roots. Devs can put whatever files they want in the root once they start developing, so I don't want to block inclusion of .sh files here.

.vs/
.vscode/
.idea/
Expand Down
57 changes: 55 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.

### (Optional) Auto-Setup:

Just run `bash install_android_deps.sh` located in android/scripts/. 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 @@ -102,6 +149,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 +158,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 (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


# 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
53 changes: 53 additions & 0 deletions android/scripts/install_android_deps.sh
Original file line number Diff line number Diff line change
@@ -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