compiling_for_android.rst 8.5 KB

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