android_custom_build.rst 9.9 KB

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