android_custom_build.rst 9.9 KB

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