为Android平台自定义编译

Godot提供了使用自定义构建Android模板的选项。与使用Godot附带的已预先构建好的模板不同,而是将实际的安卓Java项目安装到项目文件夹中。然后Godot将构建它,并在每次导出项目时将其作为导出模板使用。

出于某些原因,您可能想要这样做:

  • 在构建项目之前对其进行修改。
  • 添加与您的项目一起构建的外部SDK。

Configuring the custom build is a more or less straightforward process, but it may take a while to get used to how the Android SDK works.

将提供尽可能详细的说明来完成这一过程。

Set up the custom build environment

进入项目菜单,安装 自定义构建 模板:

../../../_images/custom_build_install_template.png

Make sure export templates are downloaded. If not, this menu will help you download them.

A Gradle-based Android project will be created under res://android/build. Editing these files is not needed unless you want to create your own add-ons, or you really need to modify the project.

安装Android SDK(命令行版本)

These are the steps for installing the Android SDK using command line. The advantage of this approach is the simplicity and small download/install size. It can be more challenging though. The Android Studio approach is easier, but it requires downloading and installing Android Studio (which may require more than 1 GB of storage).

安装JDK

The Android SDK doesn’t come with Java, so it needs to be installed manually. You need to install a Java SDK (not just the runtime or JRE). OpenJDK 8 is required, newer versions won’t work.

Windows上, 在安装界面的 *Custom Setup*确保你启用了 “Set JAVA_HOME variable”。这之后你必须重新启动Godot,不然Godot将无法找到``JAVA_HOME`` 变量。

下载命令行工具

前往`Android Studio 下载页 <https://developer.android.com/studio/#command-tools>`_。为了节省磁盘空间,你应该不想要完整的IDE,所以不要下载那个。

注解

如果你确实想要Android Studio,请查看后面关于在Android Studio中操作的说明。

Look on that page for the Command line tools only section. Currently, they are listed under Download Options. Scroll down a bit until you see them and download the ZIP file for your platform.

这可能有点令人困惑,请务必认真遵循以下说明:

在你想要的任何地方,创建一个名为 android-sdk 的文件夹(**必须**是个空目录)。在Windows平台上,用下面这个路径就可以了:

  1. C:\users\<yourusername>\android-sdk

在``android-sdk``文件夹中创建一个 cmdline-tools 的空文件夹。然后将 Android SDK 压缩文件解压到``android-sdk/cmdline-tools`` 文件夹。

注解

如果是Windows平台,禁止使用默认的Widnows解压工具(比如 Windows Explorer)解压ZIP压缩包。必须使用其他工具,比如 7zip, WinRAR 或者 Powershell 的``Expand-Archive`` 命令。如果使用默认的解压工具解压压缩包,文件将不会被正确解压,之后也会运行错误!

cmdline-tools``文件夹现在应该包含解压之后的文件夹 ``tools。最后,把``tools`` 文件夹重命名为``latest``。

Your final directory structure should look like this :

  1. android-sdk/
  2. android-sdk/cmdline-tools/
  3. android-sdk/cmdline-tools/latest
  4. android-sdk/cmdline-tools/latest/allTheOtherFiles

为了让sdkmanager(在bin文件夹下)能正常工作,需要用这种方式设置目录结构。

接受许可证

谷歌要求你接受对应的许可证,才可以使用Android SDK工具。

To do this, the sdkmanager must be executed from the command line with a special argument. Navigate to the tools/bin directory inside the SDK folder (instructions provided for Windows users, as Linux and macOS users are expected to understand how command line navigation works):

../../../_images/custom_build_bin_folder.png

然后打开命令行窗口:

../../../_images/custom_build_open_shell.png

In there, run sdkmanager --licenses:

../../../_images/custom_build_sdkmanager.png

This will ask you to accept several licenses, just write y and press Enter on every of them until it’s done.

Afterwards, install the platform tools (this is required to install adb):

../../../_images/custom_build_platform_tools.png

如果收到一条这样的报错 Warning: Could not create settings,尝试 ./sdkmanager --sdk_root=../../ --licenses 或``./sdkmanager —sdk_root=../../ platform-tools``。这些命令必须在``/tools/bin/文件夹里执行,因为 ``--sdk_root 路径是相对的。

