compiling_for_android.rst 7.7 KB

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