compiling_for_android.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. .. _doc_compiling_for_android:
  2. Compiling for Android
  3. =====================
  4. .. highlight:: shell
  5. Note
  6. ----
  7. In most cases, using the built-in deployer and export templates is good
  8. enough. Compiling the Android APK manually is mostly useful for custom
  9. builds or custom packages for the deployer.
  10. Also, you still need to follow the steps mentioned in the
  11. :ref:`doc_exporting_for_android` tutorial before attempting to build
  12. a custom export template.
  13. Requirements
  14. ------------
  15. For compiling under Windows, Linux or macOS, the following is required:
  16. - `Python 3.5+ <https://www.python.org/downloads/>`_.
  17. - `SCons 3.0+ <https://scons.org/pages/download.html>`_ build system.
  18. - `Android SDK <https://developer.android.com/studio/#command-tools>`_
  19. (command-line tools are sufficient).
  20. - Required SDK components will be automatically installed.
  21. - On Linux,
  22. **do not use an Android SDK provided by your distribution's repositories as it will often be outdated**.
  23. - Gradle (will be downloaded and installed automatically if missing).
  24. - JDK 11 (either OpenJDK or Oracle JDK).
  25. - You can download a build from `ojdkbuild <https://github.com/ojdkbuild/ojdkbuild>`_.
  26. .. seealso:: For a general overview of SCons usage for Godot, see
  27. :ref:`doc_introduction_to_the_buildsystem`.
  28. .. _doc_android_setting_up_the_buildsystem:
  29. Setting up the buildsystem
  30. --------------------------
  31. - Set the environment variable ``ANDROID_SDK_ROOT`` to point to the Android
  32. SDK. If you downloaded the Android command-line tools, this would be
  33. the folder where you extracted the contents of the ZIP archive.
  34. - Install the necessary SDK components in this folder:
  35. - Accept the SDK component licenses by running the following command
  36. where ``android_sdk_path`` is the path to the Android SDK, then answering all the prompts with ``y``:
  37. ::
  38. tools/bin/sdkmanager --sdk_root=<android_sdk_path> --licenses
  39. - Complete setup by running the following command where ``android_sdk_path`` is the path to the Android SDK.
  40. ::
  41. tools/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"
  42. .. seealso:: To set the environment variable on Windows, press :kbd:`Windows + R`, type
  43. "control system", then click on **Advanced system settings** in the left
  44. pane, then click on **Environment variables** on the window that appears.
  45. .. seealso:: To set the environment variable on Linux or macOS, use
  46. ``export ANDROID_SDK_ROOT=/path/to/android-sdk`` where ``/path/to/android-sdk`` points to
  47. the root of the SDK directories.
  48. Building the export templates
  49. -----------------------------
  50. Godot needs two export templates for Android: the optimized "release"
  51. template (``android_release.apk``) and the debug template (``android_debug.apk``).
  52. As Google will require all APKs to include ARMv8 (64-bit) libraries starting
  53. from August 2019, the commands below will build an APK containing both
  54. ARMv7 and ARMv8 libraries.
  55. Compiling the standard export templates is done by calling SCons from the Godot
  56. root directory with the following arguments:
  57. - Release template (used when exporting with **Debugging Enabled** unchecked)
  58. ::
  59. scons platform=android target=release android_arch=armv7
  60. scons platform=android target=release android_arch=arm64v8
  61. cd platform/android/java
  62. # On Windows
  63. .\gradlew generateGodotTemplates
  64. # On Linux and macOS
  65. ./gradlew generateGodotTemplates
  66. The resulting APK will be located at ``bin/android_release.apk``.
  67. - Debug template (used when exporting with **Debugging Enabled** checked)
  68. ::
  69. scons platform=android target=release_debug android_arch=armv7
  70. scons platform=android target=release_debug android_arch=arm64v8
  71. cd platform/android/java
  72. # On Windows
  73. .\gradlew generateGodotTemplates
  74. # On Linux and macOS
  75. ./gradlew generateGodotTemplates
  76. The resulting APK will be located at ``bin/android_debug.apk``.
  77. .. seealso::
  78. If you want to enable Vulkan validation layers, see
  79. :ref:`Vulkan validation layers on Android<doc_vulkan_validation_layers-android>`.
  80. Adding support for x86 devices
  81. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. If you also want to include support for x86 and x86-64 devices, run the SCons
  83. command a third and fourth time with the ``android_arch=x86``, and
  84. ``android_arch=x86_64`` arguments before building the APK with Gradle. For
  85. example, for the release template:
  86. ::
  87. scons platform=android target=release android_arch=armv7
  88. scons platform=android target=release android_arch=arm64v8
  89. scons platform=android target=release android_arch=x86
  90. scons platform=android target=release android_arch=x86_64
  91. cd platform/android/java
  92. # On Windows
  93. .\gradlew generateGodotTemplates
  94. # On Linux and macOS
  95. ./gradlew generateGodotTemplates
  96. This will create a fat binary that works on all platforms.
  97. The final APK size of exported projects will depend on the platforms you choose
  98. to support when exporting; in other words, unused platforms will be removed from
  99. the APK.
  100. Cleaning the generated export templates
  101. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  102. You can use the following commands to remove the generated export templates:
  103. ::
  104. cd platform/android/java
  105. # On Windows
  106. .\gradlew cleanGodotTemplates
  107. # On Linux and macOS
  108. ./gradlew cleanGodotTemplates
  109. Using the export templates
  110. --------------------------
  111. Godot needs release and debug APKs that were compiled against the same
  112. version/commit as the editor. If you are using official binaries
  113. for the editor, make sure to install the matching export templates,
  114. or build your own from the same version.
  115. When exporting your game, Godot opens the APK, changes a few things inside and
  116. adds your files.
  117. Installing the templates
  118. ~~~~~~~~~~~~~~~~~~~~~~~~
  119. The newly-compiled templates (``android_debug.apk``
  120. and ``android_release.apk``) must be copied to Godot's templates folder
  121. with their respective names. The templates folder can be located in:
  122. - Windows: ``%APPDATA%\Godot\templates\<version>\``
  123. - Linux: ``$HOME/.local/share/godot/templates/<version>/``
  124. - macOS: ``$HOME/Library/Application Support/Godot/templates/<version>/``
  125. ``<version>`` is of the form ``major.minor[.patch].status`` using values from
  126. ``version.py`` in your Godot source repository (e.g. ``3.0.5.stable`` or ``3.1.dev``).
  127. You also need to write this same version string to a ``version.txt`` file located
  128. next to your export templates.
  129. .. TODO: Move these paths to a common reference page
  130. However, if you are writing your custom modules or custom C++ code, you
  131. might instead want to configure your APKs as custom export templates
  132. here:
  133. .. image:: img/andtemplates.png
  134. You don't even need to copy them, you can just reference the resulting
  135. file in the ``bin\`` directory of your Godot source folder, so that the
  136. next time you build you will automatically have the custom templates
  137. referenced.
  138. Troubleshooting
  139. ---------------
  140. Platform doesn't appear in SCons
  141. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  142. Double-check that you've set the ``ANDROID_SDK_ROOT``
  143. environment variable. This is required for the platform to appear in SCons'
  144. list of detected platforms.
  145. See :ref:`Setting up the buildsystem <doc_android_setting_up_the_buildsystem>`
  146. for more information.
  147. Application not installed
  148. ~~~~~~~~~~~~~~~~~~~~~~~~~
  149. Android might complain the application is not correctly installed.
  150. If so:
  151. - Check that the debug keystore is properly generated.
  152. - Check that the jarsigner executable is from JDK 8.
  153. If it still fails, open a command line and run `logcat <https://developer.android.com/studio/command-line/logcat>`_:
  154. ::
  155. adb logcat
  156. Then check the output while the application is installed;
  157. the error message should be presented there.
  158. Seek assistance if you can't figure it out.
  159. Application exits immediately
  160. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  161. If the application runs but exits immediately, this might be due to
  162. one of the following reasons:
  163. - Make sure to use export templates that match your editor version; if
  164. you use a new Godot version, you *have* to update the templates too.
  165. - ``libgodot_android.so`` is not in ``libs/<android_arch>/``
  166. where ``<android_arch>`` is the device's architecture.
  167. - The device's architecture does not match the exported one(s).
  168. Make sure your templates were built for that device's architecture,
  169. and that the export settings included support for that architecture.
  170. In any case, ``adb logcat`` should also show the cause of the error.