compiling_for_android.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. .. _doc_compiling_for_android:
  2. Compiling for Android
  3. =====================
  4. .. highlight:: shell
  5. Note
  6. ----
  7. For 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 do all the steps mentioned in the
  11. :ref:`doc_exporting_for_android` tutorial before attempting your custom
  12. export template.
  13. Requirements
  14. ------------
  15. For compiling under Windows, Linux or macOS, the following is required:
  16. - Python 2.7+ or Python 3.5+
  17. - SCons build system
  18. - [Windows only] PyWin32 (optional, for parallel compilation)
  19. - Android SDK version 23.0.3 [Note: Please install all tools and extras of the SDK Manager]
  20. - Android build tools version 19.1
  21. - Android NDK r13 or later
  22. - Gradle (will be downloaded and installed automatically if missing)
  23. - JDK 6 or later (either OpenJDK or Oracle JDK) - JDK 9 & 10 do not work with current Gradle.
  24. .. seealso:: For a general overview of SCons usage for Godot, see
  25. :ref:`doc_introduction_to_the_buildsystem`.
  26. Setting up the buildsystem
  27. --------------------------
  28. Set the environment variable ANDROID_HOME to point to the Android
  29. SDK.
  30. Set the environment variable ANDROID_NDK_ROOT to point to the
  31. Android NDK.
  32. To set those environment variables on Windows, press Windows+R, type
  33. "control system", then click on **Advanced system settings** in the left
  34. pane, then click on **Environment variables** on the window that
  35. appears.
  36. To set those environment variables on Unix (e.g. Linux, macOS), use
  37. ``export ANDROID_HOME=/path/to/android-sdk`` and
  38. ``export ANDROID_NDK_ROOT=/path/to/android-ndk``.
  39. Where /path/to/android-sdk and /path/to/android-ndk is the path where Android SDK
  40. and Android NDK are placed on your PC.
  41. Building the export templates
  42. -----------------------------
  43. Godot needs two export templates for Android: the optimized "release"
  44. template (`android_release.apk`) and the debug version (`android_debug.apk`).
  45. Compiling the standard export templates is done by calling scons with
  46. the following arguments:
  47. - Release template (used when exporting with "Debugging Enabled" OFF)
  48. ::
  49. C:\godot> scons platform=android target=release
  50. C:\godot> cd platform/android/java
  51. C:\godot\platform\android\java> gradlew build
  52. (on Linux or macOS, execute the `gradlew` script with `./gradlew build`)
  53. The resulting APK is in:
  54. ::
  55. bin\android_release.apk
  56. - Debug template (used when exporting with "Debugging Enabled" ON)
  57. ::
  58. C:\godot> scons platform=android target=release_debug
  59. C:\godot> cd platform/android/java
  60. C:\godot\platform\android\java> gradlew build
  61. The resulting APK is in:
  62. ::
  63. bin\android_debug.apk
  64. Faster compilation
  65. ~~~~~~~~~~~~~~~~~~
  66. If you are on Unix or installed PyWin32 on Windows and have multiple CPU
  67. cores available, you can speed up the compilation by adding the ``-jX``
  68. argument to the SCons command, where ``X`` is the number of cores that you
  69. want to allocate to the compilation, e.g. ``scons -j4``.
  70. Adding support for x86 devices
  71. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. If you also want to include support for x86 devices, run the scons command
  73. a second time with the ``android_arch=x86`` argument before building the APK
  74. with Gradle. For example for the release template:
  75. ::
  76. C:\godot> scons platform=android target=release
  77. C:\godot> scons platform=android target=release android_arch=x86
  78. C:\godot> cd platform/android/java
  79. C:\godot\platform\android\java> gradlew build
  80. This will create a fat binary that works in both platforms, but will add
  81. about 6 megabytes to the APK.
  82. Troubleshooting
  83. ~~~~~~~~~~~~~~~
  84. It might be necessary to clean the build cache between two APK compilations,
  85. as some users have reported issues when building the two export templates
  86. one after the other.
  87. Using the export templates
  88. --------------------------
  89. As export templates for Android, Godot needs release and debug APKs that
  90. were compiled against the same version/commit as the editor. If you are
  91. using official binaries for the editor, make sure to install the matching
  92. export templates, or to build your own from the same version.
  93. When exporting your game, Godot opens the APK, changes a few things inside and
  94. adds your files.
  95. Installing the templates
  96. ~~~~~~~~~~~~~~~~~~~~~~~~
  97. The newly-compiled templates (android_debug.apk and android_release.apk)
  98. must be copied to Godot's templates folder with their respective names.
  99. The templates folder can be located in:
  100. - Windows: ``C:\Users\[username]\AppData\Roaming\Godot\templates\[version]\``
  101. - Linux: ``/home/[username]/.local/share/godot/templates/[version]/``
  102. - macOS: ``/Users/[username]/Library/Application Support/Godot/templates/[version]/``
  103. ``[version]`` is of the form ``major.minor[.patch].status`` using values from
  104. ``version.py`` in your Godot source repository (e.g. ``3.0.5.stable`` or `3.1.dev``).
  105. You also need to write this same version string to a ``version.txt`` file located
  106. next to your export templates.
  107. .. TODO: Move these paths to a common reference page
  108. However, if you are writing your custom modules or custom C++ code, you
  109. might instead want to configure your APKs as custom export templates
  110. here:
  111. .. image:: img/andtemplates.png
  112. You don't even need to copy them, you can just reference the resulting
  113. file in the ``bin\`` directory of your Godot source folder, so that the
  114. next time you build you will automatically have the custom templates
  115. referenced.
  116. Troubleshooting
  117. ---------------
  118. Application not installed
  119. ~~~~~~~~~~~~~~~~~~~~~~~~~
  120. Android might complain the application is not correctly installed. If
  121. so, check the following:
  122. - Check that the debug keystore is properly generated.
  123. - Check that jarsigner is from JDK 6, 7 or 8.
  124. If it still fails, open a command line and run logcat:
  125. ::
  126. C:\android-sdk\platform-tools> adb logcat
  127. And check the output while the application is installed. Reason for
  128. failure should be presented there.
  129. Seek assistance if you can't figure it out.
  130. Application exits immediately
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. If the application runs but exits immediately, there might be one of the
  133. following reasons:
  134. - Make sure to use export templates that match your editor version; if
  135. you use a new Godot version, you *have* to update the templates too.
  136. - libgodot_android.so is not in ``lib/armeabi-v7a`` or ``lib/armeabi``
  137. - Device does not support armv7 (try compiling yourself for armv6)
  138. - Device is Intel, and apk is compiled for ARM.
  139. In any case, ``adb logcat`` should also show the cause of the error.
  140. Compilation fails
  141. ~~~~~~~~~~~~~~~~~
  142. On Linux systems with Kernel version 4.3 or newer, compilation may fail
  143. with the error "pthread_create failed: Resource temporarily unavailable."
  144. This is because of a change in the way Linux limits thread creation. But
  145. you can change those limits through the command line. Please read this
  146. section thoroughly before beginning.
  147. First open a terminal, then begin compilation as usual (it may be a good
  148. idea to run a --clean first). While compiling enter the following in
  149. your terminal:
  150. ::
  151. user@host:~/$ top -b -n 1 | grep scons
  152. The output should list a scons process, with its PID as the first number
  153. in the output. For example the PID 1077 in the output shown below:
  154. ::
  155. user@host:~/$ top -b -n 1 | grep scons
  156. 1077 user 20 0 10544 1628 1508 S 0.000 0.027 0:00.00 grep
  157. Now you can use another command to increase the number of processes that
  158. scons is allowed to spawn. You can check its current limits with:
  159. ::
  160. user@host:~/$ prlimit --pid=1077 --nproc
  161. You can increase those limits with the command:
  162. ::
  163. user@host:~/$ prlimit --pid=1077 --nproc=60000:60500
  164. Obviously you should substitute the scons PID output by top and a limits
  165. that you think suitable. These are in the form --nproc=soft:hard where
  166. soft must be lesser than or equal to hard. See the man page for more
  167. information.
  168. If all went well, and you entered the prlimit command while scons was
  169. running, then your compilation should continue without the error.