Skip to content

Commit

Permalink
Android port
Browse files Browse the repository at this point in the history
Fixed compiler errors for Android NDK clang compiler
Added drop in replacement for std::jthread for Android NDK clang compiler
Added a gradle sub project in the androidplugin directory. This integrates the Java part of the Tilt Five SDK for Android.
In TileFiveXRInterface test the Android platform and load the platform context from the Java plugin
Handle pause and resume notifications from Android
Added Android export in plugin.gd to gather .arr files
Enabled Android build in SConstruct
Shell out new gradle build in SConstruct
Update github CI to assemble zip file with the new Android files.
  • Loading branch information
patrickdown committed Mar 22, 2024
1 parent a0207ea commit 096f83a
Show file tree
Hide file tree
Showing 32 changed files with 1,163 additions and 69 deletions.
78 changes: 61 additions & 17 deletions .github/workflows/build-on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
platform: windows
artifact-name: gdtiltfive.windows
artifact-path: |
build/bin/libgdtiltfive.windows.template_debug.x86_64.dll
build/bin/libgdtiltfive.windows.template_release.x86_64.dll
build/bin/windows/x86_64/libgdtiltfive.windows.template_debug.x86_64.dll
build/bin/windows/x86_64/libgdtiltfive.windows.template_release.x86_64.dll
extension/TiltFiveNDK/lib/win/x86_64/TiltFiveNative.dll
#- name: 🍎 macOS (universal)
Expand All @@ -39,12 +39,14 @@ jobs:
# artifact-name: gdtiltfive.macos
# artifact-path: build/bin/libgdtiltfive.macos.*

#- name: 🤖 Android (arm64)
# os: ubuntu-20.04
# platform: android
# flags: ANDROID_NDK_ROOT=$ANDROID_NDK_LATEST_HOME arch=arm64
# artifact-name: gdtiltfive.android
# artifact-path: build/bin/libgdtiltfive.android.*
- name: 🤖 Android (arm64)
os: ubuntu-20.04
platform: android
#flags: ANDROID_NDK_ROOT=$ANDROID_NDK_LATEST_HOME arch=arm64
artifact-name: gdtiltfive.android
artifact-path: |
build/bin/android/arm64/libgdtiltfive.android.*
androidplugin/plugin/build/outputs/aar/gdtiltfive*
#- name: 🍏 iOS (arm64)
# os: macos-11
Expand Down Expand Up @@ -72,17 +74,45 @@ jobs:
sudo apt-get update -qq
sudo apt-get install -qqq build-essential pkg-config
- name: Android dependencies
if: ${{ matrix.platform == 'android' }}
uses: nttld/setup-ndk@v1
id: setup-ndk
with:
ndk-version: r26c
add-to-path: false

- name: Install scons
run: |
python -m pip install scons==4.0.0
python -m pip install scons==4.0.0
- name: Build debug build
run: |
scons platform=${{ matrix.platform }} target=template_debug ${{ matrix.flags }}
scons platform=${{ matrix.platform }} target=template_debug ${{ matrix.flags }} --no-gradle
env:
ANDROID_NDK_ROOT: ${{ steps.setup-ndk.outputs.ndk-path }}

- name: Build release build
run: |
scons platform=${{ matrix.platform }} target=template_release ${{ matrix.flags }}
scons platform=${{ matrix.platform }} target=template_release ${{ matrix.flags }} --no-gradle
env:
ANDROID_NDK_ROOT: ${{ steps.setup-ndk.outputs.ndk-path }}


- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: 'zulu' # See 'Supported distributions' for available options
java-version: '17'

- name: Gradle build
if: ${{ matrix.platform == 'android' }}
run: |
mkdir plugin/libs
cp ../extension/TiltFiveNDK/lib/android/TiltFiveAndroidClient.jar plugin/libs/TiltFiveAndroidClient.jar
chmod +x ./gradlew
./gradlew build
working-directory: ./androidplugin