生成密钥库

Once the platform tools are installed, the last step is to generate a debug keystore (this is needed to build). Go up two folders by writing:

  1. cd ..\..\..

(or open a new shell in the android-sdk folder).

And you need to input the following line (This should work out of the box. However, if you haven’t set the JAVA_HOME variable on Windows, there are further instructions below):

  1. keytool -keyalg RSA -genkeypair -alias androiddebugkey -keypass android -keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US" -validity 9999

On Windows, if you did not install the JAVA_HOME variable, the full path to Java should be provided. You need to add & at the beginning of the line if you use PowerShell; it’s not needed for the regular cmd.exe console.

To make it clearer, here is an capture of a line that works on PowerShell (by adding & and the full Java path before keytool.exe). Again, keep in mind that you need Java installed:

../../../_images/custom_build_command_line.png

(right-click and open the image in a new tab if this appears too small)

设置Godot

Go to the Editor Settings and set up a few fields in Export > Android. Make sure they look like the following:

../../../_images/custom_build_editor_settings.png

(right-click and open the image in a new tab if this appears too small)

As it can be seen, most paths are inside either the android-sdk folder you originally created, or inside the Java install. For Linux and macOS users, jarsigner is often located in /usr/bin.

有了这个,您应该已经准备就绪。

安装安卓SDK(Android Studio)

If you just finished installing the SDK via the command-line tools, feel free to skip this section entirely. The Android Studio path is easier, but it takes up more disk space. It’s also useful if you plan to develop Godot for Android (modify the Java source code) or if you plan to develop add-ons.

下载并安装Android Studio

下载最新版本的Android Studio。安装时,请注意 android-sdk 目录创建的位置。

../../../_images/custom_build_install_android_studio1.png

注解

这很有趣,默认情况下,它建议的路径包含空格(您会为此抱怨不已)。所以它必须改变。

无论如何,最好在用户文件夹中选择不同的路径。推荐的方法通常是:

  1. C:\Users\<yourusername>\Documents\android-sdk

用实际用户名替换 yourusername。一旦正确,在同一屏幕上从以上列表中选择:

  • Android SDK
  • Android SDK 平台工具

其余都不需要,因为构建系统会自动获取它们。选定它们后,继续安装。

生成密钥库

Go to the folder where you installed android-sdk in the previous step, use File Explorer and open a command line tool there:

../../../_images/custom_build_open_shell.png

The actual command line to type is the following. On Linux and macOS, it should work out of the box, but on Windows, it needs additional details (keep reading afterwards).

  1. keytool -keyalg RSA -genkeypair -alias androiddebugkey -keypass android -keystore debug.keystore -storepass android -dname "CN=Android Debug,O=Android,C=US" -validity 9999

On Windows, the full path to Java should be provided (and & needs to be added at the beginning on the line if you use PowerShell, it’s not needed for the regular cmd.exe console). Don’t worry, at least by using Android Studio on Windows, Java comes bundled with it.

To make it clearer, here is a screen capture of a line that works on PowerShell (by adding & and the full Java Path to keytool.exe; remove & if you use cmd.exe). It uses a path to the Java version that comes with Android Studio:

../../../_images/custom_build_command_line2.png

(right-click and open the image in a new tab if this appears too small)

设置Godot

Go to the Editor Settings and set up a few fields in Export > Android. Make sure they look like the following:

../../../_images/custom_build_editor_settings2.png

(right-click and open the image in a new tab if this appears too small)

As it can be seen, most paths are inside either the android-sdk folder you originally created, or inside the Java install. For Linux and macOS users, jarsigner is often located in /usr/bin.

有了这个,您应该已经准备就绪。

Enabling the custom build and exporting

When setting up the Android project in the Project > Export dialog, Custom Build needs to be enabled:

../../../_images/custom_build_enable.png

From now on, attempting to export the project or one-click deploy will call the Gradle build system to generate fresh templates (this window will appear every time):

../../../_images/custom_build_gradle.png

生成的模板将在以后自动使用,因此不需要进一步的配置。