Skip to content

Commit

Permalink
first draft of android readme
Browse files Browse the repository at this point in the history
  • Loading branch information
TCROC committed Apr 20, 2024
1 parent eb5c2c3 commit 27c29d3
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/toolchain/export-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!--
~ Copyright (c) godot-rust; Bromeon and contributors.
~ This Source Code Form is subject to the terms of the Mozilla Public
~ License, v. 2.0. If a copy of the MPL was not distributed with this
~ file, You can obtain one at https://mozilla.org/MPL/2.0/.
-->

NOTE: Keep track of where you install the Android Command Line Tools and JDK 17

Exporting with gdext for godot requires some of the same pieces that are required for building godot from source.
Specifically, the Android SDK Command Line Tools and JDK 17 as mentioned in Godot's documentation here:
https://docs.godotengine.org/en/stable/contributing/development/compiling/compiling_for_android.html#requirements

Once you have those installed, you then need to follow godot's instructions for setting up the build system here:
https://docs.godotengine.org/en/stable/contributing/development/compiling/compiling_for_android.html#setting-up-the-buildsystem

As of writing this, I'm not entirely sure if JDK 17 is required in order for Android Command Line Tools to function properly.
I have not tested without it. To be safe, it is included in the documentation for now.

Now on to the fun part of actually compiling gdext for Android! :)

Set the environment variable CLANG_PATH to point to Android's build of clang. Example:

CLANG_PATH is used by bindgen's clang-sys dependency. Documentation here: https://github.com/KyleMayes/clang-sys?tab=readme-ov-file#environment-variables

```bash
#!/bin/bash

export CLANG_PATH = $"[android-cli-directory]/[android-cli-version]/ndk/[ndk-version]/toolchains/llvm/prebuilt/[host-machine-os]/bin/clang"
```

Then set the ``CARGO_TARGET_[target-triple-here]_LINKER`` to point to the android linker for the android triple you are targetting. You need to compile your gdext library for each android triple individually. You can find possible targets by running:

```
rustup target list
```

You can find the linkers in the android cli directory at:

```
$"[android-cli-directory]/[android-cli-version]/ndk/[ndk-version]/toolchains/llvm/prebuilt/[host-machine-os]/bin/[target-triple-here][android-version]"
```

And then look for the android targets. As of writing this, the tested tuples are:

| Tuple | Environment Variable | Godot Arch | Gdextension Config |
| --------------------------- | ----------------------------------------------- | -------------- | ----------------------------- |
| ``aarch64-linux-android`` | ``CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER`` | ``arm64`` | ``android.debug.arm64`` |
| ``x86_64-linux-android`` | ``CARGO_TARGET_X86_64_LINUX_ANDROID_LINKER`` | ``x86_64`` | ``android.debug.x86_64`` |
| ``armv7-linux-androideabi`` | ``CARGO_TARGET_ARMV7_LINUX_ANDROID_LINKER`` | ``arm32`` | ``android.debug.armeabi-v7a`` |
| ``i686-linux-android`` | ``CARGO_TARGET_I686_LINUX_ANDROID_LINKER`` | ``x86_32`` | ``android.debug.x86`` |

Notice how the environment variables are in all caps and the triple's "-" is replaced with "_"

Make sure to add all of the triples you want to support to the ``rustup`` via:

```
rustup target add [android-triple-here]
```

Example:

```
rustup target add aarch64-linux-android
```

Putting it all together, here is an example compiling for ``aarch64-linux-android``. This is also probably the most common Android target as of the writing of this.

Assuming 2 things:

1. android cli is installed in my HOME folder
2. Godot is still relying on android ndk version: 23.2.8568313. Check here: https://github.com/godotengine/godot/blob/4a0160241fd0c1e874e297f6b08676cf0761e5e8/platform/android/java/app/config.gradle#L14
3. The downloaded android cli version is: 11076708_latest (update this to be the version you downloaded)
4. This is being ran on linux. Change the ``linux-x86_64`` folder in ``CLANG_PATH`` and ``CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER`` to be your host machine's operating system.
5. You are targetting android version 34

```bash
#!/bin/bash

rustup target add aarch64-linux-android

export CLANG_PATH="$HOME/android-cli/11076708_latest/ndk/23.2.8568313/toolchains/llvm/prebuilt/linux-x86_64/bin/clang"
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="$HOME/android-cli/11076708_latest/ndk/23.2.8568313//toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android34-clang"

cargo build --target=aarch64-linux-android
```

And then you should find a built version of your gdextension lib in:

```
target/aarch64-linux-android/debug/[lib-name-here].so
```

Make sure to update your ``.gdextension`` file to point to the compiled lib. Example:

```
android.debug.arm64="res://path/to/rust/lib/target/aarch64-linux-android/debug/[lib-name-here].so
```

TODO:

1. Create an example project with an in depth tutorial
2. Maybe create a toolset that automates a lot of these steps
3. Building on 2, I've actually made a lot of progress with my ``godot-src`` tool over here: https://github.com/Lange-Studios/godot-src.git. I don't have an android example yet, but I do plan on adding one at somepoint.
4. Remove these TODO's. I just put them here for the draft PR :)

0 comments on commit 27c29d3

Please sign in to comment.