Compiling for Android (original) (raw)

See also

This page describes how to compile Android export template binaries from source. If you're looking to export your project to Android instead, read Exporting for Android.

Note

In most cases, using the built-in deployer and export templates is good enough. Compiling the Android APK manually is mostly useful for custom builds or custom packages for the deployer.

Also, you still need to follow the steps mentioned in theExporting for Android tutorial before attempting to build a custom export template.

Requirements

For compiling under Windows, Linux or macOS, the following is required:

Setting up the buildsystem

Building the export templates

Godot needs three export templates for Android: the optimized "release" template (android_release.apk), the debug template (android_debug.apk), and the Gradle build template (android_source.zip). As Google requires all APKs to include ARMv8 (64-bit) libraries since August 2019, the commands below build templates containing both ARMv7 and ARMv8 libraries.

Compiling the standard export templates is done by calling SCons from the Godot root directory with the following arguments:

scons platform=android target=template_release arch=arm32 scons platform=android target=template_release arch=arm64 generate_android_binaries=yes

scons platform=android target=template_debug arch=arm32 scons platform=android target=template_debug arch=arm64 generate_android_binaries=yes

scons platform=android target=template_debug arch=arm32 dev_build=yes scons platform=android target=template_debug arch=arm64 dev_build=yes generate_android_binaries=yes

The resulting templates will be located under the bin directory:

Note

Adding support for x86 devices

If you also want to include support for x86 and x86_64 devices, run the SCons command a third and fourth time with the arch=x86_32, andarch=x86_64 arguments before building the APK with Gradle. For example, for the release template:

scons platform=android target=template_release arch=arm32 scons platform=android target=template_release arch=arm64 scons platform=android target=template_release arch=x86_32 scons platform=android target=template_release arch=x86_64 generate_android_binaries=yes

This will create template binaries that works on all platforms. The final binary size of exported projects will depend on the platforms you choose to support when exporting; in other words, unused platforms will be removed from the binary.

Cleaning the generated export templates

You can use the following commands to remove the generated export templates:

cd platform/android/java

On Windows

.\gradlew clean

On Linux and macOS

./gradlew clean

Using the export templates

Godot needs release and debug binaries that were compiled against the same version/commit as the editor. If you are using official binaries for the editor, make sure to install the matching export templates, or build your own from the same version.

When exporting your game, Godot uses the templates as a base, and updates their content as needed.

Installing the templates

The newly-compiled templates (android_debug.apk, android_release.apk, and android_source.zip) must be copied to Godot's templates folder with their respective names. The templates folder can be located in:

<version> is of the form major.minor[.patch].status using values fromversion.py in your Godot source repository (e.g. 4.1.3.stable or 4.2.dev). You also need to write this same version string to a version.txt file located next to your export templates.

However, if you are writing your custom modules or custom C++ code, you might instead want to configure your template binaries as custom export templates here:

../../../_images/andtemplates.png

You don't even need to copy them, you can just reference the resulting file in the bin\ directory of your Godot source folder, so that the next time you build you will automatically have the custom templates referenced.

Building the Godot editor

Compiling the editor is done by calling SCons from the Godot root directory with the following arguments:

scons platform=android arch=arm32 production=yes target=editor scons platform=android arch=arm64 production=yes target=editor scons platform=android arch=x86_32 production=yes target=editor scons platform=android arch=x86_64 production=yes target=editor generate_android_binaries=yes

Remember to add generate_android_binaries=yes to the last architecture you're building, so that binaries are generated after the build.

The resulting binaries will be located under bin/android_editor_builds/.

Removing the Editor binaries

You can use the following commands to remove the generated editor binaries:

cd platform/android/java

On Windows

.\gradlew clean

On Linux and macOS

./gradlew clean

Installing the Godot editor APK

With an Android device with Developer Options enabled, connect the Android device to your computer via its charging cable to a USB/USB-C port. Open up a Terminal/Command Prompt and run the following commands from the root directory with the following arguments:

adb install ./bin/android_editor_builds/android_editor-release.apk

Troubleshooting

Platform doesn't appear in SCons

Double-check that you've set the ANDROID_HOMEenvironment variable. This is required for the platform to appear in SCons' list of detected platforms. See Setting up the buildsystemfor more information.

Application not installed

Android might complain the application is not correctly installed. If so:

If it still fails, open a command line and run logcat:

Then check the output while the application is installed; the error message should be presented there. Seek assistance if you can't figure it out.

Application exits immediately

If the application runs but exits immediately, this might be due to one of the following reasons:

In any case, adb logcat should also show the cause of the error.