| 1 | /**************************************************************************/ |
| 2 | /* android_editor_gradle_runner.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #ifdef ANDROID_ENABLED |
| 32 | #include "android_editor_gradle_runner.h" |
| 33 | |
| 34 | #include "editor/editor_interface.h" |
| 35 | #include "editor/settings/editor_settings.h" |
| 36 | #include "scene/gui/dialogs.h" |
| 37 | #include "scene/gui/rich_text_label.h" |
| 38 | |
| 39 | #include "../java_godot_wrapper.h" |
| 40 | #include "../os_android.h" |
| 41 | |
| 42 | void AndroidEditorGradleRunner::run_gradle(const String &p_project_path, const String &p_build_path, const String &p_output_path, const String &p_export_format, const List<String> &p_gradle_build_args, const List<String> &p_gradle_copy_args) { |
| 43 | project_path = p_project_path; |
| 44 | build_path = p_build_path; |
| 45 | output_path = p_output_path; |
| 46 | export_format = p_export_format; |
| 47 | gradle_build_args = p_gradle_build_args; |
| 48 | gradle_copy_args = p_gradle_copy_args; |
| 49 | |
| 50 | if (output_dialog == nullptr) { |
| 51 | output_label = memnew(RichTextLabel); |
| 52 | output_label->set_selection_enabled(true); |
| 53 | output_label->set_context_menu_enabled(true); |
| 54 | output_label->set_scroll_follow(true); |
| 55 | |
| 56 | output_dialog = memnew(ConfirmationDialog); |
| 57 | output_dialog->set_unparent_when_invisible(true); |
| 58 | output_dialog->set_title(TTR("Building Android Project (gradle)")); |
| 59 | output_dialog->add_child(output_label); |
| 60 | |
| 61 | output_dialog->connect("canceled", callable_mp(this, &AndroidEditorGradleRunner::_android_gradle_build_cancel)); |
| 62 | } |
| 63 | |
| 64 | output_label->clear(); |
| 65 | output_dialog->get_ok_button()->set_disabled(true); |
| 66 | |
| 67 | EditorInterface::get_singleton()->popup_dialog_centered_ratio(output_dialog); |
| 68 | |
| 69 | state = STATE_BUILDING; |
| 70 | _android_gradle_build_connect(); |
| 71 | } |
| 72 | |
| 73 | void AndroidEditorGradleRunner::_android_gradle_build_connect() { |
| 74 | _android_gradle_build_output(0, TTR("> Connecting to Gradle Build Environment...")); |
| 75 | |
| 76 | GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java(); |
| 77 | if (!godot_java->build_env_connect(callable_mp(this, &AndroidEditorGradleRunner::_android_gradle_build_build))) { |
| 78 | _android_gradle_build_failed(TTR("Unable to connect to Gradle Build Environment service")); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void AndroidEditorGradleRunner::_android_gradle_build_disconnect() { |
| 83 | GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java(); |
| 84 | godot_java->build_env_disconnect(); |
| 85 | } |
| 86 | |
| 87 | void AndroidEditorGradleRunner::_android_gradle_build_output(int p_type, const String &p_line) { |
| 88 | if (p_type == 0) { |
| 89 | print_line(p_line); |
| 90 | output_label->append_text("[color=green]" + p_line + "[/color]\n"); |
| 91 | } else if (p_type == 1) { |
| 92 | print_line(p_line); |
| 93 | output_label->add_text(p_line + "\n"); |
| 94 | } else { |
| 95 | print_error(p_line); |
| 96 | output_label->append_text("[color=red]" + p_line + "[/color]\n"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void AndroidEditorGradleRunner::_android_gradle_build_build() { |
| 101 | _android_gradle_build_output(0, TTR("> Starting Gradle build...")); |
| 102 | |
| 103 | GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java(); |
| 104 | job_id = godot_java->build_env_execute( |
| 105 | "gradle", |
| 106 | gradle_build_args, |
| 107 | project_path, |
| 108 | build_path, |
| 109 | callable_mp(this, &AndroidEditorGradleRunner::_android_gradle_build_output), |
| 110 | callable_mp(this, &AndroidEditorGradleRunner::_android_gradle_build_build_callback)); |
| 111 | if (job_id < 0) { |
| 112 | _android_gradle_build_failed(TTR("Failed to execute Gradle command")); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void AndroidEditorGradleRunner::_android_gradle_build_build_callback(int p_exit_code) { |
| 117 | job_id = -1; |
| 118 | if (p_exit_code != 0) { |
| 119 | _android_gradle_build_failed(); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | _android_gradle_build_copy(); |
| 124 | } |
| 125 | |
| 126 | void AndroidEditorGradleRunner::_android_gradle_build_copy() { |
| 127 | _android_gradle_build_output(0, TTR("> Copying Gradle artifacts...")); |
| 128 | |
| 129 | GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java(); |
| 130 | job_id = godot_java->build_env_execute( |
| 131 | "gradle", |
| 132 | gradle_copy_args, |
| 133 | project_path, |
| 134 | build_path, |
| 135 | callable_mp(this, &AndroidEditorGradleRunner::_android_gradle_build_output), |
| 136 | callable_mp(this, &AndroidEditorGradleRunner::_android_gradle_build_copy_callback)); |
| 137 | if (job_id < 0) { |
| 138 | _android_gradle_build_failed(TTR("Failed to execute Gradle command")); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void AndroidEditorGradleRunner::_android_gradle_build_copy_callback(int p_exit_code) { |
| 143 | job_id = -1; |
| 144 | if (p_exit_code != 0) { |
| 145 | _android_gradle_build_failed(); |
| 146 | } else { |
| 147 | _android_gradle_build_clean_project(true); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void AndroidEditorGradleRunner::_android_gradle_build_clean_project(bool p_was_successful) { |
| 152 | if (state != STATE_CLEANING) { |
| 153 | state = STATE_CLEANING; |
| 154 | |
| 155 | if (p_was_successful) { |
| 156 | output_dialog->hide(); |
| 157 | |
| 158 | bool prompt_apk_install = EDITOR_GET("export/android/install_exported_apk"); |
| 159 | if (prompt_apk_install && export_format == "apk") { |
| 160 | OS_Android::get_singleton()->shell_open(output_path); |
| 161 | } |
| 162 | } else { |
| 163 | output_dialog->get_ok_button()->set_disabled(false); |
| 164 | } |
| 165 | |
| 166 | GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java(); |
| 167 | godot_java->build_env_clean_project( |
| 168 | project_path, |
| 169 | build_path, |
| 170 | callable_mp(this, &AndroidEditorGradleRunner::_android_gradle_build_clean_project_callback)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void AndroidEditorGradleRunner::_android_gradle_build_clean_project_callback() { |
| 175 | // Ensure we haven't switched back to STATE_BUILDING in the meantime. |
| 176 | if (state == STATE_CLEANING) { |
| 177 | _android_gradle_build_disconnect(); |
| 178 | state = STATE_IDLE; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void AndroidEditorGradleRunner::_android_gradle_build_failed(const String &p_msg) { |
| 183 | job_id = -1; |
| 184 | |
| 185 | if (p_msg != "") { |
| 186 | _android_gradle_build_output(1, p_msg); |
| 187 | } |
| 188 | |
| 189 | _android_gradle_build_clean_project(false); |
| 190 | } |
| 191 | |
| 192 | void AndroidEditorGradleRunner::_android_gradle_build_cancel() { |
| 193 | if (job_id > 0) { |
| 194 | GodotJavaWrapper *godot_java = OS_Android::get_singleton()->get_godot_java(); |
| 195 | godot_java->build_env_cancel(job_id); |
| 196 | _android_gradle_build_clean_project(false); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | #endif // ANDROID_ENABLED |