compiling_for_android.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. .. _doc_compiling_for_android:
  2. Compiling for Android
  3. =====================
  4. .. highlight:: shell
  5. .. seealso::
  6. This page describes how to compile Android export template binaries from source.
  7. If you're looking to export your project to Android instead, read :ref:`doc_exporting_for_android`.
  8. Note
  9. ----
  10. In most cases, using the built-in deployer and export templates is good
  11. enough. Compiling the Android APK manually is mostly useful for custom
  12. builds or custom packages for the deployer.
  13. Also, you still need to follow the steps mentioned in the
  14. :ref:`doc_exporting_for_android` tutorial before attempting to build
  15. a custom export template.
  16. Requirements
  17. ------------
  18. For compiling under Windows, Linux or macOS, the following is required:
  19. - `Python 3.8+ <https://www.python.org/downloads/>`_.
  20. - `SCons 4.0+ <https://scons.org/pages/download.html>`_ build system.
  21. - `Android SDK <https://developer.android.com/studio/#command-tools>`_
  22. (command-line tools are sufficient).
  23. - Required SDK components will be automatically installed.
  24. - On Linux, **do not use an Android SDK provided by your distribution's repositories** as it will often be outdated.
  25. - On macOS, **do not use an Android SDK provided by Homebrew** as it will not be installed in a unified location.
  26. - Gradle (will be downloaded and installed automatically if missing).
  27. - JDK 17 (either OpenJDK or Oracle JDK).
  28. - You can download a build from `Adoptium <https://adoptium.net/temurin/releases/?variant=openjdk17>`_.
  29. .. seealso:: To get the Godot source code for compiling, see
  30. :ref:`doc_getting_source`.
  31. For a general overview of SCons usage for Godot, see
  32. :ref:`doc_introduction_to_the_buildsystem`.
  33. .. _doc_android_setting_up_the_buildsystem:
  34. Setting up the buildsystem
  35. --------------------------
  36. - Set the environment variable ``ANDROID_HOME`` to point to the Android
  37. SDK. If you downloaded the Android command-line tools, this would be
  38. the folder where you extracted the contents of the ZIP archive.
  39. - Windows: Press :kbd:`Windows + R`, type "control system",
  40. then click on **Advanced system settings** in the left pane,
  41. then click on **Environment variables** on the window that appears.
  42. - Linux or macOS: Add the text ``export ANDROID_HOME="/path/to/android-sdk"``
  43. to your ``.bashrc`` or ``.zshrc`` where ``/path/to/android-sdk`` points to
  44. the root of the SDK directories.
  45. - Install the necessary SDK components in this folder:
  46. - Accept the SDK component licenses by running the following command
  47. where ``android_sdk_path`` is the path to the Android SDK, then answering all the prompts with ``y``:
  48. ::
  49. cmdline-tools/latest/bin/sdkmanager --sdk_root=<android_sdk_path> --licenses
  50. - Complete setup by running the following command where ``android_sdk_path`` is the path to the Android SDK.
  51. ::
  52. cmdline-tools/latest/bin/sdkmanager --sdk_root=<android_sdk_path> "platform-tools" "build-tools;35.0.0" "platforms;android-35" "cmdline-tools;latest" "cmake;3.10.2.4988404" "ndk;28.1.13356709"
  53. - After setting up the SDK and environment variables, be sure to
  54. **restart your terminal** to apply the changes. If you are using
  55. an IDE with an integrated terminal, you need to restart the IDE.
  56. - Run ``scons platform=android``. If this fails, go back and check the steps.
  57. If you completed the setup correctly, the NDK will begin downloading.
  58. If you are trying to compile GDExtension, you need to first compile
  59. the engine to download the NDK, then you can compile GDExtension.
  60. Building the export templates
  61. -----------------------------
  62. Godot needs three export templates for Android: the optimized "release"
  63. template (``android_release.apk``), the debug template (``android_debug.apk``),
  64. and the Gradle build template (``android_source.zip``).
  65. As Google requires all APKs to include ARMv8 (64-bit) libraries since August 2019,
  66. the commands below build templates containing both ARMv7 and ARMv8 libraries.
  67. Compiling the standard export templates is done by calling SCons from the Godot
  68. root directory with the following arguments:
  69. - Release template (used when exporting with **Debugging Enabled** unchecked)
  70. ::
  71. scons platform=android target=template_release arch=arm32
  72. scons platform=android target=template_release arch=arm64 generate_android_binaries=yes
  73. - Debug template (used when exporting with **Debugging Enabled** checked)
  74. ::
  75. scons platform=android target=template_debug arch=arm32
  76. scons platform=android target=template_debug arch=arm64 generate_android_binaries=yes
  77. - (**Optional**) Dev template (used when troubleshooting)
  78. ::
  79. scons platform=android target=template_debug arch=arm32 dev_build=yes
  80. scons platform=android target=template_debug arch=arm64 dev_build=yes generate_android_binaries=yes
  81. The resulting templates will be located under the ``bin`` directory:
  82. - ``bin/android_release.apk`` for the release template
  83. - ``bin/android_debug.apk`` for the debug template
  84. - ``bin/android_dev.apk`` for the dev template
  85. - ``bin/android_source.zip`` for the Gradle build template
  86. .. note::
  87. - If you are changing the list of architectures you're building, remember to add ``generate_android_binaries=yes`` to the *last* architecture you're building, so that the template files are generated after the build.
  88. - To include debug symbols in the generated templates, add the ``debug_symbols=yes`` parameters to the SCons command.
  89. - Note that you can include ``separate_debug_symbols=yes`` to generate the debug symbols in a separate ``*-native-debug-symbols.zip`` file.
  90. .. seealso::
  91. If you want to enable Vulkan validation layers, see
  92. :ref:`Vulkan validation layers on Android <doc_vulkan_validation_layers_android>`.
  93. Adding support for x86 devices
  94. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. If you also want to include support for x86 and x86_64 devices, run the SCons
  96. command a third and fourth time with the ``arch=x86_32``, and
  97. ``arch=x86_64`` arguments before building the APK with Gradle. For
  98. example, for the release template:
  99. ::
  100. scons platform=android target=template_release arch=arm32
  101. scons platform=android target=template_release arch=arm64
  102. scons platform=android target=template_release arch=x86_32
  103. scons platform=android target=template_release arch=x86_64 generate_android_binaries=yes
  104. This will create template binaries that works on all platforms.
  105. The final binary size of exported projects will depend on the platforms you choose
  106. to support when exporting; in other words, unused platforms will be removed from
  107. the binary.
  108. Cleaning the generated export templates
  109. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  110. You can use the following commands to remove the generated export templates:
  111. ::
  112. cd platform/android/java
  113. # On Windows
  114. .\gradlew clean
  115. # On Linux and macOS
  116. ./gradlew clean
  117. Using the export templates
  118. --------------------------
  119. Godot needs release and debug binaries that were compiled against the same
  120. version/commit as the editor. If you are using official binaries
  121. for the editor, make sure to install the matching export templates,
  122. or build your own from the same version.
  123. When exporting your game, Godot uses the templates as a base, and updates their content as needed.
  124. Installing the templates
  125. ~~~~~~~~~~~~~~~~~~~~~~~~
  126. The newly-compiled templates (``android_debug.apk``
  127. , ``android_release.apk``, and ``android_source.zip``) must be copied to Godot's templates folder
  128. with their respective names. The templates folder can be located in:
  129. - Windows: ``%APPDATA%\Godot\export_templates\<version>\``
  130. - Linux: ``$HOME/.local/share/godot/export_templates/<version>/``
  131. - macOS: ``$HOME/Library/Application Support/Godot/export_templates/<version>/``
  132. ``<version>`` is of the form ``major.minor[.patch].status`` using values from
  133. ``version.py`` in your Godot source repository (e.g. ``4.1.3.stable`` or ``4.2.dev``).
  134. You also need to write this same version string to a ``version.txt`` file located
  135. next to your export templates.
  136. .. TODO: Move these paths to a common reference page
  137. However, if you are writing your custom modules or custom C++ code, you
  138. might instead want to configure your template binaries as custom export templates
  139. in the project export menu. You must have **Advanced Options** enabled to set this.
  140. .. image:: img/andtemplates.webp
  141. You don't even need to copy them, you can just reference the resulting
  142. file in the ``bin\`` directory of your Godot source folder, so that the
  143. next time you build you will automatically have the custom templates
  144. referenced.
  145. Building the Godot editor
  146. -------------------------
  147. Compiling the editor is done by calling SCons from the Godot
  148. root directory with the following arguments:
  149. ::
  150. scons platform=android arch=arm32 production=yes target=editor
  151. scons platform=android arch=arm64 production=yes target=editor
  152. scons platform=android arch=x86_32 production=yes target=editor
  153. scons platform=android arch=x86_64 production=yes target=editor generate_android_binaries=yes
  154. - You can add the ``dev_build=yes`` parameter to generate a dev build of the Godot editor.
  155. - You can add the ``debug_symbols=yes`` parameters to include the debug symbols in the generated build.
  156. - Note that you can include ``separate_debug_symbols=yes`` to generate the debug symbols in a separate ``*-native-debug-symbols.zip`` file.
  157. - You can skip certain architectures depending on your target device to speed up compilation.
  158. Remember to add ``generate_android_binaries=yes`` to the *last* architecture you're building, so that binaries are generated after the build.
  159. The resulting binaries will be located under ``bin/android_editor_builds/``.
  160. Removing the Editor binaries
  161. ----------------------------
  162. You can use the following commands to remove the generated editor binaries:
  163. ::
  164. cd platform/android/java
  165. # On Windows
  166. .\gradlew clean
  167. # On Linux and macOS
  168. ./gradlew clean
  169. Installing the Godot editor APK
  170. -------------------------------
  171. 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.
  172. Open up a Terminal/Command Prompt and run the following commands from the root directory with the following arguments:
  173. ::
  174. adb install ./bin/android_editor_builds/android_editor-release.apk
  175. Troubleshooting
  176. ---------------
  177. Platform doesn't appear in SCons
  178. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  179. Double-check that you've set the ``ANDROID_HOME``
  180. environment variable. This is required for the platform to appear in SCons'
  181. list of detected platforms.
  182. See :ref:`Setting up the buildsystem <doc_android_setting_up_the_buildsystem>`
  183. for more information.
  184. Application not installed
  185. ~~~~~~~~~~~~~~~~~~~~~~~~~
  186. Android might complain the application is not correctly installed.
  187. If so:
  188. - Check that the debug keystore is properly generated.
  189. - Check that the jarsigner executable is from JDK 8.
  190. If it still fails, open a command line and run `logcat <https://developer.android.com/studio/command-line/logcat>`_:
  191. ::
  192. adb logcat
  193. Then check the output while the application is installed;
  194. the error message should be presented there.
  195. Seek assistance if you can't figure it out.
  196. Application exits immediately
  197. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  198. If the application runs but exits immediately, this might be due to
  199. one of the following reasons:
  200. - Make sure to use export templates that match your editor version; if
  201. you use a new Godot version, you *have* to update the templates too.
  202. - ``libgodot_android.so`` is not in ``libs/<arch>/``
  203. where ``<arch>`` is the device's architecture.
  204. - The device's architecture does not match the exported one(s).
  205. Make sure your templates were built for that device's architecture,
  206. and that the export settings included support for that architecture.
  207. In any case, ``adb logcat`` should also show the cause of the error.