- name: Upload artifact
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -117,12 +147,19 @@ jobs:
cp source/CONTRIBUTORS.md plugin/addons/tiltfive
cp source/CHANGES.md plugin/addons/tiltfive
mkdir plugin/addons/tiltfive/bin
mkdir plugin/addons/tiltfive/bin/windows
mkdir plugin/addons/tiltfive/bin/windows/x86_64
mkdir plugin/addons/tiltfive/bin/android/
mkdir plugin/addons/tiltfive/bin/android/arm64
# cp gdtiltfive.linux/*.so plugin/addons/tiltfive/bin/
cp gdtiltfive.windows/build/bin/*.dll plugin/addons/tiltfive/bin/
cp gdtiltfive.windows/extension/TiltFiveNDK/lib/win/x86_64/*.dll plugin/addons/tiltfive/bin/
# cp gdtiltfive.android/*.so plugin/addons/tiltfive/bin/
cp gdtiltfive.windows/build/bin/windows/x86_64/*.dll plugin/addons/tiltfive/bin/windows/x86_64/
cp gdtiltfive.windows/extension/TiltFiveNDK/lib/win/x86_64/*.dll plugin/addons/tiltfive/bin/windows/x86_64/
cp gdtiltfive.android/build/bin/android/arm64/*.so plugin/addons/tiltfive/bin/android/arm64/
cp source/extension/TiltFiveNDK/lib/android/arm64-v8a/*.so plugin/addons/tiltfive/bin/android/arm64/
# cp gdtiltfive.ios/*.dylib plugin/addons/tiltfive/bin/
# cp -R gdtiltfive.macos/libgdtiltfive.macos.* plugin/addons/tiltfive/bin/
cp gdtiltfive.android/androidplugin/plugin/build/outputs/aar/gdtiltfive-debug.aar plugin/addons/tiltfive/bin/android
cp gdtiltfive.android/androidplugin/plugin/build/outputs/aar/gdtiltfive-release.aar plugin/addons/tiltfive/bin/android
- name: Copy files to destination for csharp
run: |
mkdir plugin_cs
Expand All @@ -132,12 +169,19 @@ jobs:
cp source/CONTRIBUTORS.md plugin_cs/addons/tiltfive
cp source/CHANGES.md plugin_cs/addons/tiltfive
mkdir plugin_cs/addons/tiltfive/bin
mkdir plugin_cs/addons/tiltfive/bin/windows
mkdir plugin_cs/addons/tiltfive/bin/windows/x86_64
mkdir plugin_cs/addons/tiltfive/bin/android/
mkdir plugin_cs/addons/tiltfive/bin/android/arm64
# cp gdtiltfive.linux/*.so plugin_cs/addons/tiltfive/bin/
cp gdtiltfive.windows/build/bin/*.dll plugin_cs/addons/tiltfive/bin/
cp gdtiltfive.windows/extension/TiltFiveNDK/lib/win/x86_64/*.dll plugin_cs/addons/tiltfive/bin/
# cp gdtiltfive.android/*.so plugin_cs/addons/tiltfive/bin/
cp gdtiltfive.windows/build/bin/windows/x86_64/*.dll plugin_cs/addons/tiltfive/bin/windows/x86_64/
cp gdtiltfive.windows/extension/TiltFiveNDK/lib/win/x86_64/*.dll plugin_cs/addons/tiltfive/bin/windows/x86_64/
cp gdtiltfive.android/build/bin/android/arm64/*.so plugin_cs/addons/tiltfive/bin/android/arm64/
cp source/extension/TiltFiveNDK/lib/android/arm64-v8a/*.so plugin_cs/addons/tiltfive/bin/android/arm64/
# cp gdtiltfive.ios/*.dylib plugin_cs/addons/tiltfive/bin/
# cp -R gdtiltfive.macos/libgdtiltfive.macos.* plugin_cs/addons/tiltfive/bin/
cp gdtiltfive.android/androidplugin/plugin/build/outputs/aar/gdtiltfive-debug.aar plugin_cs/addons/tiltfive/bin/android
cp gdtiltfive.android/androidplugin/plugin/build/outputs/aar/gdtiltfive-release.aar plugin_cs/addons/tiltfive/bin/android
- name: Calculate GIT short ref
run: |
cd source
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ build
*.includes
*.idb
*.exp
*.aar

# project specific ignores
!extension/TiltFiveNDK/lib/*/*/*

example.gd/android/
example.gd/export_presets.cfg
example.csharp/android/
example.csharp/export_presets.cfg
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TiltFiveGodot4 is **GDExtension** for the Godot 4 engine to connect to the [T
| - | - | - | - |
| Windows ||||
| Linux<sup>1</sup> | &checkmark; |&checkmark; | &checkmark; |
| Android | | &#10060; | |
| Android |&#10060;| &#10060; ||

1. [Experimental Version](https://github.com/patrickdown/TiltFiveGodot4/releases/tag/1.1.0-linux-experimental3)

Expand All @@ -26,15 +26,15 @@ Please refer to this [documentation](https://patrickdown.github.io/godot/tilt-fi

### Prerequisites

This extension requires a C++20 capable compiler.
This extension requires a C++20 capable compiler. To build for Android you will need Java 17 and the Android SDK along with the r26c NDK.

Things you will need to know how to do.
* Use [scons](https://scons.org/)
* [Build GDExtensions](https://docs.godotengine.org/en/stable/tutorials/scripting/gdextension/gdextension_cpp_example.html)

### Building the extension

To build the plugin invoke `scons` from the root directory of the project. The build product will in `build\bin`. Invoking `scons example` will build the product and copy the binaries to the `example.gd\addons\tilt-five\bin` and `example.csharp\addons\tilt-five\bin` directories.
To build the plugin invoke `scons` from the root directory of the project. The build product will in `build\bin`. Invoking `scons --assemble` will build the product and copy the binaries to the `example.gd\addons\tilt-five\bin` and `example.csharp\addons\tilt-five\bin` directories.

## Using the build products

Expand Down
101 changes: 78 additions & 23 deletions SConstruct
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
#!/usr/bin/env python
import os
import sys
from pprint import pprint

env = SConscript('godot-cpp/SConstruct')

AddOption(
'--no-gradle',
dest='no_gradle',
action='store_true',
help='no not spawn gradle to build the android plugin')

AddOption(
'--assemble',
dest='assemble_example',
action='store_true',
help='assemble the plugin files into the example project')

AddOption(
'--gd-install-dir',
dest='gd_install_dir',
type='string',
nargs=1,
action='store',
metavar='DIR',
help='godot project directory to copy the gdscript plugin to')

AddOption(
'--cs-install-dir',
dest='cs_install_dir',
type='string',
nargs=1,
action='store',
metavar='DIR',
help='godot project directory to copy the c# plugin to')

gd_install_dir = GetOption('gd_install_dir')
cs_install_dir = GetOption('cs_install_dir')

assemble_example = GetOption('assemble_example') or gd_install_dir or cs_install_dir

build_aar_library = not GetOption('no_gradle') and env['platform'] == 'android'

VariantDir('build/src','extension/src', duplicate=False)
VariantDir('build/T5Integration','extension/T5Integration', duplicate=False)

env = SConscript('godot-cpp/SConstruct')
tilt_five_headers_path = 'extension/TiltFiveNDK/include'
tilt_five_library_path = 'extension/TiltFiveNDK/lib/' + { 'windows' : 'win/x86_64', 'linux' : 'linux/x86_64', 'android' : 'android/arm64-v8a'}[env["platform"]]
tilt_five_library = {'windows' : 'TiltFiveNative.dll.if', 'linux' : 'libTiltFiveNative.so', 'android' : 'libTiltFiveNative.so'}[env["platform"]]
tilt_five_library = {'windows' : 'TiltFiveNative.dll.if', 'linux' : 'libTiltFiveNative.so', 'android' : 'TiltFiveNative'}[env["platform"]]
tilt_five_jar = 'extension/TiltFiveNDK/lib/android/TiltFiveAndroidClient.jar'

bin_path = "{}/{}".format(env['platform'], env['arch'])

# For the reference:
# - CCFLAGS are compilation flags shared between C and C++
Expand All @@ -25,6 +65,7 @@ env.Append(CPPPATH=['extension/src/','extension/T5Integration/',tilt_five_header
sources = Glob('build/src/*.cpp')
sources += Glob('build/T5Integration/*.cpp')


env.Append(LIBPATH=[tilt_five_library_path])
env.Append(LIBS=[tilt_five_library])

Expand All @@ -33,36 +74,50 @@ if env['platform'] == 'windows':
env['CXXFLAGS'].remove('/std:c++17')
env.Append(CXXFLAGS=['/std:c++20'])
env.Append(CXXFLAGS=['/Zc:__cplusplus'])
library = env.SharedLibrary(
'build/bin/libgdtiltfive{}{}'.format(env['suffix'], env['SHLIBSUFFIX']),
source=sources,
)
elif env['platform'] == 'linux':
env['t5_shared_lib'] = 'libTiltFiveNative.so'
env['CXXFLAGS'].remove('-std=c++17')
env.Append(CXXFLAGS=['-std=c++20'])
env.Append(RPATH=env.Literal('\\$$ORIGIN' ))
library = env.SharedLibrary(
'build/bin/libgdtiltfive{}{}'.format(env['suffix'], env['SHLIBSUFFIX']),
source=sources,
)
elif env['platform'] == 'android':
env['t5_shared_lib'] = 'libTiltFiveNative.so'
env['t5_shared_lib'] = 'lib{}.so'.format(tilt_five_library)
env['CXXFLAGS'].remove('-std=c++17')
env.Append(CXXFLAGS=['-std=c++20'])
env.Append(CXXFLAGS=['-stdlib=libc++'])
env.Append(CCFLAGS=['-fPIC'])
#env.Append(CPPDEFINES = ['ANDROID_CPP'])
env.Append(RPATH=env.Literal('\\$$ORIGIN' ))
library = env.SharedLibrary(
'build/bin/libgdtiltfive{}{}'.format(env['suffix'], env['SHLIBSUFFIX']),
source=sources,
)

f1 = env.Command('example.gd/addons/tiltfive/bin/libgdtiltfive{}{}'.format(env['suffix'], env['SHLIBSUFFIX']), library, Copy('$TARGET', '$SOURCE') )
f2 = env.Command('example.gd/addons/tiltfive/bin/{}'.format(env['t5_shared_lib']), tilt_five_library_path + '/{}'.format(env['t5_shared_lib']), Copy('$TARGET', '$SOURCE') )
f3 = env.Command('example.csharp/addons/tiltfive/bin/libgdtiltfive{}{}'.format(env['suffix'], env['SHLIBSUFFIX']), library, Copy('$TARGET', '$SOURCE') )
f4 = env.Command('example.csharp/addons/tiltfive/bin/{}'.format(env['t5_shared_lib']), tilt_five_library_path + '/{}'.format(env['t5_shared_lib']), Copy('$TARGET', '$SOURCE') )
library = env.SharedLibrary(
'build/bin/{}/libgdtiltfive{}{}'.format(bin_path,env['suffix'], env['SHLIBSUFFIX']),
source=sources,
)

things_to_build = [library]

if build_aar_library:
print("Building Android AAR library...")
Execute(Copy('androidplugin/plugin/libs/TiltFiveAndroidClient.jar', tilt_five_jar))
SConscript('androidplugin/SConstruct', exports="env")

if assemble_example:
f1 = env.Command('example.gd/addons/tiltfive/bin/{}/libgdtiltfive{}{}'.format(bin_path,env['suffix'], env['SHLIBSUFFIX']), library, Copy('$TARGET', '$SOURCE') )
f2 = env.Command('example.gd/addons/tiltfive/bin/{}/{}'.format(bin_path,env['t5_shared_lib']), tilt_five_library_path + '/{}'.format(env['t5_shared_lib']), Copy('$TARGET', '$SOURCE') )
f3 = env.Command('example.csharp/addons/tiltfive/bin/{}/libgdtiltfive{}{}'.format(bin_path,env['suffix'], env['SHLIBSUFFIX']), library, Copy('$TARGET', '$SOURCE') )
f4 = env.Command('example.csharp/addons/tiltfive/bin/{}/{}'.format(bin_path,env['t5_shared_lib']), tilt_five_library_path + '/{}'.format(env['t5_shared_lib']), Copy('$TARGET', '$SOURCE') )
things_to_build += [f1, f2, f3, f4]
if build_aar_library:
f5 = env.Command('example.gd/addons/tiltfive/bin/android/gdtiltfive-debug.aar', 'androidplugin/plugin/build/outputs/aar/gdtiltfive-debug.aar', Copy('$TARGET', '$SOURCE') )
f6 = env.Command('example.gd/addons/tiltfive/bin/android/gdtiltfive-release.aar', 'androidplugin/plugin/build/outputs/aar/gdtiltfive-release.aar', Copy('$TARGET', '$SOURCE') )
f7 = env.Command('example.csharp/addons/tiltfive/bin/android/gdtiltfive-debug.aar', 'androidplugin/plugin/build/outputs/aar/gdtiltfive-debug.aar', Copy('$TARGET', '$SOURCE') )
f8 = env.Command('example.csharp/addons/tiltfive/bin/android/gdtiltfive-release.aar', 'androidplugin/plugin/build/outputs/aar/gdtiltfive-release.aar', Copy('$TARGET', '$SOURCE') )
things_to_build += [f5, f6, f7, f8]

if gd_install_dir:
inst1 = env.Install("{}/addons".format(gd_install_dir) , "example.gd/addons/tiltfive")
things_to_build += [inst1]

if cs_install_dir:
inst2 = env.Install("{}/addons".format(cs_install_dir) , "example.csharp/addons/tiltfive")
things_to_build += [inst2]

env.Alias('example', [f1, f2, f3, f4])
Default(things_to_build)

Default(library)
34 changes: 34 additions & 0 deletions androidplugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gradle files
.gradle/
build/
libs/

# Local configuration file (sdk path, etc)
local.properties

# Log/OS Files
*.log

# Android Studio generated files and folders
captures/
.externalNativeBuild/
.cxx/
*.apk
output.json

# IntelliJ
*.iml
.idea/
misc.xml
deploymentTargetDropDown.xml
render.experimental.xml

# Keystore files
*.jks
*.keystore

# Google Services (e.g. APIs or Firebase)
google-services.json

# Android Profiling
*.hprof
5 changes: 5 additions & 0 deletions androidplugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Tilt Five Godot Android ARR

This is a small project that builds the Java parts that are
required by the Tile Five client. It's only callable functions
are used by the c++ parts of the plugin.
13 changes: 13 additions & 0 deletions androidplugin/SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python
import os
import sys

Import("env")

def gradle_build(target, source, env):
os.system('gradlew build --no-daemon')

android_outputs = ['plugin/build/outputs/aar/gdtiltfive-debug.aar', 'plugin/build/outputs/aar/gdtiltfive-release.aar']
android_inputs = ['plugin/libs/TiltFiveAndroidClient.jar', 'plugin/src/main/java/org/tiltfivegodot/plugin/GodotAndroidPlugin.java']
Default(env.Command(android_outputs, android_inputs, gradle_build))

5 changes: 5 additions & 0 deletions androidplugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.library") version "7.4.1" apply false
id("org.jetbrains.kotlin.android") version "1.8.0" apply false
}
23 changes: 23 additions & 0 deletions androidplugin/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
Binary file added androidplugin/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions androidplugin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Fri Aug 25 21:04:51 PDT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 096f83a

Please sign in to comment.