compiling_for_android.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.1" "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. Adding support for x86 devices
  79. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80. If you also want to include support for x86 and x86-64 devices, run the SCons
  81. command a third and fourth time with the ``android_arch=x86``, and
  82. ``android_arch=x86_64`` arguments before building the APK with Gradle. For
  83. example, for the release template:
  84. ::
  85. scons platform=android target=release android_arch=armv7
  86. scons platform=android target=release android_arch=arm64v8
  87. scons platform=android target=release android_arch=x86
  88. scons platform=android target=release android_arch=x86_64
  89. cd platform/android/java
  90. # On Windows
  91. .\gradlew generateGodotTemplates
  92. # On Linux and macOS
  93. ./gradlew generateGodotTemplates
  94. This will create a fat binary that works on all platforms.
  95. The final APK size of exported projects will depend on the platforms you choose
  96. to support when exporting; in other words, unused platforms will be removed from
  97. the APK.
  98. Cleaning the generated export templates
  99. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. You can use the following commands to remove the generated export templates:
  101. ::
  102. cd platform/android/java
  103. # On Windows
  104. .\gradlew cleanGodotTemplates
  105. # On Linux and macOS
  106. ./gradlew cleanGodotTemplates
  107. Using the export templates
  108. --------------------------
  109. Godot needs release and debug APKs that were compiled against the same
  110. version/commit as the editor. If you are using official binaries
  111. for the editor, make sure to install the matching export templates,
  112. or build your own from the same version.
  113. When exporting your game, Godot opens the APK, changes a few things inside and
  114. adds your files.
  115. Installing the templates
  116. ~~~~~~~~~~~~~~~~~~~~~~~~
  117. The newly-compiled templates (``android_debug.apk``
  118. and ``android_release.apk``) must be copied to Godot's templates folder
  119. with their respective names. The templates folder can be located in:
  120. - Windows: ``%APPDATA%\Godot\templates\<version>\``
  121. - Linux: ``$HOME/.local/share/godot/templates/<version>/``
  122. - macOS: ``$HOME/Library/Application Support/Godot/templates/<version>/``
  123. ``<version>`` is of the form ``major.minor[.patch].status`` using values from
  124. ``version.py`` in your Godot source repository (e.g. ``3.0.5.stable`` or ``3.1.dev``).
  125. You also need to write this same version string to a ``version.txt`` file located
  126. next to your export templates.
  127. .. TODO: Move these paths to a common reference page
  128. However, if you are writing your custom modules or custom C++ code, you
  129. might instead want to configure your APKs as custom export templates
  130. here:
  131. .. image:: img/andtemplates.png
  132. You don't even need to copy them, you can just reference the resulting
  133. file in the ``bin\`` directory of your Godot source folder, so that the
  134. next time you build you will automatically have the custom templates
  135. referenced.
  136. Troubleshooting
  137. ---------------
  138. Platform doesn't appear in SCons
  139. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  140. Double-check that you've set the ``ANDROID_SDK_ROOT``
  141. environment variable. This is required for the platform to appear in SCons'
  142. list of detected platforms.
  143. See :ref:`Setting up the buildsystem <doc_android_setting_up_the_buildsystem>`
  144. for more information.
  145. Application not installed
  146. ~~~~~~~~~~~~~~~~~~~~~~~~~
  147. Android might complain the application is not correctly installed.
  148. If so:
  149. - Check that the debug keystore is properly generated.
  150. - Check that the jarsigner executable is from JDK 8.
  151. If it still fails, open a command line and run `logcat <https://developer.android.com/studio/command-line/logcat>`_:
  152. ::
  153. adb logcat
  154. Then check the output while the application is installed;
  155. the error message should be presented there.
  156. Seek assistance if you can't figure it out.
  157. Application exits immediately
  158. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159. If the application runs but exits immediately, this might be due to
  160. one of the following reasons:
  161. - Make sure to use export templates that match your editor version; if
  162. you use a new Godot version, you *have* to update the templates too.
  163. - ``libgodot_android.so`` is not in ``libs/<android_arch>/``
  164. where ``<android_arch>`` is the device's architecture.
  165. - The device's architecture does not match the exported one(s).
  166. Make sure your templates were built for that device's architecture,
  167. and that the export settings included support for that architecture.
  168. In any case, ``adb logcat`` should also show the cause of the error.