android_custom_build.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. .. _doc_android_custom_build:
  2. Custom builds for Android
  3. =========================
  4. Godot provides the option to use custom build Android templates. Instead of
  5. using the already pre-built template that ships with Godot, an actual Android
  6. Java project gets installed into your project folder. Godot will then build it
  7. and use it as an export template every time you export the project.
  8. There are some reasons why you may want to do this:
  9. * Modify the project before it's built.
  10. * Add external SDKs that build with your project.
  11. Configuring the custom build is a more or less straightforward process,
  12. but it may take a while to get used to how the Android SDK works.
  13. Instructions will be provided as detailed as possible to do this process.
  14. Set up the custom build environment
  15. -----------------------------------
  16. Go to the Project menu, and install the *Custom Build* template:
  17. .. image:: img/custom_build_install_template.png
  18. Make sure export templates are downloaded. If not, this menu will help you
  19. download them.
  20. A Gradle-based Android project will be created under ``res://android/build``.
  21. Editing these files is not needed unless you want to :ref:`create
  22. your own add-ons<doc_android_plugin>`, or you really need to modify the project.
  23. Install the Android SDK (command-line version)
  24. ----------------------------------------------
  25. These are the steps for installing the Android SDK using command line. The
  26. advantage of this approach is the simplicity and small download/install size. It
  27. can be more challenging though. The Android Studio approach is easier, but it
  28. requires downloading and installing Android Studio (which may require more than
  29. 1 GB of storage).
  30. Install a JDK
  31. ^^^^^^^^^^^^^
  32. The Android SDK doesn't come with Java, so it needs to be installed manually.
  33. You need to install a Java SDK (**not** just the runtime or JRE).
  34. `OpenJDK 8 <https://adoptopenjdk.net/index.html>`__ is required, newer
  35. versions won't work.
  36. On Windows, make sure that you enable "Set ``JAVA_HOME`` variable" in the *Custom Setup* view
  37. of the installer. You have to restart Godot after this, otherwise Godot can't
  38. find the ``JAVA_HOME`` variable.
  39. Download the command-line tools
  40. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  41. Go to the `Android Studio download page <https://developer.android.com/studio/#command-tools>`_.
  42. To save disk space, you don't want the full IDE, so don't download it.
  43. .. note::
  44. If you do want Android Studio, read further down for instructions for
  45. doing the same using Android Studio.
  46. Look on that page for the *Command line tools only* section. Currently, they are listed under
  47. *Download Options*. Scroll down a bit until you see them and download the ZIP file for
  48. your platform.
  49. This may appear a little confusing, but be sure to follow these instructions
  50. carefully:
  51. Create a new folder anywhere you want named ``android-sdk`` (it **must** be
  52. an empty directory). On Windows, the following path is usually good enough:
  53. .. code-block:: none
  54. C:\users\<yourusername>\android-sdk
  55. Create an empty folder named ``cmdline-tools`` inside of the ``android-sdk`` folder.
  56. Then unzip the Android SDK ZIP file into the ``android-sdk/cmdline-tools`` folder.
  57. .. note::
  58. If you're on Windows, you must not extract the ZIP archive with the default
  59. Windows extractor (e.g. Windows Explorer). You have to use another tool
  60. like 7zip, WinRAR or the Powershell ``Expand-Archive`` command. If you
  61. extract the archive with the default Windows extractor, the files are not
  62. extracted correctly and you will run into errors later on!
  63. The ``cmdline-tools`` folder should now contain the unzipped folder called ``tools``.
  64. Finally, rename the ``tools`` folder to ``latest``.
  65. Your final directory structure should look like this :
  66. .. code-block:: none
  67. android-sdk/
  68. android-sdk/cmdline-tools/
  69. android-sdk/cmdline-tools/latest
  70. android-sdk/cmdline-tools/latest/allTheOtherFiles
  71. We need to setup the directory structure this way for the sdkmanager (inside the bin folder) to work.
  72. Accepting the licenses
  73. ^^^^^^^^^^^^^^^^^^^^^^
  74. To be able to use the Android SDK tools, Google requires you to accept
  75. its licenses.
  76. To do this, the ``sdkmanager`` must be executed from the command line with a
  77. special argument. Navigate to the ``tools/bin`` directory inside the SDK folder
  78. (instructions provided for Windows users, as Linux and macOS users are expected
  79. to understand how command line navigation works):
  80. .. image:: img/custom_build_bin_folder.png
  81. Then open a command line window:
  82. .. image:: img/custom_build_open_shell.png
  83. In there, run ``sdkmanager --licenses``:
  84. .. image:: img/custom_build_sdkmanager.png
  85. This will ask you to accept several licenses, just write ``y`` and press :kbd:`Enter`
  86. on every of them until it's done.
  87. Afterwards, install the platform tools (this is required to install ``adb``):
  88. .. image:: img/custom_build_platform_tools.png
  89. If you get an error saying ``Warning: Could not create settings``, try ``./sdkmanager --sdk_root=../../ --licenses`` or ``./sdkmanager --sdk_root=../../ platform-tools``. These must be executed inside the ``/tools/bin/`` folder because the path for ``--sdk_root`` is relative.
  90. Generating the keystore
  91. ^^^^^^^^^^^^^^^^^^^^^^^
  92. Once the *platform tools* are installed, the last step is to generate a debug
  93. keystore (this is needed to build). Go up two folders by writing:
  94. .. code-block:: shell
  95. cd ..\..\..
  96. (or open a new shell in the ``android-sdk`` folder).
  97. And you need to input the following line (This should work out of the box.
  98. However, if you haven't set the ``JAVA_HOME`` variable on Windows,
  99. there are further instructions below):
  100. .. code-block:: shell
  101. keytool -keyalg RSA -genkeypair -alias androiddebugkey -keypass android -keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US" -validity 9999
  102. On Windows, if you did not install the ``JAVA_HOME`` variable, the full path to Java
  103. should be provided. You need to add ``&`` at the beginning of the line if you use
  104. PowerShell; it's not needed for the regular ``cmd.exe`` console.
  105. To make it clearer, here is an capture of a line that works on PowerShell (by
  106. adding ``&`` and the full Java path before ``keytool.exe``). Again, keep in mind that you
  107. need Java installed:
  108. .. image:: img/custom_build_command_line.png
  109. (right-click and open the image in a new tab if this appears too small)
  110. Setting up Godot
  111. ^^^^^^^^^^^^^^^^
  112. Go to the **Editor Settings** and set up a few fields in **Export > Android**.
  113. Make sure they look like the following:
  114. .. image:: img/custom_build_editor_settings.png
  115. (right-click and open the image in a new tab if this appears too small)
  116. As it can be seen, most paths are inside either the ``android-sdk`` folder you
  117. originally created, or inside the Java install. For Linux and macOS users,
  118. ``jarsigner`` is often located in ``/usr/bin``.
  119. With this, you should be all set.
  120. Install the Android SDK (Android Studio)
  121. ----------------------------------------
  122. If you just finished installing the SDK via the command-line tools, feel free to
  123. skip this section entirely. The Android Studio path is easier, but it takes up
  124. more disk space. It's also useful if you plan to develop Godot for Android
  125. (modify the Java source code) or if you plan to develop add-ons.
  126. Download and install Android Studio
  127. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  128. Download the latest version of Android Studio. When installing, pay attention to
  129. where the *android-sdk* directory is created.
  130. .. image:: img/custom_build_install_android_studio1.png
  131. .. note:: This is funny, the path it proposes by default contains whitespace (and complains about it). It must be changed.
  132. In any case, it's better to select a different path inside your user folders.
  133. The recommended one is usually:
  134. .. code-block:: none
  135. C:\Users\<yourusername>\Documents\android-sdk
  136. Replace *yourusername* by your actual user name. Once it's correct, select from
  137. the list above in the same screen:
  138. * Android SDK
  139. * Android SDK Platform
  140. The rest are not needed, because the build system will fetch them itself. After
  141. selecting them, go on with the installation.
  142. Generating the keystore
  143. ^^^^^^^^^^^^^^^^^^^^^^^
  144. Go to the folder where you installed ``android-sdk`` in the previous step, use File
  145. Explorer and open a command line tool there:
  146. .. image:: img/custom_build_open_shell.png
  147. The actual command line to type is the following. On Linux and macOS, it should
  148. work out of the box, but on Windows, it needs additional details (keep reading
  149. afterwards).
  150. .. code-block:: shell
  151. keytool -keyalg RSA -genkeypair -alias androiddebugkey -keypass android -keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US" -validity 9999
  152. On Windows, the full path to Java should be provided (and ``&`` needs to be
  153. added at the beginning on the line if you use PowerShell, it's not needed for
  154. the regular ``cmd.exe`` console). Don't worry, at least by using Android Studio
  155. on Windows, Java comes bundled with it.
  156. To make it clearer, here is a screen capture of a line that works on PowerShell
  157. (by adding ``&`` and the full Java Path to ``keytool.exe``; remove ``&`` if you
  158. use ``cmd.exe``). It uses a path to the Java version that comes with Android
  159. Studio:
  160. .. image:: img/custom_build_command_line2.png
  161. (right-click and open the image in a new tab if this appears too small)
  162. Setting up Godot
  163. ^^^^^^^^^^^^^^^^
  164. Go to the **Editor Settings** and set up a few fields in **Export > Android**.
  165. Make sure they look like the following:
  166. .. image:: img/custom_build_editor_settings2.png
  167. (right-click and open the image in a new tab if this appears too small)
  168. As it can be seen, most paths are inside either the ``android-sdk`` folder you
  169. originally created, or inside the Java install. For Linux and macOS users,
  170. ``jarsigner`` is often located in ``/usr/bin``.
  171. With this, you should be all set.
  172. Enabling the custom build and exporting
  173. ---------------------------------------
  174. When setting up the Android project in the **Project > Export** dialog,
  175. **Custom Build** needs to be enabled:
  176. .. image:: img/custom_build_enable.png
  177. From now on, attempting to export the project or one-click deploy will call the
  178. `Gradle <https://gradle.org/>`__ build system to generate fresh templates (this
  179. window will appear every time):
  180. .. image:: img/custom_build_gradle.png
  181. The templates built will be used automatically afterwards, so no further
  182. configuration is needed.