compiling_for_android.rst 7.9 KB

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