Building Android Templates For your Godot C# (Mono) Project on Linux

Hi so this tutorial is going to be very precise, no shortcuts and it expects that you know how to use linux terminal and build things from command. When you’re building, you might see warnings which can be ignored, sometimes you may see an error and if rerunning the scons build doesn’t fix that error that means you either did something wrong or there is an issue that needs to be solved and it’s best to report it on the godot github issue tracker.

Requirements

  • Terminal (because you’ll be doing all this from terminal)
  • Python 2.7 or 3 (not sure if it matters, as long as scons runs)
  • Scons 3.0
  • Git
  • Android Studio (For installing SDK AND NDK-Bundle AND cmake)
  • Mono (I’m using 6.0.0 stable – editor & templates must be built from the same mono version and exporting must use the same mono version) [install specific version of mono]
  • Java (OpenJDK 1.8) [Scroll down to ‘Installing Specific Versions of OpenJDK‘ and follow those steps]
  • Distro specific one liners [to save you some time this will trying to install every build requirement such scons, libx11-develop, etc..]

Make sure you setup all these requirements first or you’ll end up being told what tools you’re missing when trying to build.

Setup Android Studio (SDK & NDK)

Now a lot of you probably already used Android Studio before so you may already have this setup. For those that do not have it setup, open AS for the first time and you’ll be greeted with a Welcome to Android Studio Screen.

Now at the bottom you should see Configure. Select that and click on SDK Manager. If this is your first time it should ask you to install the android sdk to the provided location. You’ll see that location at the top once complete.

Next, make sure you check CMake and NDK near the bottom. [note: do not select ndk (side by side because the one near the bottom w/ a version number will put ndk inside your sdk folder wherever it is located which is what we want and side by side will not]

Once finished remember you can always find the sdk location from the SDK Manager page.

Clone necessary repos

This step, we’re going to need 3 specific repos. Godot Master Source, Neikeq’s Build Tools, and Mono Project Master Source. For me, I have a directory called Git under ~/Development/Git where I clone these repos to. So go ahead and clone each repo:

  • clone script source: git@github.com:godotengine/godot-mono-builds.git
  • clone mono project source: git@github.com:mono/mono.git
  • clone godot source: git@github.com:godotengine/godot.git

Patch Mono

Before we proceed, we need to apply a patch to the mono source code. So first go download the diff file from the godot repository. Click on the raw button and save the diff file to the folder above your mono source project.

Now we need to apply patch by opening terminal into the mono directory then running the following command: patch -p1 < ../fix-mono-android-tkill.diff

Setup Exports

You can set these up permanently in ~/.profile_bash, but we’ll be setting up our exports temporarily.

  • export ANDROID_SDK_ROOT=$HOME/Android/Sdk
  • export ANDROID_NDK_ROOT=$ANDROID_SDK_ROOT/ndk-bundle
  • export MONO_SOURCE_ROOT=$HOME/Development/Git/mono
  • export MONO_SOURCE=$MONO_SOURCE_ROOT
  • export ANDROID_HOME=$ANDROID_SDK_ROOT
  • export MONO_VERSION=6.0.0.319

These exports are necessary for building and running the build script in different areas, such as the final process for building export templates.

Just verify your paths are correct and mono version and you should be good to go for the rest of this guide.

Build Godot Editor & Mono Glue

After you’ve cloned the godot editor, lets build the editor without the glue.

scons p=x11 tools=yes module_mono_enabled=yes mono_glue=no

(note: append -jX where X is the total threads to run on, I usually go -j4)

Then, we need to generate the glue.

bin/godot.x11.tools.64.mono --generate-mono-glue modules/mono/glue

Next go to the bin folder and move everything into a subfolder called glue, nothing special about this, but it makes it easier to see the difference in binaries when we build the editor w/ glue.

Now build the editor w/ glue

scons p=x11 target=release_debug tools=yes module_mono_enabled=yes

Copy all the contents of the editor inside the bin folder excluding the glue folder we created so we don’t lose our newly build editor.

Build Mono Against Android

This part is a bit simple as long as you have all your exports setup. The first step I’d take is to create a directory somewhere called Targets. I personally created this folder inside my Android folder parallel to my Sdk directory.

Travel to the directory in terminal where we cloned the godot-mono-builds repo.

(Just a note: if you’ve gotten this far, it should be noted that the terminal we set our exports in should be the only terminal we use to do everything in else we’d have to set exports again or something might not work – so any time I write, ‘travel to’, that just means cd to that directory)

If you go to the repo and check the readme, it’ll have a way to see all available commands. For what we want to do we will only be targeting one device which is armv7 (armeabi-v7a).

When rebuilding you want to run the clean command, but since this is our first build we’re only going to configure then make.

We’re going to need to set the –target which is armeabi-v7a. The –install-dir which for us is $HOME/Android/Targets. For other targets that require higher api versions you would want to set the –android-api-version which is 18 by default. arm64-v8a requires 21. So remember that when targeting all.

Here are the commands in order, configured

  • ./build_mono_android.py configure --target=armeabi-v7a --install-dir=$HOME/Android/Targets
  • ./build_mono_android.py make --target=armeabi-v7a --install-dir=$HOME/Android/Targets

Once complete, we can build the apk templates to use for our project.

Build Android APK Templates

Note when building these templates, there will be two apk’s generated, but only one is required to keep which is the release you built against (debug/release) – you will need both release and debug builds when setting up your project for exporting.

So first we’re going to build the debug template and then the release template. Building both is almost as similar to building export template for any other platform except we target android and we need to set a few parameters. One of those parameters is mono_prefix to tell scons which version of mono to build godot against. Which is the one we target we created in the previous step. Go to your targets folder and find that path then generate a release and debug command that looks something like below:

Release
scons platform=android target=release android_arch=armv7 module_mono_enabled=yes mono_prefix=/home/lamonte/Android/Targets/android-armeabi-v7a-release ndk_platform=android-18

Debug
scons platform=android target=release_debug android_arch=armv7 module_mono_enabled=yes mono_prefix=/home/lamonte/Android/Targets/android-armeabi-v7a-release ndk_platform=android-18

Now just like building the editor, you can append -jX to the end to have it build in parallel on different threads.

So before we start with the debug command, head to our godot repo like when we built godot then run the command.

Once godot is finished building head to the following subfolder: platform/android/java

Next we need to let gradle build our apk’s by running: ./gradlew build

Lastly

Travel to the build folder (platform/android/java/build) then outputs and apks. Then grab the apk for the release build you created. Repeat for the other build and then you should have an apk for debug & release that you can use for exporting your mono created games.