compiling_for_android.rst 8.0 KB

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