Godot Engine – Multi-platform 2D and 3D game engine
[Android] Improve build flow by automatically setting up the build directory
Fredia Huya-Kouadio0fa44d83f821
diff --git a/editor/editor_node.cpp b/editor/editor_node.cppindex 8efc2a94be1..5ac0376102c 100644--- a/editor/editor_node.cpp+++ b/editor/editor_node.cpp@@ -216,7 +216,8 @@ EditorNode *EditorNode::singleton = nullptr;static const String EDITOR_NODE_CONFIG_SECTION = "EditorNode";-static const String REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE = TTRC("The Android build template is already installed in this project and it won't be overwritten.\nRemove the \"%s\" directory manually before attempting this operation again.");+static const String ANDROID_BUILD_TEMPLATE_ALREADY_INSTALLED_MESSAGE = TTRC("The Android build template is already installed.");+static const String REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE = TTRC("Remove the \"%s\" directory manually, or enable automatic deletion in the Editor Settings (Export > Android > Build > Automatically Delete Build Directory) before attempting this operation again.");static const String INSTALL_ANDROID_BUILD_TEMPLATE_MESSAGE = TTRC("This will set up your project for gradle Android builds by installing the source template to \"%s\".\nNote that in order to make gradle builds instead of using pre-built APKs, the \"Use Gradle Build\" option should be enabled in the Android export preset.");constexpr int LARGE_RESOURCE_WARNING_SIZE_THRESHOLD = 512'000; // 500 KB@@ -3386,6 +3387,58 @@ void EditorNode::_android_explore_build_templates() {OS::get_singleton()->shell_show_in_file_manager(ProjectSettings::get_singleton()->globalize_path(export_template_manager->get_android_build_directory(android_export_preset).get_base_dir()), true);}+void EditorNode::_android_remove_build_templates(bool p_prompt_for_removal) {+ String removal_text = ANDROID_BUILD_TEMPLATE_ALREADY_INSTALLED_MESSAGE;+ if (p_prompt_for_removal) {+ removal_text += "\n" + vformat(TTR(REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE), export_template_manager->get_android_build_directory(android_export_preset));+ }+ remove_android_build_template->set_text(removal_text);+ remove_android_build_template->popup_centered_clamped(Size2(600, 200) * EDSCALE, 0.7);+}++Error EditorNode::setup_android_build_template(const Ref<EditorExportPreset> &p_preset, bool p_confirmed) {+ android_export_preset = p_preset;+ if (export_template_manager->is_android_template_installed(p_preset)) {+ if (export_template_manager->is_android_build_version_valid(p_preset)) {+ if (!p_confirmed) {+ // The setup is valid, maybe the user wants to delete and reinstall it...+ _android_remove_build_templates(false);+ }+ return OK;+ }++ // If we get here, then the installed android build directory is no longer valid (e.g. editor upgrade).+ // Let's check whether we can automatically delete the build directory. If not, we return an error and let the+ // user handle the deletion.+ bool can_delete_automatically = EDITOR_GET("export/android/build/automatically_delete_build_directory");+ if (can_delete_automatically) {+ if (!p_confirmed) {+ install_android_build_template->popup_centered();+ return ERR_UNCONFIGURED;+ }++ if (export_template_manager->delete_android_build_directory(android_export_preset) == OK) {+ return setup_android_build_template(android_export_preset, p_confirmed);+ }+ }++ // If we get here, we need to prompt the user to delete the android build directory, either because we cannot do+ // it automatically, or because we tried to do it automatically and failed.+ _android_remove_build_templates(true);+ return ERR_UNCONFIGURED;+ } else if (!export_template_manager->can_install_android_template(p_preset)) {+ gradle_build_manage_templates->popup_centered();+ } else {+ if (p_confirmed) {+ return export_template_manager->install_android_template(p_preset);+ } else {+ install_android_build_template->popup_centered();+ }+ }++ return ERR_UNCONFIGURED;+}+static String _get_unsaved_scene_dialog_text(String p_scene_filename, uint64_t p_opened_timestamp) {const uint64_t scene_modified_time = FileAccess::get_modified_time(p_scene_filename);String unsaved_message;@@ -3722,14 +3775,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {case PROJECT_INSTALL_ANDROID_SOURCE: {if (p_confirmed) {- if (export_template_manager->is_android_template_installed(android_export_preset)) {- remove_android_build_template->set_text(vformat(TTR(REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE), export_template_manager->get_android_build_directory(android_export_preset)));- remove_android_build_template->popup_centered();- } else if (!export_template_manager->can_install_android_template(android_export_preset)) {- gradle_build_manage_templates->popup_centered();- } else {- export_template_manager->install_android_template(android_export_preset);- }+ setup_android_build_template(android_export_preset, p_confirmed);} else {bool has_custom_gradle_build = false;choose_android_export_profile->clear();@@ -3753,14 +3799,7 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {} else {choose_android_export_profile->hide();- if (export_template_manager->is_android_template_installed(android_export_preset)) {- remove_android_build_template->set_text(vformat(TTR(REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE), export_template_manager->get_android_build_directory(android_export_preset)));- remove_android_build_template->popup_centered();- } else if (export_template_manager->can_install_android_template(android_export_preset)) {- install_android_build_template->popup_centered();- } else {- gradle_build_manage_templates->popup_centered();- }+ setup_android_build_template(android_export_preset, p_confirmed);}}} break;@@ -9372,6 +9411,7 @@ EditorNode::EditorNode() {remove_android_build_template = memnew(ConfirmationDialog);remove_android_build_template->set_ok_button_text(OS::get_singleton()->get_platform_string(OS::PLATFORM_STRING_FILE_MANAGER_OPEN));+ remove_android_build_template->set_autowrap(true);remove_android_build_template->connect(SceneStringName(confirmed), callable_mp(this, &EditorNode::_android_explore_build_templates));gui_base->add_child(remove_android_build_template);diff --git a/editor/editor_node.h b/editor/editor_node.hindex d63a416a551..d934b4f347e 100644--- a/editor/editor_node.h+++ b/editor/editor_node.h@@ -546,6 +546,7 @@ private:void _android_export_preset_selected(int p_index);void _android_install_build_template();void _android_explore_build_templates();+ void _android_remove_build_templates(bool p_prompt_for_removal);void _request_screenshot();void _screenshot(bool p_use_utc = false);@@ -1039,6 +1040,8 @@ public:int execute_and_show_output(const String &p_title, const String &p_path, const List<String> &p_arguments, bool p_close_on_ok = true, bool p_close_on_errors = false, String *r_output = nullptr);+ Error setup_android_build_template(const Ref<EditorExportPreset> &p_preset, bool p_confirmed);+EditorNode();~EditorNode();diff --git a/editor/export/export_template_manager.cpp b/editor/export/export_template_manager.cppindex 774d4ce65db..ad6afe30343 100644--- a/editor/export/export_template_manager.cpp+++ b/editor/export/export_template_manager.cpp@@ -1418,10 +1418,44 @@ bool ExportTemplateManager::is_android_template_installed(const Ref<EditorExportreturn DirAccess::exists(get_android_build_directory(p_preset));}+bool ExportTemplateManager::is_android_build_version_valid(const Ref<EditorExportPreset> &p_preset) {+ String build_version_path = get_android_build_directory(p_preset).get_base_dir().path_join(".build_version");+ Ref<FileAccess> f = FileAccess::open(build_version_path, FileAccess::READ);+ if (f.is_valid()) {+ String current_version = get_android_template_identifier(p_preset);+ String installed_version = f->get_line().strip_edges();+ if (current_version == installed_version) {+ return true;+ }+ }+ return false;+}+bool ExportTemplateManager::can_install_android_template(const Ref<EditorExportPreset> &p_preset) {return FileAccess::exists(get_android_source_zip(p_preset));}+Error ExportTemplateManager::delete_android_build_directory(const Ref<EditorExportPreset> &p_preset) {+ Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);+ ERR_FAIL_COND_V(da.is_null(), ERR_CANT_CREATE);++ String build_dir = get_android_build_directory(p_preset);+ String parent_dir = build_dir.get_base_dir();+ print_verbose("Deleting android build directory " + build_dir);++ ProgressDialog::get_singleton()->add_task("deleting_android_build_dir", TTR("Deleting Android Build Directory"), 1);++ Error err = da->change_dir(parent_dir);+ ERR_FAIL_COND_V(err != OK, err);++ err = da->erase_contents_recursive();+ ERR_FAIL_COND_V(err != OK, err);++ ProgressDialog::get_singleton()->end_task("deleting_android_build_dir");+ EditorFileSystem::get_singleton()->scan_changes();+ return OK;+}+Error ExportTemplateManager::install_android_template(const Ref<EditorExportPreset> &p_preset) {const String source_zip = get_android_source_zip(p_preset);ERR_FAIL_COND_V(!FileAccess::exists(source_zip), ERR_CANT_OPEN);@@ -1437,6 +1471,7 @@ Error ExportTemplateManager::install_android_template_from_file(const String &p_String build_dir = get_android_build_directory(p_preset);String parent_dir = build_dir.get_base_dir();+ print_verbose("Installing android build directory " + build_dir);// Make parent of the build dir (if it does not exist).da->make_dir_recursive(parent_dir);diff --git a/editor/export/export_template_manager.h b/editor/export/export_template_manager.hindex b9547300a28..40f96db7c55 100644--- a/editor/export/export_template_manager.h+++ b/editor/export/export_template_manager.h@@ -301,10 +301,12 @@ public:static String get_android_source_zip(const Ref<EditorExportPreset> &p_preset);static String get_android_template_identifier(const Ref<EditorExportPreset> &p_preset);- bool is_android_template_installed(const Ref<EditorExportPreset> &p_preset);- bool can_install_android_template(const Ref<EditorExportPreset> &p_preset);- Error install_android_template(const Ref<EditorExportPreset> &p_preset);- Error install_android_template_from_file(const String &p_file, const Ref<EditorExportPreset> &p_preset);+ static bool is_android_template_installed(const Ref<EditorExportPreset> &p_preset);+ static bool is_android_build_version_valid(const Ref<EditorExportPreset> &p_preset);+ static bool can_install_android_template(const Ref<EditorExportPreset> &p_preset);+ static Error delete_android_build_directory(const Ref<EditorExportPreset> &p_preset);+ static Error install_android_template(const Ref<EditorExportPreset> &p_preset);+ static Error install_android_template_from_file(const String &p_file, const Ref<EditorExportPreset> &p_preset);void popup_manager();bool is_downloading() const;diff --git a/platform/android/export/export.cpp b/platform/android/export/export.cppindex 520eeb6b2d3..bc4aca776d1 100644--- a/platform/android/export/export.cpp+++ b/platform/android/export/export.cpp@@ -51,6 +51,7 @@ void register_android_exporter() {EDITOR_DEF_BASIC("export/android/debug_keystore_user", DEFAULT_ANDROID_KEYSTORE_DEBUG_USER);EDITOR_DEF_BASIC("export/android/debug_keystore_pass", DEFAULT_ANDROID_KEYSTORE_DEBUG_PASSWORD);EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/debug_keystore_pass", PROPERTY_HINT_PASSWORD));+ EDITOR_DEF_BASIC("export/android/build/automatically_delete_build_directory", false);#ifdef ANDROID_ENABLEDEDITOR_DEF_BASIC("export/android/install_exported_apk", !OS::get_singleton()->has_feature("horizonos"));diff --git a/platform/android/export/export_plugin.cpp b/platform/android/export/export_plugin.cppindex 7b17b6970f4..4ef815e5d1d 100644--- a/platform/android/export/export_plugin.cpp+++ b/platform/android/export/export_plugin.cpp@@ -228,8 +228,6 @@ static const char *ANDROID_PERMS[] = {nullptr};-static const char *MISMATCHED_VERSIONS_MESSAGE = "Android build version mismatch:\n| Template installed: %s\n| Requested version: %s\nPlease reinstall Android build template from 'Project' menu.";-static const char *GDEXTENSION_LIBS_PATH = "libs/gdextensionlibs.json";// This template string must be in sync with the content of 'platform/android/java/lib/src/main/java/res/mipmap-anydpi-v26/icon.xml'.@@ -2984,28 +2982,38 @@ bool EditorExportPlatformAndroid::has_valid_export_configuration(const Ref<Editoerr += template_err;}} else {- // Validate the custom gradle android source template.- bool android_source_template_valid = false;- const String android_source_template = p_preset->get("gradle_build/android_source_template");- if (!android_source_template.is_empty()) {- android_source_template_valid = FileAccess::exists(android_source_template);- if (!android_source_template_valid) {- err += TTR("Custom Android source template not found.") + "\n";+ r_missing_templates = false;+ valid = true;++ // Check if the build template is already installed and is valid.+ bool android_build_installed_and_valid = false;+ if (ExportTemplateManager::is_android_template_installed(p_preset)) {+ if (ExportTemplateManager::is_android_build_version_valid(p_preset)) {+ android_build_installed_and_valid = true;+ } else {+ bool can_automatically_delete_build_dir = EDITOR_GET("export/android/build/automatically_delete_build_directory");+ if (!can_automatically_delete_build_dir) {+ // Show an error requesting the user to delete the current build directory manually.+ err += vformat(TTR("Invalid build directory. Manually delete \"%s\" or enable automatic deletion in the Editor Settings (Export > Android > Build > Automatically Delete Build Directory)."), ExportTemplateManager::get_android_build_directory(p_preset)) + "\n";+ valid = false;+ }}}- // Validate the installed build template.- bool installed_android_build_template = FileAccess::exists(ExportTemplateManager::get_android_build_directory(p_preset).path_join("build.gradle"));- if (!installed_android_build_template) {- if (!android_source_template_valid) {- r_missing_templates = !exists_export_template("android_source.zip", &err);+ if (!android_build_installed_and_valid) {+ // Validate the custom gradle android source templates.+ const String android_source_template = p_preset->get("gradle_build/android_source_template");+ if (!android_source_template.is_empty()) {+ if (!FileAccess::exists(android_source_template)) {+ err += TTR("Custom Android source template not found.") + "\n";+ valid = false;+ }+ } else {+ // Default android build template validation.+ valid = exists_export_template("android_source.zip", &err);+ r_missing_templates = !valid;}- err += TTR("Android build template not installed in the project. Install it from the Project menu.") + "\n";- } else {- r_missing_templates = false;}-- valid = installed_android_build_template && !r_missing_templates;}// Validate the rest of the export configuration.@@ -3152,18 +3160,7 @@ bool EditorExportPlatformAndroid::has_valid_project_configuration(const Ref<Edit}bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build");- if (gradle_build_enabled) {- String build_version_path = ExportTemplateManager::get_android_build_directory(p_preset).get_base_dir().path_join(".build_version");- Ref<FileAccess> f = FileAccess::open(build_version_path, FileAccess::READ);- if (f.is_valid()) {- String current_version = ExportTemplateManager::get_android_template_identifier(p_preset);- String installed_version = f->get_line().strip_edges();- if (current_version != installed_version) {- err += vformat(TTR(MISMATCHED_VERSIONS_MESSAGE), installed_version, current_version);- err += "\n";- }- }- } else {+ if (!gradle_build_enabled) {if (_is_transparency_allowed(p_preset)) {// Warning only, so don't override `valid`.err += vformat(TTR("\"Use Gradle Build\" is required for transparent background on Android"));@@ -3758,21 +3755,19 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportP}if (use_gradle_build) {+ if (ep.step(TTR("Starting gradle build..."), 0)) {+ return ERR_SKIP;+ }print_verbose("Starting gradle build...");//test that installed build version is alright{- print_verbose("Checking build version...");- String gradle_base_directory = gradle_build_directory.get_base_dir();- Ref<FileAccess> f = FileAccess::open(gradle_base_directory.path_join(".build_version"), FileAccess::READ);- if (f.is_null()) {- add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Trying to build from a gradle built template, but no version info for it exists. Please reinstall from the 'Project' menu."));- return ERR_UNCONFIGURED;+ if (ep.step(TTR("Checking build directory..."), 10)) {+ return ERR_SKIP;}- String current_version = ExportTemplateManager::get_android_template_identifier(p_preset);- String installed_version = f->get_line().strip_edges();- print_verbose("- build version: " + installed_version);- if (installed_version != current_version) {- add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR(MISMATCHED_VERSIONS_MESSAGE), installed_version, current_version));+ print_verbose("Checking build directory...");+ err = EditorNode::get_singleton()->setup_android_build_template(p_preset, true);+ if (err != OK) {+ add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Unable to set up Android build directory. Please fix by manually deleting the \"%s\" directory and try again. Or enable automatic deletion in the Editor Settings (Export > Android > Build > Automatically Delete Build Directory)."), gradle_build_directory));return ERR_UNCONFIGURED;}}@@ -3810,6 +3805,10 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportP_clear_assets_directory(p_preset);String gdextension_libs_path = gradle_build_directory.path_join(GDEXTENSION_LIBS_PATH);_remove_copied_libs(gdextension_libs_path);++ if (ep.step(TTR("Exporting project files..."), 20)) {+ return ERR_SKIP;+ }print_verbose("Exporting project files...");CustomExportData user_data;user_data.assets_directory = assets_directory;@@ -3850,6 +3849,9 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportPfa->store_string(JSON::stringify(user_data.libs, "\t"));}+ if (ep.step(TTR("Storing command line flags..."), 30)) {+ return ERR_SKIP;+ }print_verbose("Storing command line flags...");store_file_at_path(assets_directory + "/_cl_", command_line_flags);@@ -3968,6 +3970,9 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportP// to avoid accidentally leaking sensitive information when sharing verbose logs for troubleshooting.// Any non-sensitive additions to the command line arguments must be done above this section.// Sensitive additions must be done below the logging statement.+ if (ep.step(TTR("Build Android project..."), 40)) {+ return ERR_SKIP;+ }print_verbose("Build Android project using gradle command: " + String("\n") + build_command + " " + join_list(cmdline, String(" ")));if (should_sign) {@@ -4088,6 +4093,9 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportPprint_verbose("Successfully completed Android gradle build.");#endif+ if (ep.step(TTR("Build complete."), 105)) {+ return ERR_SKIP;+ }return OK;}// This is the start of the Legacy build system