compiling_for_android.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.6+ <https://www.python.org/downloads/>`_.
  20. - `SCons 3.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,
  25. **do not use an Android SDK provided by your distribution's repositories as it will often be outdated**.
  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 `ojdkbuild <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. - Install the necessary SDK components in this folder:
  40. - Accept the SDK component licenses by running the following command
  41. where ``android_sdk_path`` is the path to the Android SDK, then answering all the prompts with ``y``:
  42. ::
  43. cmdline-tools/latest/bin/sdkmanager --sdk_root=<android_sdk_path> --licenses
  44. - Complete setup by running the following command where ``android_sdk_path`` is the path to the Android SDK.
  45. ::
  46. cmdline-tools/latest/bin/sdkmanager --sdk_root=<android_sdk_path> "platform-tools" "build-tools;30.0.3" "platforms;android-29" "cmdline-tools;latest" "cmake;3.10.2.4988404"
  47. .. seealso:: To set the environment variable on Windows, press :kbd:`Windows + R`, type
  48. "control system", then click on **Advanced system settings** in the left
  49. pane, then click on **Environment variables** on the window that appears.
  50. .. seealso:: To set the environment variable on Linux or macOS, use
  51. ``export ANDROID_HOME=/path/to/android-sdk`` where ``/path/to/android-sdk`` points to
  52. the root of the SDK directories.
  53. Building the export templates
  54. -----------------------------
  55. Godot needs two export templates for Android: the optimized "release"
  56. template (``android_release.apk``) and the debug template (``android_debug.apk``).
  57. As Google will require all APKs to include ARMv8 (64-bit) libraries starting
  58. from August 2019, the commands below will build an APK containing both
  59. ARMv7 and ARMv8 libraries.
  60. Compiling the standard export templates is done by calling SCons from the Godot
  61. root directory with the following arguments:
  62. - Release template (used when exporting with **Debugging Enabled** unchecked)
  63. ::
  64. scons platform=android target=template_release arch=arm32
  65. scons platform=android target=template_release arch=arm64
  66. cd platform/android/java
  67. # On Windows
  68. .\gradlew generateGodotTemplates
  69. # On Linux and macOS
  70. ./gradlew generateGodotTemplates
  71. The resulting APK will be located at ``bin/android_release.apk``.
  72. - Debug template (used when exporting with **Debugging Enabled** checked)
  73. ::
  74. scons platform=android target=template_debug arch=arm32
  75. scons platform=android target=template_debug arch=arm64
  76. cd platform/android/java
  77. # On Windows
  78. .\gradlew generateGodotTemplates
  79. # On Linux and macOS
  80. ./gradlew generateGodotTemplates
  81. The resulting APK will be located at ``bin/android_debug.apk``.
  82. .. seealso::
  83. If you want to enable Vulkan validation layers, see
  84. :ref:`Vulkan validation layers on Android <doc_vulkan_validation_layers_android>`.
  85. Adding support for x86 devices
  86. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. If you also want to include support for x86 and x86-64 devices, run the SCons
  88. command a third and fourth time with the ``arch=x86_32``, and
  89. ``arch=x86_64`` arguments before building the APK with Gradle. For
  90. example, for the release template:
  91. ::
  92. scons platform=android target=template_release arch=arm32
  93. scons platform=android target=template_release arch=arm64
  94. scons platform=android target=template_release arch=x86_32
  95. scons platform=android target=template_release arch=x86_64
  96. cd platform/android/java
  97. # On Windows
  98. .\gradlew generateGodotTemplates
  99. # On Linux and macOS
  100. ./gradlew generateGodotTemplates
  101. This will create a fat binary that works on all platforms.
  102. The final APK size of exported projects will depend on the platforms you choose
  103. to support when exporting; in other words, unused platforms will be removed from
  104. the APK.
  105. Cleaning the generated export templates
  106. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  107. You can use the following commands to remove the generated export templates:
  108. ::
  109. cd platform/android/java
  110. # On Windows
  111. .\gradlew clean
  112. # On Linux and macOS
  113. ./gradlew clean
  114. Using the export templates
  115. --------------------------
  116. Godot needs release and debug APKs that were compiled against the same
  117. version/commit as the editor. If you are using official binaries
  118. for the editor, make sure to install the matching export templates,
  119. or build your own from the same version.
  120. When exporting your game, Godot opens the APK, changes a few things inside and
  121. adds your files.
  122. Installing the templates
  123. ~~~~~~~~~~~~~~~~~~~~~~~~
  124. The newly-compiled templates (``android_debug.apk``
  125. and ``android_release.apk``) must be copied to Godot's templates folder
  126. with their respective names. The templates folder can be located in:
  127. - Windows: ``%APPDATA%\Godot\export_templates\<version>\``
  128. - Linux: ``$HOME/.local/share/godot/export_templates/<version>/``
  129. - macOS: ``$HOME/Library/Application Support/Godot/export_templates/<version>/``
  130. ``<version>`` is of the form ``major.minor[.patch].status`` using values from
  131. ``version.py`` in your Godot source repository (e.g. ``3.0.5.stable`` or ``3.1.dev``).
  132. You also need to write this same version string to a ``version.txt`` file located
  133. next to your export templates.
  134. .. TODO: Move these paths to a common reference page
  135. However, if you are writing your custom modules or custom C++ code, you
  136. might instead want to configure your APKs as custom export templates
  137. here:
  138. .. image:: img/andtemplates.png
  139. You don't even need to copy them, you can just reference the resulting
  140. file in the ``bin\`` directory of your Godot source folder, so that the
  141. next time you build you will automatically have the custom templates
  142. referenced.
  143. Building the Godot editor
  144. -------------------------
  145. Compiling the editor is done by calling SCons from the Godot
  146. root directory with the following arguments:
  147. ::
  148. scons platform=android arch=arm32 production=yes target=editor
  149. scons platform=android arch=arm64 production=yes target=editor
  150. scons platform=android arch=x86_32 production=yes target=editor
  151. scons platform=android arch=x86_64 production=yes target=editor
  152. cd platform/android/java
  153. # On Windows
  154. .\gradlew generateGodotEditor
  155. # On Linux and macOS
  156. ./gradlew generateGodotEditor
  157. The resulting APK will be located at ``bin/android_editor_builds/android_editor-release.apk``.
  158. Removing the Editor templates
  159. -----------------------------
  160. You can use the following commands to remove the generated editor templates:
  161. ::
  162. cd platform/android/java
  163. # On Windows
  164. .\gradlew clean
  165. # On Linux and macOS
  166. ./gradlew clean
  167. Installing the Godot editor
  168. ---------------------------
  169. 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.
  170. Open up a Terminal/Command Prompt and run the following commands from the root directory with the following arguments:
  171. ::
  172. adb install ./bin/android_editor_builds/android_editor-release.apk
  173. Troubleshooting
  174. ---------------
  175. Platform doesn't appear in SCons
  176. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  177. Double-check that you've set the ``ANDROID_HOME``
  178. environment variable. This is required for the platform to appear in SCons'
  179. list of detected platforms.
  180. See :ref:`Setting up the buildsystem <doc_android_setting_up_the_buildsystem>`
  181. for more information.
  182. Application not installed
  183. ~~~~~~~~~~~~~~~~~~~~~~~~~
  184. Android might complain the application is not correctly installed.
  185. If so:
  186. - Check that the debug keystore is properly generated.
  187. - Check that the jarsigner executable is from JDK 8.
  188. If it still fails, open a command line and run `logcat <https://developer.android.com/studio/command-line/logcat>`_:
  189. ::
  190. adb logcat
  191. Then check the output while the application is installed;
  192. the error message should be presented there.
  193. Seek assistance if you can't figure it out.
  194. Application exits immediately
  195. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  196. If the application runs but exits immediately, this might be due to
  197. one of the following reasons:
  198. - Make sure to use export templates that match your editor version; if
  199. you use a new Godot version, you *have* to update the templates too.
  200. - ``libgodot_android.so`` is not in ``libs/<arch>/``
  201. where ``<arch>`` is the device's architecture.
  202. - The device's architecture does not match the exported one(s).
  203. Make sure your templates were built for that device's architecture,
  204. and that the export settings included support for that architecture.
  205. In any case, ``adb logcat`` should also show the cause of the error.