| 1 | /**************************************************************************/ |
| 2 | /* export_plugin.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 | #include "export_plugin.h" |
| 32 | |
| 33 | #include "logo_svg.gen.h" |
| 34 | #include "run_icon_svg.gen.h" |
| 35 | |
| 36 | #include "core/config/project_settings.h" |
| 37 | #include "core/io/dir_access.h" |
| 38 | #include "core/io/file_access.h" |
| 39 | #include "core/io/image_loader.h" |
| 40 | #include "core/io/json.h" |
| 41 | #include "core/io/marshalls.h" |
| 42 | #include "core/string/translation_server.h" |
| 43 | #include "core/version.h" |
| 44 | #include "editor/editor_log.h" |
| 45 | #include "editor/editor_node.h" |
| 46 | #include "editor/editor_string_names.h" |
| 47 | #include "editor/export/export_template_manager.h" |
| 48 | #include "editor/file_system/editor_paths.h" |
| 49 | #include "editor/import/resource_importer_texture_settings.h" |
| 50 | #include "editor/settings/editor_settings.h" |
| 51 | #include "editor/themes/editor_scale.h" |
| 52 | #include "main/splash.gen.h" |
| 53 | #include "scene/resources/image_texture.h" |
| 54 | |
| 55 | #include "modules/modules_enabled.gen.h" // For mono. |
| 56 | #include "modules/svg/image_loader_svg.h" |
| 57 | |
| 58 | #ifdef MODULE_MONO_ENABLED |
| 59 | #include "modules/mono/utils/path_utils.h" |
| 60 | #endif |
| 61 | |
| 62 | #ifdef ANDROID_ENABLED |
| 63 | #include "../java_godot_wrapper.h" |
| 64 | #include "../os_android.h" |
| 65 | #include "android_editor_gradle_runner.h" |
| 66 | #endif |
| 67 | |
| 68 | static const char *ANDROID_PERMS[] = { |
| 69 | "ACCESS_CHECKIN_PROPERTIES", |
| 70 | "ACCESS_COARSE_LOCATION", |
| 71 | "ACCESS_FINE_LOCATION", |
| 72 | "ACCESS_LOCATION_EXTRA_COMMANDS", |
| 73 | "ACCESS_MEDIA_LOCATION", |
| 74 | "ACCESS_MOCK_LOCATION", |
| 75 | "ACCESS_NETWORK_STATE", |
| 76 | "ACCESS_SURFACE_FLINGER", |
| 77 | "ACCESS_WIFI_STATE", |
| 78 | "ACCOUNT_MANAGER", |
| 79 | "ADD_VOICEMAIL", |
| 80 | "AUTHENTICATE_ACCOUNTS", |
| 81 | "BATTERY_STATS", |
| 82 | "BIND_ACCESSIBILITY_SERVICE", |
| 83 | "BIND_APPWIDGET", |
| 84 | "BIND_DEVICE_ADMIN", |
| 85 | "BIND_INPUT_METHOD", |
| 86 | "BIND_NFC_SERVICE", |
| 87 | "BIND_NOTIFICATION_LISTENER_SERVICE", |
| 88 | "BIND_PRINT_SERVICE", |
| 89 | "BIND_REMOTEVIEWS", |
| 90 | "BIND_TEXT_SERVICE", |
| 91 | "BIND_VPN_SERVICE", |
| 92 | "BIND_WALLPAPER", |
| 93 | "BLUETOOTH", |
| 94 | "BLUETOOTH_ADMIN", |
| 95 | "BLUETOOTH_PRIVILEGED", |
| 96 | "BRICK", |
| 97 | "BROADCAST_PACKAGE_REMOVED", |
| 98 | "BROADCAST_SMS", |
| 99 | "BROADCAST_STICKY", |
| 100 | "BROADCAST_WAP_PUSH", |
| 101 | "CALL_PHONE", |
| 102 | "CALL_PRIVILEGED", |
| 103 | "CAMERA", |
| 104 | "CAPTURE_AUDIO_OUTPUT", |
| 105 | "CAPTURE_SECURE_VIDEO_OUTPUT", |
| 106 | "CAPTURE_VIDEO_OUTPUT", |
| 107 | "CHANGE_COMPONENT_ENABLED_STATE", |
| 108 | "CHANGE_CONFIGURATION", |
| 109 | "CHANGE_NETWORK_STATE", |
| 110 | "CHANGE_WIFI_MULTICAST_STATE", |
| 111 | "CHANGE_WIFI_STATE", |
| 112 | "CLEAR_APP_CACHE", |
| 113 | "CLEAR_APP_USER_DATA", |
| 114 | "CONTROL_LOCATION_UPDATES", |
| 115 | "DELETE_CACHE_FILES", |
| 116 | "DELETE_PACKAGES", |
| 117 | "DEVICE_POWER", |
| 118 | "DIAGNOSTIC", |
| 119 | "DISABLE_KEYGUARD", |
| 120 | "DUMP", |
| 121 | "EXPAND_STATUS_BAR", |
| 122 | "FACTORY_TEST", |
| 123 | "FLASHLIGHT", |
| 124 | "FORCE_BACK", |
| 125 | "GET_ACCOUNTS", |
| 126 | "GET_PACKAGE_SIZE", |
| 127 | "GET_TASKS", |
| 128 | "GET_TOP_ACTIVITY_INFO", |
| 129 | "GLOBAL_SEARCH", |
| 130 | "HARDWARE_TEST", |
| 131 | "INJECT_EVENTS", |
| 132 | "INSTALL_LOCATION_PROVIDER", |
| 133 | "INSTALL_PACKAGES", |
| 134 | "INSTALL_SHORTCUT", |
| 135 | "INTERNAL_SYSTEM_WINDOW", |
| 136 | "INTERNET", |
| 137 | "KILL_BACKGROUND_PROCESSES", |
| 138 | "LOCATION_HARDWARE", |
| 139 | "MANAGE_ACCOUNTS", |
| 140 | "MANAGE_APP_TOKENS", |
| 141 | "MANAGE_DOCUMENTS", |
| 142 | "MANAGE_EXTERNAL_STORAGE", |
| 143 | "MANAGE_MEDIA", |
| 144 | "MASTER_CLEAR", |
| 145 | "MEDIA_CONTENT_CONTROL", |
| 146 | "MODIFY_AUDIO_SETTINGS", |
| 147 | "MODIFY_PHONE_STATE", |
| 148 | "MOUNT_FORMAT_FILESYSTEMS", |
| 149 | "MOUNT_UNMOUNT_FILESYSTEMS", |
| 150 | "NFC", |
| 151 | "PERSISTENT_ACTIVITY", |
| 152 | "POST_NOTIFICATIONS", |
| 153 | "PROCESS_OUTGOING_CALLS", |
| 154 | "READ_CALENDAR", |
| 155 | "READ_CALL_LOG", |
| 156 | "READ_CONTACTS", |
| 157 | "READ_EXTERNAL_STORAGE", |
| 158 | "READ_FRAME_BUFFER", |
| 159 | "READ_HISTORY_BOOKMARKS", |
| 160 | "READ_INPUT_STATE", |
| 161 | "READ_LOGS", |
| 162 | "READ_MEDIA_AUDIO", |
| 163 | "READ_MEDIA_IMAGES", |
| 164 | "READ_MEDIA_VIDEO", |
| 165 | "READ_MEDIA_VISUAL_USER_SELECTED", |
| 166 | "READ_PHONE_STATE", |
| 167 | "READ_PROFILE", |
| 168 | "READ_SMS", |
| 169 | "READ_SOCIAL_STREAM", |
| 170 | "READ_SYNC_SETTINGS", |
| 171 | "READ_SYNC_STATS", |
| 172 | "READ_USER_DICTIONARY", |
| 173 | "REBOOT", |
| 174 | "RECEIVE_BOOT_COMPLETED", |
| 175 | "RECEIVE_MMS", |
| 176 | "RECEIVE_SMS", |
| 177 | "RECEIVE_WAP_PUSH", |
| 178 | "RECORD_AUDIO", |
| 179 | "REORDER_TASKS", |
| 180 | "RESTART_PACKAGES", |
| 181 | "SEND_RESPOND_VIA_MESSAGE", |
| 182 | "SEND_SMS", |
| 183 | "SET_ACTIVITY_WATCHER", |
| 184 | "SET_ALARM", |
| 185 | "SET_ALWAYS_FINISH", |
| 186 | "SET_ANIMATION_SCALE", |
| 187 | "SET_DEBUG_APP", |
| 188 | "SET_ORIENTATION", |
| 189 | "SET_POINTER_SPEED", |
| 190 | "SET_PREFERRED_APPLICATIONS", |
| 191 | "SET_PROCESS_LIMIT", |
| 192 | "SET_TIME", |
| 193 | "SET_TIME_ZONE", |
| 194 | "SET_WALLPAPER", |
| 195 | "SET_WALLPAPER_HINTS", |
| 196 | "SIGNAL_PERSISTENT_PROCESSES", |
| 197 | "STATUS_BAR", |
| 198 | "SUBSCRIBED_FEEDS_READ", |
| 199 | "SUBSCRIBED_FEEDS_WRITE", |
| 200 | "SYSTEM_ALERT_WINDOW", |
| 201 | "TRANSMIT_IR", |
| 202 | "UNINSTALL_SHORTCUT", |
| 203 | "UPDATE_DEVICE_STATS", |
| 204 | "USE_CREDENTIALS", |
| 205 | "USE_SIP", |
| 206 | "VIBRATE", |
| 207 | "WAKE_LOCK", |
| 208 | "WRITE_APN_SETTINGS", |
| 209 | "WRITE_CALENDAR", |
| 210 | "WRITE_CALL_LOG", |
| 211 | "WRITE_CONTACTS", |
| 212 | "WRITE_EXTERNAL_STORAGE", |
| 213 | "WRITE_GSERVICES", |
| 214 | "WRITE_HISTORY_BOOKMARKS", |
| 215 | "WRITE_PROFILE", |
| 216 | "WRITE_SECURE_SETTINGS", |
| 217 | "WRITE_SETTINGS", |
| 218 | "WRITE_SMS", |
| 219 | "WRITE_SOCIAL_STREAM", |
| 220 | "WRITE_SYNC_SETTINGS", |
| 221 | "WRITE_USER_DICTIONARY", |
| 222 | nullptr |
| 223 | }; |
| 224 | |
| 225 | 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."; |
| 226 | |
| 227 | static const char *GDEXTENSION_LIBS_PATH = "libs/gdextensionlibs.json"; |
| 228 | |
| 229 | // This template string must be in sync with the content of 'platform/android/java/lib/src/main/java/res/mipmap-anydpi-v26/icon.xml'. |
| 230 | static const String ICON_XML_TEMPLATE = |
| 231 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 232 | "<adaptive-icon xmlns:android=\"http://schemas.android.com/apk/res/android\">\n" |
| 233 | " <background android:drawable=\"@mipmap/icon_background\"/>\n" |
| 234 | " <foreground android:drawable=\"@mipmap/icon_foreground\"/>\n" |
| 235 | "%s" // Placeholder for the optional monochrome tag. |
| 236 | "</adaptive-icon>"; |
| 237 | |
| 238 | static const String ICON_XML_PATH = "res/mipmap-anydpi-v26/icon.xml"; |
| 239 | static const String THEMED_ICON_XML_PATH = "res/mipmap-anydpi-v26/themed_icon.xml"; |
| 240 | |
| 241 | static const int ICON_DENSITIES_COUNT = 6; |
| 242 | static const char *LAUNCHER_ICON_OPTION = PNAME("launcher_icons/main_192x192"); |
| 243 | static const char *LAUNCHER_ADAPTIVE_ICON_FOREGROUND_OPTION = PNAME("launcher_icons/adaptive_foreground_432x432"); |
| 244 | static const char *LAUNCHER_ADAPTIVE_ICON_BACKGROUND_OPTION = PNAME("launcher_icons/adaptive_background_432x432"); |
| 245 | static const char *LAUNCHER_ADAPTIVE_ICON_MONOCHROME_OPTION = PNAME("launcher_icons/adaptive_monochrome_432x432"); |
| 246 | |
| 247 | static const LauncherIcon LAUNCHER_ICONS[ICON_DENSITIES_COUNT] = { |
| 248 | { "res/mipmap-xxxhdpi-v4/icon.webp", 192 }, |
| 249 | { "res/mipmap-xxhdpi-v4/icon.webp", 144 }, |
| 250 | { "res/mipmap-xhdpi-v4/icon.webp", 96 }, |
| 251 | { "res/mipmap-hdpi-v4/icon.webp", 72 }, |
| 252 | { "res/mipmap-mdpi-v4/icon.webp", 48 }, |
| 253 | { "res/mipmap/icon.webp", 192 } |
| 254 | }; |
| 255 | |
| 256 | static const LauncherIcon LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[ICON_DENSITIES_COUNT] = { |
| 257 | { "res/mipmap-xxxhdpi-v4/icon_foreground.webp", 432 }, |
| 258 | { "res/mipmap-xxhdpi-v4/icon_foreground.webp", 324 }, |
| 259 | { "res/mipmap-xhdpi-v4/icon_foreground.webp", 216 }, |
| 260 | { "res/mipmap-hdpi-v4/icon_foreground.webp", 162 }, |
| 261 | { "res/mipmap-mdpi-v4/icon_foreground.webp", 108 }, |
| 262 | { "res/mipmap/icon_foreground.webp", 432 } |
| 263 | }; |
| 264 | |
| 265 | static const LauncherIcon LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[ICON_DENSITIES_COUNT] = { |
| 266 | { "res/mipmap-xxxhdpi-v4/icon_background.webp", 432 }, |
| 267 | { "res/mipmap-xxhdpi-v4/icon_background.webp", 324 }, |
| 268 | { "res/mipmap-xhdpi-v4/icon_background.webp", 216 }, |
| 269 | { "res/mipmap-hdpi-v4/icon_background.webp", 162 }, |
| 270 | { "res/mipmap-mdpi-v4/icon_background.webp", 108 }, |
| 271 | { "res/mipmap/icon_background.webp", 432 } |
| 272 | }; |
| 273 | |
| 274 | static const LauncherIcon LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[ICON_DENSITIES_COUNT] = { |
| 275 | { "res/mipmap-xxxhdpi-v4/icon_monochrome.webp", 432 }, |
| 276 | { "res/mipmap-xxhdpi-v4/icon_monochrome.webp", 324 }, |
| 277 | { "res/mipmap-xhdpi-v4/icon_monochrome.webp", 216 }, |
| 278 | { "res/mipmap-hdpi-v4/icon_monochrome.webp", 162 }, |
| 279 | { "res/mipmap-mdpi-v4/icon_monochrome.webp", 108 }, |
| 280 | { "res/mipmap/icon_monochrome.webp", 432 } |
| 281 | }; |
| 282 | |
| 283 | static const int EXPORT_FORMAT_APK = 0; |
| 284 | static const int EXPORT_FORMAT_AAB = 1; |
| 285 | |
| 286 | static const char *APK_ASSETS_DIRECTORY = "src/main/assets"; |
| 287 | static const char *AAB_ASSETS_DIRECTORY = "assetPackInstallTime/src/main/assets"; |
| 288 | |
| 289 | static const int DEFAULT_MIN_SDK_VERSION = 24; // Should match the value in 'platform/android/java/app/config.gradle#minSdk' |
| 290 | static const int DEFAULT_TARGET_SDK_VERSION = 36; // Should match the value in 'platform/android/java/app/config.gradle#targetSdk' |
| 291 | |
| 292 | #ifndef ANDROID_ENABLED |
| 293 | void EditorExportPlatformAndroid::_check_for_changes_poll_thread(void *ud) { |
| 294 | if (!EditorSettings::get_singleton()) { |
| 295 | // Some methods called here query editor settings, so we need it to be ready first. |
| 296 | // If it's not ready yet, just wait for the next iteration. |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | EditorExportPlatformAndroid *ea = static_cast<EditorExportPlatformAndroid *>(ud); |
| 301 | |
| 302 | while (!ea->quit_request.is_set()) { |
| 303 | #ifndef DISABLE_DEPRECATED |
| 304 | // Check for android plugins updates |
| 305 | { |
| 306 | // Nothing to do if we already know the plugins have changed. |
| 307 | if (!ea->android_plugins_changed.is_set()) { |
| 308 | Vector<PluginConfigAndroid> loaded_plugins = get_plugins(); |
| 309 | |
| 310 | MutexLock lock(ea->android_plugins_lock); |
| 311 | |
| 312 | if (ea->android_plugins.size() != loaded_plugins.size()) { |
| 313 | ea->android_plugins_changed.set(); |
| 314 | } else { |
| 315 | for (int i = 0; i < ea->android_plugins.size(); i++) { |
| 316 | if (ea->android_plugins[i].name != loaded_plugins[i].name) { |
| 317 | ea->android_plugins_changed.set(); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (ea->android_plugins_changed.is_set()) { |
| 324 | ea->android_plugins = loaded_plugins; |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | #endif // DISABLE_DEPRECATED |
| 329 | |
| 330 | // Check for devices updates |
| 331 | String adb = get_adb_path(); |
| 332 | // adb.exe was locking the editor_doc_cache file on startup. Adding a check for is_editor_ready provides just enough time |
| 333 | // to regenerate the doc cache. |
| 334 | if (ea->has_runnable_preset.is_set() && FileAccess::exists(adb) && EditorNode::get_singleton()->is_editor_ready()) { |
| 335 | String devices; |
| 336 | List<String> args; |
| 337 | args.push_back("devices"); |
| 338 | int ec; |
| 339 | OS::get_singleton()->execute(adb, args, &devices, &ec); |
| 340 | |
| 341 | Vector<String> ds = devices.split("\n"); |
| 342 | Vector<String> ldevices; |
| 343 | for (int i = 1; i < ds.size(); i++) { |
| 344 | String d = ds[i]; |
| 345 | int dpos = d.find("device"); |
| 346 | if (dpos == -1) { |
| 347 | continue; |
| 348 | } |
| 349 | d = d.substr(0, dpos).strip_edges(); |
| 350 | ldevices.push_back(d); |
| 351 | } |
| 352 | |
| 353 | MutexLock lock(ea->device_lock); |
| 354 | |
| 355 | bool different = false; |
| 356 | |
| 357 | if (ea->devices.size() != ldevices.size()) { |
| 358 | different = true; |
| 359 | } else { |
| 360 | for (int i = 0; i < ea->devices.size(); i++) { |
| 361 | if (ea->devices[i].id != ldevices[i]) { |
| 362 | different = true; |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (different) { |
| 369 | Vector<Device> ndevices; |
| 370 | |
| 371 | for (int i = 0; i < ldevices.size(); i++) { |
| 372 | Device d; |
| 373 | d.id = ldevices[i]; |
| 374 | for (int j = 0; j < ea->devices.size(); j++) { |
| 375 | if (ea->devices[j].id == ldevices[i]) { |
| 376 | d.description = ea->devices[j].description; |
| 377 | d.name = ea->devices[j].name; |
| 378 | d.api_level = ea->devices[j].api_level; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (d.description.is_empty()) { |
| 383 | //in the oven, request! |
| 384 | args.clear(); |
| 385 | args.push_back("-s"); |
| 386 | args.push_back(d.id); |
| 387 | args.push_back("shell"); |
| 388 | args.push_back("getprop"); |
| 389 | int ec2; |
| 390 | String dp; |
| 391 | |
| 392 | OS::get_singleton()->execute(adb, args, &dp, &ec2); |
| 393 | |
| 394 | Vector<String> props = dp.split("\n"); |
| 395 | String vendor; |
| 396 | String device; |
| 397 | d.description = "Device ID: " + d.id + "\n"; |
| 398 | d.api_level = 0; |
| 399 | for (int j = 0; j < props.size(); j++) { |
| 400 | // got information by `shell cat /system/build.prop` before and its format is "property=value" |
| 401 | // it's now changed to use `shell getporp` because of permission issue with Android 8.0 and above |
| 402 | // its format is "[property]: [value]" so changed it as like build.prop |
| 403 | String p = props[j]; |
| 404 | p = p.replace("]: ", "="); |
| 405 | p = p.remove_chars("[]"); |
| 406 | |
| 407 | if (p.begins_with("ro.product.model=")) { |
| 408 | device = p.get_slicec('=', 1).strip_edges(); |
| 409 | } else if (p.begins_with("ro.product.brand=")) { |
| 410 | vendor = p.get_slicec('=', 1).strip_edges().capitalize(); |
| 411 | } else if (p.begins_with("ro.build.display.id=")) { |
| 412 | d.description += "Build: " + p.get_slicec('=', 1).strip_edges() + "\n"; |
| 413 | } else if (p.begins_with("ro.build.version.release=")) { |
| 414 | d.description += "Release: " + p.get_slicec('=', 1).strip_edges() + "\n"; |
| 415 | } else if (p.begins_with("ro.build.version.sdk=")) { |
| 416 | d.api_level = p.get_slicec('=', 1).to_int(); |
| 417 | } else if (p.begins_with("ro.product.cpu.abi=")) { |
| 418 | d.architecture = p.get_slicec('=', 1).strip_edges(); |
| 419 | d.description += "CPU: " + d.architecture + "\n"; |
| 420 | } else if (p.begins_with("ro.product.manufacturer=")) { |
| 421 | d.description += "Manufacturer: " + p.get_slicec('=', 1).strip_edges() + "\n"; |
| 422 | } else if (p.begins_with("ro.board.platform=")) { |
| 423 | d.description += "Chipset: " + p.get_slicec('=', 1).strip_edges() + "\n"; |
| 424 | } else if (p.begins_with("ro.opengles.version=")) { |
| 425 | uint32_t opengl = p.get_slicec('=', 1).to_int(); |
| 426 | d.description += "OpenGL: " + itos(opengl >> 16) + "." + itos((opengl >> 8) & 0xFF) + "." + itos((opengl) & 0xFF) + "\n"; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | d.name = vendor + " " + device; |
| 431 | if (device.is_empty()) { |
| 432 | continue; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | ndevices.push_back(d); |
| 437 | } |
| 438 | |
| 439 | ea->devices = ndevices; |
| 440 | ea->devices_changed.set(); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | uint64_t sleep = 200; |
| 445 | uint64_t wait = 3000000; |
| 446 | uint64_t time = OS::get_singleton()->get_ticks_usec(); |
| 447 | while (OS::get_singleton()->get_ticks_usec() - time < wait) { |
| 448 | OS::get_singleton()->delay_usec(1000 * sleep); |
| 449 | if (ea->quit_request.is_set()) { |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | if (ea->has_runnable_preset.is_set() && EDITOR_GET("export/android/shutdown_adb_on_exit")) { |
| 456 | String adb = get_adb_path(); |
| 457 | if (!FileAccess::exists(adb)) { |
| 458 | return; //adb not configured |
| 459 | } |
| 460 | |
| 461 | List<String> args; |
| 462 | args.push_back("kill-server"); |
| 463 | OS::get_singleton()->execute(adb, args); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | void EditorExportPlatformAndroid::_update_preset_status() { |
| 468 | const int preset_count = EditorExport::get_singleton()->get_export_preset_count(); |
| 469 | bool has_runnable = false; |
| 470 | |
| 471 | for (int i = 0; i < preset_count; i++) { |
| 472 | const Ref<EditorExportPreset> &preset = EditorExport::get_singleton()->get_export_preset(i); |
| 473 | if (preset->get_platform() == this && preset->is_runnable()) { |
| 474 | has_runnable = true; |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (has_runnable) { |
| 480 | has_runnable_preset.set(); |
| 481 | } else { |
| 482 | has_runnable_preset.clear(); |
| 483 | } |
| 484 | devices_changed.set(); |
| 485 | } |
| 486 | #endif |
| 487 | |
| 488 | String EditorExportPlatformAndroid::get_project_name(const Ref<EditorExportPreset> &p_preset, const String &p_name) const { |
| 489 | String aname; |
| 490 | if (!p_name.is_empty()) { |
| 491 | aname = p_name; |
| 492 | } else { |
| 493 | aname = get_project_setting(p_preset, "application/config/name"); |
| 494 | } |
| 495 | |
| 496 | if (aname.is_empty()) { |
| 497 | aname = GODOT_VERSION_NAME; |
| 498 | } |
| 499 | |
| 500 | return aname; |
| 501 | } |
| 502 | |
| 503 | String EditorExportPlatformAndroid::get_package_name(const Ref<EditorExportPreset> &p_preset, const String &p_package) const { |
| 504 | String pname = p_package; |
| 505 | String name = get_valid_basename(p_preset); |
| 506 | pname = pname.replace("$genname", name); |
| 507 | return pname; |
| 508 | } |
| 509 | |
| 510 | // Returns the project name without invalid characters |
| 511 | // or the "noname" string if all characters are invalid. |
| 512 | String EditorExportPlatformAndroid::get_valid_basename(const Ref<EditorExportPreset> &p_preset) const { |
| 513 | String basename = get_project_setting(p_preset, "application/config/name"); |
| 514 | basename = basename.to_lower(); |
| 515 | |
| 516 | String name; |
| 517 | bool first = true; |
| 518 | for (int i = 0; i < basename.length(); i++) { |
| 519 | char32_t c = basename[i]; |
| 520 | if (is_digit(c) && first) { |
| 521 | continue; |
| 522 | } |
| 523 | if (is_ascii_identifier_char(c)) { |
| 524 | name += String::chr(c); |
| 525 | first = false; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if (name.is_empty()) { |
| 530 | name = "noname"; |
| 531 | } |
| 532 | |
| 533 | return name; |
| 534 | } |
| 535 | |
| 536 | String EditorExportPlatformAndroid::get_assets_directory(const Ref<EditorExportPreset> &p_preset, int p_export_format) const { |
| 537 | String gradle_build_directory = ExportTemplateManager::get_android_build_directory(p_preset); |
| 538 | return gradle_build_directory.path_join(p_export_format == EXPORT_FORMAT_AAB ? AAB_ASSETS_DIRECTORY : APK_ASSETS_DIRECTORY); |
| 539 | } |
| 540 | |
| 541 | bool EditorExportPlatformAndroid::is_package_name_valid(const Ref<EditorExportPreset> &p_preset, const String &p_package, String *r_error) const { |
| 542 | String pname = get_package_name(p_preset, p_package); |
| 543 | |
| 544 | if (pname.length() == 0) { |
| 545 | if (r_error) { |
| 546 | *r_error = TTR("Package name is missing."); |
| 547 | } |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | int segments = 0; |
| 552 | bool first = true; |
| 553 | for (int i = 0; i < pname.length(); i++) { |
| 554 | char32_t c = pname[i]; |
| 555 | if (first && c == '.') { |
| 556 | if (r_error) { |
| 557 | *r_error = TTR("Package segments must be of non-zero length."); |
| 558 | } |
| 559 | return false; |
| 560 | } |
| 561 | if (c == '.') { |
| 562 | segments++; |
| 563 | first = true; |
| 564 | continue; |
| 565 | } |
| 566 | if (!is_ascii_identifier_char(c)) { |
| 567 | if (r_error) { |
| 568 | *r_error = vformat(TTR("The character '%s' is not allowed in Android application package names."), String::chr(c)); |
| 569 | } |
| 570 | return false; |
| 571 | } |
| 572 | if (first && is_digit(c)) { |
| 573 | if (r_error) { |
| 574 | *r_error = TTR("A digit cannot be the first character in a package segment."); |
| 575 | } |
| 576 | return false; |
| 577 | } |
| 578 | if (first && is_underscore(c)) { |
| 579 | if (r_error) { |
| 580 | *r_error = vformat(TTR("The character '%s' cannot be the first character in a package segment."), String::chr(c)); |
| 581 | } |
| 582 | return false; |
| 583 | } |
| 584 | first = false; |
| 585 | } |
| 586 | |
| 587 | if (segments == 0) { |
| 588 | if (r_error) { |
| 589 | *r_error = TTR("The package must have at least one '.' separator."); |
| 590 | } |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | if (first) { |
| 595 | if (r_error) { |
| 596 | *r_error = TTR("Package segments must be of non-zero length."); |
| 597 | } |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | return true; |
| 602 | } |
| 603 | |
| 604 | bool EditorExportPlatformAndroid::is_project_name_valid(const Ref<EditorExportPreset> &p_preset) const { |
| 605 | // Get the original project name and convert to lowercase. |
| 606 | String basename = get_project_setting(p_preset, "application/config/name"); |
| 607 | basename = basename.to_lower(); |
| 608 | // Check if there are invalid characters. |
| 609 | if (basename != get_valid_basename(p_preset)) { |
| 610 | return false; |
| 611 | } |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | bool EditorExportPlatformAndroid::_should_compress_asset(const String &p_path, const Vector<uint8_t> &p_data) { |
| 616 | /* |
| 617 | * By not compressing files with little or no benefit in doing so, |
| 618 | * a performance gain is expected at runtime. Moreover, if the APK is |
| 619 | * zip-aligned, assets stored as they are can be efficiently read by |
| 620 | * Android by memory-mapping them. |
| 621 | */ |
| 622 | |
| 623 | // -- Unconditional uncompress to mimic AAPT plus some other |
| 624 | |
| 625 | static const char *unconditional_compress_ext[] = { |
| 626 | // From https://github.com/android/platform_frameworks_base/blob/master/tools/aapt/Package.cpp |
| 627 | // These formats are already compressed, or don't compress well: |
| 628 | ".jpg", ".jpeg", ".png", ".gif", |
| 629 | ".wav", ".mp2", ".mp3", ".ogg", ".aac", |
| 630 | ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", |
| 631 | ".rtttl", ".imy", ".xmf", ".mp4", ".m4a", |
| 632 | ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2", |
| 633 | ".amr", ".awb", ".wma", ".wmv", |
| 634 | // Godot-specific: |
| 635 | ".webp", // Same reasoning as .png |
| 636 | ".cfb", // Don't let small config files slow-down startup |
| 637 | ".scn", // Binary scenes are usually already compressed |
| 638 | ".ctex", // Streamable textures are usually already compressed |
| 639 | ".pck", // Pack. |
| 640 | // Trailer for easier processing |
| 641 | nullptr |
| 642 | }; |
| 643 | |
| 644 | for (const char **ext = unconditional_compress_ext; *ext; ++ext) { |
| 645 | if (p_path.to_lower().ends_with(String(*ext))) { |
| 646 | return false; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // -- Compressed resource? |
| 651 | |
| 652 | if (p_data.size() >= 4 && p_data[0] == 'R' && p_data[1] == 'S' && p_data[2] == 'C' && p_data[3] == 'C') { |
| 653 | // Already compressed |
| 654 | return false; |
| 655 | } |
| 656 | |
| 657 | // --- TODO: Decide on texture resources according to their image compression setting |
| 658 | |
| 659 | return true; |
| 660 | } |
| 661 | |
| 662 | zip_fileinfo EditorExportPlatformAndroid::get_zip_fileinfo() { |
| 663 | OS::DateTime dt = OS::get_singleton()->get_datetime(); |
| 664 | |
| 665 | zip_fileinfo zipfi; |
| 666 | zipfi.tmz_date.tm_year = dt.year; |
| 667 | zipfi.tmz_date.tm_mon = dt.month - 1; // tm_mon is zero indexed |
| 668 | zipfi.tmz_date.tm_mday = dt.day; |
| 669 | zipfi.tmz_date.tm_hour = dt.hour; |
| 670 | zipfi.tmz_date.tm_min = dt.minute; |
| 671 | zipfi.tmz_date.tm_sec = dt.second; |
| 672 | zipfi.dosDate = 0; |
| 673 | zipfi.external_fa = 0; |
| 674 | zipfi.internal_fa = 0; |
| 675 | |
| 676 | return zipfi; |
| 677 | } |
| 678 | |
| 679 | Vector<EditorExportPlatformAndroid::ABI> EditorExportPlatformAndroid::get_abis() { |
| 680 | // Should have the same order and size as get_archs. |
| 681 | Vector<ABI> abis; |
| 682 | abis.push_back(ABI("armeabi-v7a", "arm32")); |
| 683 | abis.push_back(ABI("arm64-v8a", "arm64")); |
| 684 | abis.push_back(ABI("x86", "x86_32")); |
| 685 | abis.push_back(ABI("x86_64", "x86_64")); |
| 686 | return abis; |
| 687 | } |
| 688 | |
| 689 | #ifndef DISABLE_DEPRECATED |
| 690 | /// List the gdap files in the directory specified by the p_path parameter. |
| 691 | Vector<String> EditorExportPlatformAndroid::list_gdap_files(const String &p_path) { |
| 692 | Vector<String> dir_files; |
| 693 | Ref<DirAccess> da = DirAccess::open(p_path); |
| 694 | if (da.is_valid()) { |
| 695 | da->list_dir_begin(); |
| 696 | while (true) { |
| 697 | String file = da->get_next(); |
| 698 | if (file.is_empty()) { |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | if (da->current_is_dir() || da->current_is_hidden()) { |
| 703 | continue; |
| 704 | } |
| 705 | |
| 706 | if (file.ends_with(PluginConfigAndroid::PLUGIN_CONFIG_EXT)) { |
| 707 | dir_files.push_back(file); |
| 708 | } |
| 709 | } |
| 710 | da->list_dir_end(); |
| 711 | } |
| 712 | |
| 713 | return dir_files; |
| 714 | } |
| 715 | |
| 716 | Vector<PluginConfigAndroid> EditorExportPlatformAndroid::get_plugins() { |
| 717 | Vector<PluginConfigAndroid> loaded_plugins; |
| 718 | |
| 719 | String plugins_dir = ProjectSettings::get_singleton()->get_resource_path().path_join("android/plugins"); |
| 720 | |
| 721 | // Add the prebuilt plugins |
| 722 | loaded_plugins.append_array(PluginConfigAndroid::get_prebuilt_plugins(plugins_dir)); |
| 723 | |
| 724 | if (DirAccess::exists(plugins_dir)) { |
| 725 | Vector<String> plugins_filenames = list_gdap_files(plugins_dir); |
| 726 | |
| 727 | if (!plugins_filenames.is_empty()) { |
| 728 | Ref<ConfigFile> config_file; |
| 729 | config_file.instantiate(); |
| 730 | for (int i = 0; i < plugins_filenames.size(); i++) { |
| 731 | PluginConfigAndroid config = PluginConfigAndroid::load_plugin_config(config_file, plugins_dir.path_join(plugins_filenames[i])); |
| 732 | if (config.valid_config) { |
| 733 | loaded_plugins.push_back(config); |
| 734 | } else { |
| 735 | print_error("Invalid plugin config file " + plugins_filenames[i]); |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | return loaded_plugins; |
| 742 | } |
| 743 | |
| 744 | Vector<PluginConfigAndroid> EditorExportPlatformAndroid::get_enabled_plugins(const Ref<EditorExportPreset> &p_presets) { |
| 745 | Vector<PluginConfigAndroid> enabled_plugins; |
| 746 | Vector<PluginConfigAndroid> all_plugins = get_plugins(); |
| 747 | for (int i = 0; i < all_plugins.size(); i++) { |
| 748 | PluginConfigAndroid plugin = all_plugins[i]; |
| 749 | bool enabled = p_presets->get("plugins/" + plugin.name); |
| 750 | if (enabled) { |
| 751 | enabled_plugins.push_back(plugin); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | return enabled_plugins; |
| 756 | } |
| 757 | #endif // DISABLE_DEPRECATED |
| 758 | |
| 759 | Error EditorExportPlatformAndroid::store_in_apk(APKExportData *ed, const String &p_path, const Vector<uint8_t> &p_data, int compression_method) { |
| 760 | zip_fileinfo zipfi = get_zip_fileinfo(); |
| 761 | zipOpenNewFileInZip(ed->apk, |
| 762 | p_path.utf8().get_data(), |
| 763 | &zipfi, |
| 764 | nullptr, |
| 765 | 0, |
| 766 | nullptr, |
| 767 | 0, |
| 768 | nullptr, |
| 769 | compression_method, |
| 770 | Z_DEFAULT_COMPRESSION); |
| 771 | |
| 772 | zipWriteInFileInZip(ed->apk, p_data.ptr(), p_data.size()); |
| 773 | zipCloseFileInZip(ed->apk); |
| 774 | |
| 775 | return OK; |
| 776 | } |
| 777 | |
| 778 | Error EditorExportPlatformAndroid::save_apk_so(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const SharedObject &p_so) { |
| 779 | if (!p_so.path.get_file().begins_with("lib")) { |
| 780 | String err = "Android .so file names must start with \"lib\", but got: " + p_so.path; |
| 781 | ERR_PRINT(err); |
| 782 | return FAILED; |
| 783 | } |
| 784 | APKExportData *ed = static_cast<APKExportData *>(p_userdata); |
| 785 | Vector<ABI> abis = get_abis(); |
| 786 | bool exported = false; |
| 787 | for (int i = 0; i < p_so.tags.size(); ++i) { |
| 788 | // shared objects can be fat (compatible with multiple ABIs) |
| 789 | int abi_index = -1; |
| 790 | for (int j = 0; j < abis.size(); ++j) { |
| 791 | if (abis[j].abi == p_so.tags[i] || abis[j].arch == p_so.tags[i]) { |
| 792 | abi_index = j; |
| 793 | break; |
| 794 | } |
| 795 | } |
| 796 | if (abi_index != -1) { |
| 797 | exported = true; |
| 798 | String abi = abis[abi_index].abi; |
| 799 | String dst_path = String("lib").path_join(abi).path_join(p_so.path.get_file()); |
| 800 | Vector<uint8_t> array = FileAccess::get_file_as_bytes(p_so.path); |
| 801 | Error store_err = store_in_apk(ed, dst_path, array, Z_NO_COMPRESSION); |
| 802 | ERR_FAIL_COND_V_MSG(store_err, store_err, "Cannot store in apk file '" + dst_path + "'."); |
| 803 | } |
| 804 | } |
| 805 | if (!exported) { |
| 806 | ERR_PRINT("Cannot determine architecture for library \"" + p_so.path + "\". One of the supported architectures must be used as a tag: " + join_abis(abis, " ", true)); |
| 807 | return FAILED; |
| 808 | } |
| 809 | return OK; |
| 810 | } |
| 811 | |
| 812 | Error EditorExportPlatformAndroid::save_apk_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool p_delta) { |
| 813 | APKExportData *ed = static_cast<APKExportData *>(p_userdata); |
| 814 | |
| 815 | const String simplified_path = simplify_path(p_path); |
| 816 | |
| 817 | Vector<uint8_t> enc_data; |
| 818 | EditorExportPlatform::SavedData sd; |
| 819 | Error err = _store_temp_file(simplified_path, p_data, p_enc_in_filters, p_enc_ex_filters, p_key, p_seed, p_delta, enc_data, sd); |
| 820 | if (err != OK) { |
| 821 | return err; |
| 822 | } |
| 823 | |
| 824 | const String dst_path = String("assets/") + simplified_path.trim_prefix("res://"); |
| 825 | print_verbose("Saving project files from " + simplified_path + " into " + dst_path); |
| 826 | store_in_apk(ed, dst_path, enc_data, _should_compress_asset(simplified_path, enc_data) ? Z_DEFLATED : 0); |
| 827 | |
| 828 | ed->pd.file_ofs.push_back(sd); |
| 829 | |
| 830 | return OK; |
| 831 | } |
| 832 | |
| 833 | Error EditorExportPlatformAndroid::ignore_apk_file(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const String &p_path, const Vector<uint8_t> &p_data, int p_file, int p_total, const Vector<String> &p_enc_in_filters, const Vector<String> &p_enc_ex_filters, const Vector<uint8_t> &p_key, uint64_t p_seed, bool p_delta) { |
| 834 | return OK; |
| 835 | } |
| 836 | |
| 837 | Error EditorExportPlatformAndroid::copy_gradle_so(const Ref<EditorExportPreset> &p_preset, void *p_userdata, const SharedObject &p_so) { |
| 838 | ERR_FAIL_COND_V_MSG(!p_so.path.get_file().begins_with("lib"), FAILED, |
| 839 | "Android .so file names must start with \"lib\", but got: " + p_so.path); |
| 840 | Vector<ABI> abis = get_abis(); |
| 841 | CustomExportData *export_data = static_cast<CustomExportData *>(p_userdata); |
| 842 | bool exported = false; |
| 843 | for (int i = 0; i < p_so.tags.size(); ++i) { |
| 844 | int abi_index = -1; |
| 845 | for (int j = 0; j < abis.size(); ++j) { |
| 846 | if (abis[j].abi == p_so.tags[i] || abis[j].arch == p_so.tags[i]) { |
| 847 | abi_index = j; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | if (abi_index != -1) { |
| 852 | exported = true; |
| 853 | String type = export_data->debug ? "debug" : "release"; |
| 854 | String abi = abis[abi_index].abi; |
| 855 | String filename = p_so.path.get_file(); |
| 856 | String dst_path = export_data->libs_directory.path_join(type).path_join(abi).path_join(filename); |
| 857 | Vector<uint8_t> data = FileAccess::get_file_as_bytes(p_so.path); |
| 858 | print_verbose("Copying .so file from " + p_so.path + " to " + dst_path); |
| 859 | Error err = store_file_at_path(dst_path, data); |
| 860 | ERR_FAIL_COND_V_MSG(err, err, "Failed to copy .so file from " + p_so.path + " to " + dst_path); |
| 861 | export_data->libs.push_back(dst_path); |
| 862 | } |
| 863 | } |
| 864 | ERR_FAIL_COND_V_MSG(!exported, FAILED, |
| 865 | "Cannot determine architecture for library \"" + p_so.path + "\". One of the supported architectures must be used as a tag:" + join_abis(abis, " ", true)); |
| 866 | return OK; |
| 867 | } |
| 868 | |
| 869 | bool EditorExportPlatformAndroid::_has_read_write_storage_permission(const Vector<String> &p_permissions) { |
| 870 | return p_permissions.has("android.permission.READ_EXTERNAL_STORAGE") || p_permissions.has("android.permission.WRITE_EXTERNAL_STORAGE"); |
| 871 | } |
| 872 | |
| 873 | bool EditorExportPlatformAndroid::_has_manage_external_storage_permission(const Vector<String> &p_permissions) { |
| 874 | return p_permissions.has("android.permission.MANAGE_EXTERNAL_STORAGE"); |
| 875 | } |
| 876 | |
| 877 | bool EditorExportPlatformAndroid::_uses_vulkan(const Ref<EditorExportPreset> &p_preset) const { |
| 878 | String rendering_method = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile"); |
| 879 | String rendering_driver = get_project_setting(p_preset, "rendering/rendering_device/driver.android"); |
| 880 | return (rendering_method == "forward_plus" || rendering_method == "mobile") && rendering_driver == "vulkan"; |
| 881 | } |
| 882 | |
| 883 | void EditorExportPlatformAndroid::_notification(int p_what) { |
| 884 | #ifndef ANDROID_ENABLED |
| 885 | switch (p_what) { |
| 886 | case NOTIFICATION_POSTINITIALIZE: { |
| 887 | if (EditorExport::get_singleton()) { |
| 888 | EditorExport::get_singleton()->connect_presets_runnable_updated(callable_mp(this, &EditorExportPlatformAndroid::_update_preset_status)); |
| 889 | } |
| 890 | } break; |
| 891 | |
| 892 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 893 | if (EditorSettings::get_singleton()->check_changed_settings_in_group("export/android")) { |
| 894 | _create_editor_debug_keystore_if_needed(); |
| 895 | } |
| 896 | } break; |
| 897 | } |
| 898 | #endif |
| 899 | } |
| 900 | |
| 901 | void EditorExportPlatformAndroid::_create_editor_debug_keystore_if_needed() { |
| 902 | // Check if we have a valid keytool path. |
| 903 | String keytool_path = get_keytool_path(); |
| 904 | if (!FileAccess::exists(keytool_path)) { |
| 905 | return; |
| 906 | } |
| 907 | |
| 908 | // Check if the current editor debug keystore exists. |
| 909 | String editor_debug_keystore = EDITOR_GET("export/android/debug_keystore"); |
| 910 | if (FileAccess::exists(editor_debug_keystore)) { |
| 911 | return; |
| 912 | } |
| 913 | |
| 914 | // Generate the debug keystore. |
| 915 | String keystore_path = EditorPaths::get_singleton()->get_debug_keystore_path(); |
| 916 | String keystores_dir = keystore_path.get_base_dir(); |
| 917 | if (!DirAccess::exists(keystores_dir)) { |
| 918 | Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 919 | Error err = dir_access->make_dir_recursive(keystores_dir); |
| 920 | if (err != OK) { |
| 921 | WARN_PRINT(TTR("Error creating keystores directory:") + "\n" + keystores_dir); |
| 922 | return; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | if (!FileAccess::exists(keystore_path)) { |
| 927 | String output; |
| 928 | List<String> args; |
| 929 | args.push_back("-genkey"); |
| 930 | args.push_back("-keystore"); |
| 931 | args.push_back(keystore_path); |
| 932 | args.push_back("-storepass"); |
| 933 | args.push_back("android"); |
| 934 | args.push_back("-alias"); |
| 935 | args.push_back(DEFAULT_ANDROID_KEYSTORE_DEBUG_USER); |
| 936 | args.push_back("-keypass"); |
| 937 | args.push_back(DEFAULT_ANDROID_KEYSTORE_DEBUG_PASSWORD); |
| 938 | args.push_back("-keyalg"); |
| 939 | args.push_back("RSA"); |
| 940 | args.push_back("-keysize"); |
| 941 | args.push_back("2048"); |
| 942 | args.push_back("-validity"); |
| 943 | args.push_back("10000"); |
| 944 | args.push_back("-dname"); |
| 945 | args.push_back("cn=Godot, ou=Godot Engine, o=Stichting Godot, c=NL"); |
| 946 | Error error = OS::get_singleton()->execute(keytool_path, args, &output, nullptr, true); |
| 947 | print_verbose(output); |
| 948 | if (error != OK) { |
| 949 | WARN_PRINT("Error: Unable to create debug keystore"); |
| 950 | return; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | // Update the editor settings. |
| 955 | EditorSettings::get_singleton()->set("export/android/debug_keystore", keystore_path); |
| 956 | EditorSettings::get_singleton()->set("export/android/debug_keystore_user", DEFAULT_ANDROID_KEYSTORE_DEBUG_USER); |
| 957 | EditorSettings::get_singleton()->set("export/android/debug_keystore_pass", DEFAULT_ANDROID_KEYSTORE_DEBUG_PASSWORD); |
| 958 | print_verbose("Updated editor debug keystore to " + keystore_path); |
| 959 | } |
| 960 | |
| 961 | void EditorExportPlatformAndroid::_get_manifest_info(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, Vector<String> &r_permissions, Vector<FeatureInfo> &r_features, Vector<MetadataInfo> &r_metadata) { |
| 962 | const char **aperms = ANDROID_PERMS; |
| 963 | while (*aperms) { |
| 964 | bool enabled = p_preset->get("permissions/" + String(*aperms).to_lower()); |
| 965 | if (enabled) { |
| 966 | r_permissions.push_back("android.permission." + String(*aperms)); |
| 967 | } |
| 968 | aperms++; |
| 969 | } |
| 970 | PackedStringArray user_perms = p_preset->get("permissions/custom_permissions"); |
| 971 | for (int i = 0; i < user_perms.size(); i++) { |
| 972 | String user_perm = user_perms[i].strip_edges(); |
| 973 | if (!user_perm.is_empty()) { |
| 974 | r_permissions.push_back(user_perm); |
| 975 | } |
| 976 | } |
| 977 | if (p_give_internet) { |
| 978 | if (!r_permissions.has("android.permission.INTERNET")) { |
| 979 | r_permissions.push_back("android.permission.INTERNET"); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | if (_uses_vulkan(p_preset)) { |
| 984 | // Require vulkan hardware level 1 support |
| 985 | FeatureInfo vulkan_level = { |
| 986 | "android.hardware.vulkan.level", // name |
| 987 | false, // required |
| 988 | "1" // version |
| 989 | }; |
| 990 | r_features.append(vulkan_level); |
| 991 | |
| 992 | // Require vulkan version 1.0 |
| 993 | FeatureInfo vulkan_version = { |
| 994 | "android.hardware.vulkan.version", // name |
| 995 | true, // required |
| 996 | "0x400003" // version - Encoded value for api version 1.0 |
| 997 | }; |
| 998 | r_features.append(vulkan_version); |
| 999 | } |
| 1000 | |
| 1001 | MetadataInfo rendering_method_metadata = { |
| 1002 | "org.godotengine.rendering.method", |
| 1003 | p_preset->get_project_setting("rendering/renderer/rendering_method.mobile") |
| 1004 | }; |
| 1005 | r_metadata.append(rendering_method_metadata); |
| 1006 | |
| 1007 | MetadataInfo editor_version_metadata = { |
| 1008 | "org.godotengine.editor.version", |
| 1009 | String(GODOT_VERSION_FULL_CONFIG) |
| 1010 | }; |
| 1011 | r_metadata.append(editor_version_metadata); |
| 1012 | } |
| 1013 | |
| 1014 | void EditorExportPlatformAndroid::_write_tmp_manifest(const Ref<EditorExportPreset> &p_preset, bool p_give_internet, bool p_debug) { |
| 1015 | print_verbose("Building temporary manifest..."); |
| 1016 | String manifest_text = |
| 1017 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 1018 | "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" |
| 1019 | " xmlns:tools=\"http://schemas.android.com/tools\">\n"; |
| 1020 | |
| 1021 | manifest_text += _get_screen_sizes_tag(p_preset); |
| 1022 | manifest_text += _get_gles_tag(); |
| 1023 | |
| 1024 | Vector<String> perms; |
| 1025 | Vector<FeatureInfo> features; |
| 1026 | Vector<MetadataInfo> manifest_metadata; |
| 1027 | _get_manifest_info(p_preset, p_give_internet, perms, features, manifest_metadata); |
| 1028 | for (int i = 0; i < perms.size(); i++) { |
| 1029 | String permission = perms.get(i); |
| 1030 | if (permission == "android.permission.WRITE_EXTERNAL_STORAGE" || (permission == "android.permission.READ_EXTERNAL_STORAGE" && _has_manage_external_storage_permission(perms))) { |
| 1031 | manifest_text += vformat(" <uses-permission android:name=\"%s\" android:maxSdkVersion=\"29\" />\n", permission); |
| 1032 | } else { |
| 1033 | manifest_text += vformat(" <uses-permission android:name=\"%s\" />\n", permission); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | for (int i = 0; i < features.size(); i++) { |
| 1038 | manifest_text += vformat(" <uses-feature tools:node=\"replace\" android:name=\"%s\" android:required=\"%s\" android:version=\"%s\" />\n", features[i].name, features[i].required, features[i].version); |
| 1039 | } |
| 1040 | |
| 1041 | Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins(); |
| 1042 | for (int i = 0; i < export_plugins.size(); i++) { |
| 1043 | if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) { |
| 1044 | const String contents = export_plugins[i]->get_android_manifest_element_contents(Ref<EditorExportPlatform>(this), p_debug); |
| 1045 | if (!contents.is_empty()) { |
| 1046 | const String export_plugin_name = export_plugins[i]->get_name(); |
| 1047 | manifest_text += "<!-- Start of manifest element contents from " + export_plugin_name + " -->\n"; |
| 1048 | manifest_text += contents; |
| 1049 | manifest_text += "\n"; |
| 1050 | manifest_text += "<!-- End of manifest element contents from " + export_plugin_name + " -->\n"; |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | manifest_text += _get_application_tag(Ref<EditorExportPlatform>(this), p_preset, _has_read_write_storage_permission(perms), p_debug, manifest_metadata); |
| 1056 | manifest_text += "</manifest>\n"; |
| 1057 | String manifest_path = ExportTemplateManager::get_android_build_directory(p_preset).path_join(vformat("src/%s/AndroidManifest.xml", (p_debug ? "debug" : "release"))); |
| 1058 | |
| 1059 | print_verbose("Storing manifest into " + manifest_path + ": " + "\n" + manifest_text); |
| 1060 | store_string_at_path(manifest_path, manifest_text); |
| 1061 | } |
| 1062 | |
| 1063 | bool EditorExportPlatformAndroid::_is_transparency_allowed(const Ref<EditorExportPreset> &p_preset) const { |
| 1064 | return (bool)get_project_setting(p_preset, "display/window/per_pixel_transparency/allowed"); |
| 1065 | } |
| 1066 | |
| 1067 | void EditorExportPlatformAndroid::_fix_themes_xml(const Ref<EditorExportPreset> &p_preset) { |
| 1068 | const String themes_xml_path = ExportTemplateManager::get_android_build_directory(p_preset).path_join("res/values/themes.xml"); |
| 1069 | |
| 1070 | if (!FileAccess::exists(themes_xml_path)) { |
| 1071 | print_error("res/values/themes.xml does not exist."); |
| 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | bool transparency_allowed = _is_transparency_allowed(p_preset); |
| 1076 | |
| 1077 | // Default/Reserved theme attributes. |
| 1078 | Dictionary main_theme_attributes; |
| 1079 | main_theme_attributes["android:windowSwipeToDismiss"] = bool_to_string(p_preset->get("gesture/swipe_to_dismiss")); |
| 1080 | main_theme_attributes["android:windowIsTranslucent"] = bool_to_string(transparency_allowed); |
| 1081 | if (transparency_allowed) { |
| 1082 | main_theme_attributes["android:windowBackground"] = "@android:color/transparent"; |
| 1083 | } else { |
| 1084 | main_theme_attributes["android:windowBackground"] = "#" + p_preset->get("screen/background_color").operator Color().to_html(false); |
| 1085 | } |
| 1086 | |
| 1087 | Dictionary splash_theme_attributes; |
| 1088 | splash_theme_attributes["android:windowSplashScreenBackground"] = "@mipmap/icon_background"; |
| 1089 | splash_theme_attributes["windowSplashScreenAnimatedIcon"] = "@mipmap/icon_foreground"; |
| 1090 | splash_theme_attributes["postSplashScreenTheme"] = "@style/GodotAppMainTheme"; |
| 1091 | splash_theme_attributes["android:windowIsTranslucent"] = bool_to_string(transparency_allowed); |
| 1092 | |
| 1093 | PackedStringArray reserved_splash_keys; |
| 1094 | reserved_splash_keys.append("postSplashScreenTheme"); |
| 1095 | reserved_splash_keys.append("android:windowIsTranslucent"); |
| 1096 | |
| 1097 | Dictionary custom_theme_attributes = p_preset->get("gradle_build/custom_theme_attributes"); |
| 1098 | |
| 1099 | // Does not override default/reserved theme attributes; skips any duplicates from custom_theme_attributes. |
| 1100 | for (const Variant &k : custom_theme_attributes.keys()) { |
| 1101 | String key = k; |
| 1102 | String value = custom_theme_attributes[k]; |
| 1103 | if (key.begins_with("[splash]")) { |
| 1104 | String splash_key = key.trim_prefix("[splash]"); |
| 1105 | if (reserved_splash_keys.has(splash_key)) { |
| 1106 | WARN_PRINT(vformat("Skipped custom_theme_attribute '%s'; this is a reserved attribute configured via other export options or project settings.", splash_key)); |
| 1107 | } else { |
| 1108 | splash_theme_attributes[splash_key] = value; |
| 1109 | } |
| 1110 | } else { |
| 1111 | if (main_theme_attributes.has(key)) { |
| 1112 | WARN_PRINT(vformat("Skipped custom_theme_attribute '%s'; this is a reserved attribute configured via other export options or project settings.", key)); |
| 1113 | } else { |
| 1114 | main_theme_attributes[key] = value; |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | Ref<FileAccess> file = FileAccess::open(themes_xml_path, FileAccess::READ); |
| 1120 | PackedStringArray lines = file->get_as_text().split("\n"); |
| 1121 | file->close(); |
| 1122 | |
| 1123 | PackedStringArray new_lines; |
| 1124 | bool inside_main_theme = false; |
| 1125 | bool inside_splash_theme = false; |
| 1126 | |
| 1127 | for (int i = 0; i < lines.size(); i++) { |
| 1128 | String line = lines[i]; |
| 1129 | |
| 1130 | if (line.contains("<style name=\"GodotAppMainTheme\"")) { |
| 1131 | inside_main_theme = true; |
| 1132 | new_lines.append(line); |
| 1133 | continue; |
| 1134 | } |
| 1135 | if (line.contains("<style name=\"GodotAppSplashTheme\"")) { |
| 1136 | inside_splash_theme = true; |
| 1137 | new_lines.append(line); |
| 1138 | continue; |
| 1139 | } |
| 1140 | |
| 1141 | // Inject GodotAppMainTheme attributes. |
| 1142 | if (inside_main_theme && line.contains("</style>")) { |
| 1143 | for (const Variant &attribute : main_theme_attributes.keys()) { |
| 1144 | String value = main_theme_attributes[attribute]; |
| 1145 | String item_line = vformat(" <item name=\"%s\">%s</item>", attribute, value); |
| 1146 | new_lines.append(item_line); |
| 1147 | } |
| 1148 | new_lines.append(line); // Add </style> in the end. |
| 1149 | inside_main_theme = false; |
| 1150 | continue; |
| 1151 | } |
| 1152 | |
| 1153 | // Inject GodotAppSplashTheme attributes. |
| 1154 | if (inside_splash_theme && line.contains("</style>")) { |
| 1155 | for (const Variant &attribute : splash_theme_attributes.keys()) { |
| 1156 | String value = splash_theme_attributes[attribute]; |
| 1157 | String item_line = vformat(" <item name=\"%s\">%s</item>", attribute, value); |
| 1158 | new_lines.append(item_line); |
| 1159 | } |
| 1160 | new_lines.append(line); // Add </style> in the end. |
| 1161 | inside_splash_theme = false; |
| 1162 | continue; |
| 1163 | } |
| 1164 | |
| 1165 | // Add all other lines unchanged. |
| 1166 | if (!inside_main_theme && !inside_splash_theme) { |
| 1167 | new_lines.append(line); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | // Reconstruct the XML content from the modified lines. |
| 1172 | String xml_content = String("\n").join(new_lines); |
| 1173 | store_string_at_path(themes_xml_path, xml_content); |
| 1174 | print_verbose("Successfully modified " + themes_xml_path + ": " + "\n" + xml_content); |
| 1175 | } |
| 1176 | |
| 1177 | void EditorExportPlatformAndroid::_fix_manifest(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &p_manifest, bool p_give_internet) { |
| 1178 | // Leaving the unused types commented because looking these constants up |
| 1179 | // again later would be annoying |
| 1180 | // const int CHUNK_AXML_FILE = 0x00080003; |
| 1181 | // const int CHUNK_RESOURCEIDS = 0x00080180; |
| 1182 | const int CHUNK_STRINGS = 0x001C0001; |
| 1183 | // const int CHUNK_XML_END_NAMESPACE = 0x00100101; |
| 1184 | const int CHUNK_XML_END_TAG = 0x00100103; |
| 1185 | // const int CHUNK_XML_START_NAMESPACE = 0x00100100; |
| 1186 | const int CHUNK_XML_START_TAG = 0x00100102; |
| 1187 | // const int CHUNK_XML_TEXT = 0x00100104; |
| 1188 | const int UTF8_FLAG = 0x00000100; |
| 1189 | |
| 1190 | Vector<String> string_table; |
| 1191 | |
| 1192 | uint32_t ofs = 8; |
| 1193 | |
| 1194 | uint32_t string_count = 0; |
| 1195 | uint32_t string_flags = 0; |
| 1196 | uint32_t string_data_offset = 0; |
| 1197 | |
| 1198 | uint32_t string_table_begins = 0; |
| 1199 | uint32_t string_table_ends = 0; |
| 1200 | Vector<uint8_t> stable_extra; |
| 1201 | |
| 1202 | String version_name = p_preset->get_version("version/name"); |
| 1203 | int version_code = p_preset->get("version/code"); |
| 1204 | String package_name = p_preset->get("package/unique_name"); |
| 1205 | |
| 1206 | const int screen_orientation = |
| 1207 | _get_android_orientation_value(DisplayServer::ScreenOrientation(int(get_project_setting(p_preset, "display/window/handheld/orientation")))); |
| 1208 | |
| 1209 | bool screen_support_small = p_preset->get("screen/support_small"); |
| 1210 | bool screen_support_normal = p_preset->get("screen/support_normal"); |
| 1211 | bool screen_support_large = p_preset->get("screen/support_large"); |
| 1212 | bool screen_support_xlarge = p_preset->get("screen/support_xlarge"); |
| 1213 | |
| 1214 | bool backup_allowed = p_preset->get("user_data_backup/allow"); |
| 1215 | int app_category = p_preset->get("package/app_category"); |
| 1216 | bool retain_data_on_uninstall = p_preset->get("package/retain_data_on_uninstall"); |
| 1217 | bool exclude_from_recents = p_preset->get("package/exclude_from_recents"); |
| 1218 | bool is_resizeable = bool(get_project_setting(p_preset, "display/window/size/resizable")); |
| 1219 | |
| 1220 | Vector<String> perms; |
| 1221 | Vector<FeatureInfo> features; |
| 1222 | Vector<MetadataInfo> manifest_metadata; |
| 1223 | _get_manifest_info(p_preset, p_give_internet, perms, features, manifest_metadata); |
| 1224 | bool has_read_write_storage_permission = _has_read_write_storage_permission(perms); |
| 1225 | |
| 1226 | while (ofs < (uint32_t)p_manifest.size()) { |
| 1227 | uint32_t chunk = decode_uint32(&p_manifest[ofs]); |
| 1228 | uint32_t size = decode_uint32(&p_manifest[ofs + 4]); |
| 1229 | |
| 1230 | switch (chunk) { |
| 1231 | case CHUNK_STRINGS: { |
| 1232 | int iofs = ofs + 8; |
| 1233 | |
| 1234 | string_count = decode_uint32(&p_manifest[iofs]); |
| 1235 | string_flags = decode_uint32(&p_manifest[iofs + 8]); |
| 1236 | string_data_offset = decode_uint32(&p_manifest[iofs + 12]); |
| 1237 | |
| 1238 | uint32_t st_offset = iofs + 20; |
| 1239 | string_table.resize(string_count); |
| 1240 | uint32_t string_end = 0; |
| 1241 | |
| 1242 | string_table_begins = st_offset; |
| 1243 | |
| 1244 | for (uint32_t i = 0; i < string_count; i++) { |
| 1245 | uint32_t string_at = decode_uint32(&p_manifest[st_offset + i * 4]); |
| 1246 | string_at += st_offset + string_count * 4; |
| 1247 | |
| 1248 | ERR_FAIL_COND_MSG(string_flags & UTF8_FLAG, "Unimplemented, can't read UTF-8 string table."); |
| 1249 | |
| 1250 | if (string_flags & UTF8_FLAG) { |
| 1251 | } else { |
| 1252 | uint32_t len = decode_uint16(&p_manifest[string_at]); |
| 1253 | Vector<char32_t> ucstring; |
| 1254 | ucstring.resize(len + 1); |
| 1255 | for (uint32_t j = 0; j < len; j++) { |
| 1256 | uint16_t c = decode_uint16(&p_manifest[string_at + 2 + 2 * j]); |
| 1257 | ucstring.write[j] = c; |
| 1258 | } |
| 1259 | string_end = MAX(string_at + 2 + 2 * len, string_end); |
| 1260 | ucstring.write[len] = 0; |
| 1261 | string_table.write[i] = ucstring.ptr(); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | for (uint32_t i = string_end; i < (ofs + size); i++) { |
| 1266 | stable_extra.push_back(p_manifest[i]); |
| 1267 | } |
| 1268 | |
| 1269 | string_table_ends = ofs + size; |
| 1270 | |
| 1271 | } break; |
| 1272 | case CHUNK_XML_START_TAG: { |
| 1273 | int iofs = ofs + 8; |
| 1274 | uint32_t name = decode_uint32(&p_manifest[iofs + 12]); |
| 1275 | |
| 1276 | String tname = string_table[name]; |
| 1277 | uint32_t attrcount = decode_uint32(&p_manifest[iofs + 20]); |
| 1278 | iofs += 28; |
| 1279 | |
| 1280 | for (uint32_t i = 0; i < attrcount; i++) { |
| 1281 | uint32_t attr_nspace = decode_uint32(&p_manifest[iofs]); |
| 1282 | uint32_t attr_name = decode_uint32(&p_manifest[iofs + 4]); |
| 1283 | uint32_t attr_value = decode_uint32(&p_manifest[iofs + 8]); |
| 1284 | uint32_t attr_resid = decode_uint32(&p_manifest[iofs + 16]); |
| 1285 | |
| 1286 | const String value = (attr_value != 0xFFFFFFFF) ? string_table[attr_value] : "Res #" + itos(attr_resid); |
| 1287 | String attrname = string_table[attr_name]; |
| 1288 | const String nspace = (attr_nspace != 0xFFFFFFFF) ? string_table[attr_nspace] : ""; |
| 1289 | |
| 1290 | //replace project information |
| 1291 | if (tname == "manifest" && attrname == "package") { |
| 1292 | string_table.write[attr_value] = get_package_name(p_preset, package_name); |
| 1293 | } |
| 1294 | |
| 1295 | if (tname == "manifest" && attrname == "versionCode") { |
| 1296 | encode_uint32(version_code, &p_manifest.write[iofs + 16]); |
| 1297 | } |
| 1298 | |
| 1299 | if (tname == "manifest" && attrname == "versionName") { |
| 1300 | if (attr_value == 0xFFFFFFFF) { |
| 1301 | WARN_PRINT("Version name in a resource, should be plain text"); |
| 1302 | } else { |
| 1303 | string_table.write[attr_value] = version_name; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | if (tname == "application" && attrname == "requestLegacyExternalStorage") { |
| 1308 | encode_uint32(has_read_write_storage_permission ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]); |
| 1309 | } |
| 1310 | |
| 1311 | if (tname == "application" && attrname == "allowBackup") { |
| 1312 | encode_uint32(backup_allowed, &p_manifest.write[iofs + 16]); |
| 1313 | } |
| 1314 | |
| 1315 | if (tname == "application" && attrname == "appCategory") { |
| 1316 | encode_uint32(_get_app_category_value(app_category), &p_manifest.write[iofs + 16]); |
| 1317 | } |
| 1318 | |
| 1319 | if (tname == "application" && attrname == "isGame") { |
| 1320 | encode_uint32(app_category == APP_CATEGORY_GAME, &p_manifest.write[iofs + 16]); |
| 1321 | } |
| 1322 | |
| 1323 | if (tname == "application" && attrname == "hasFragileUserData") { |
| 1324 | encode_uint32(retain_data_on_uninstall, &p_manifest.write[iofs + 16]); |
| 1325 | } |
| 1326 | |
| 1327 | if (tname == "activity" && attrname == "screenOrientation") { |
| 1328 | encode_uint32(screen_orientation, &p_manifest.write[iofs + 16]); |
| 1329 | } |
| 1330 | |
| 1331 | if (tname == "activity" && attrname == "excludeFromRecents") { |
| 1332 | encode_uint32(exclude_from_recents, &p_manifest.write[iofs + 16]); |
| 1333 | } |
| 1334 | |
| 1335 | if (tname == "activity" && attrname == "resizeableActivity") { |
| 1336 | encode_uint32(is_resizeable, &p_manifest.write[iofs + 16]); |
| 1337 | } |
| 1338 | |
| 1339 | if (tname == "provider" && attrname == "authorities") { |
| 1340 | string_table.write[attr_value] = get_package_name(p_preset, package_name) + String(".fileprovider"); |
| 1341 | } |
| 1342 | |
| 1343 | if (tname == "supports-screens") { |
| 1344 | if (attrname == "smallScreens") { |
| 1345 | encode_uint32(screen_support_small ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]); |
| 1346 | |
| 1347 | } else if (attrname == "normalScreens") { |
| 1348 | encode_uint32(screen_support_normal ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]); |
| 1349 | |
| 1350 | } else if (attrname == "largeScreens") { |
| 1351 | encode_uint32(screen_support_large ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]); |
| 1352 | |
| 1353 | } else if (attrname == "xlargeScreens") { |
| 1354 | encode_uint32(screen_support_xlarge ? 0xFFFFFFFF : 0, &p_manifest.write[iofs + 16]); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | iofs += 20; |
| 1359 | } |
| 1360 | |
| 1361 | } break; |
| 1362 | case CHUNK_XML_END_TAG: { |
| 1363 | int iofs = ofs + 8; |
| 1364 | uint32_t name = decode_uint32(&p_manifest[iofs + 12]); |
| 1365 | String tname = string_table[name]; |
| 1366 | |
| 1367 | if (tname == "manifest" || tname == "application") { |
| 1368 | // save manifest ending so we can restore it |
| 1369 | Vector<uint8_t> manifest_end; |
| 1370 | uint32_t manifest_cur_size = p_manifest.size(); |
| 1371 | |
| 1372 | manifest_end.resize(p_manifest.size() - ofs); |
| 1373 | memcpy(manifest_end.ptrw(), &p_manifest[ofs], manifest_end.size()); |
| 1374 | |
| 1375 | int32_t attr_name_string = string_table.find("name"); |
| 1376 | ERR_FAIL_COND_MSG(attr_name_string == -1, "Template does not have 'name' attribute."); |
| 1377 | |
| 1378 | int32_t ns_android_string = string_table.find("http://schemas.android.com/apk/res/android"); |
| 1379 | if (ns_android_string == -1) { |
| 1380 | string_table.push_back("http://schemas.android.com/apk/res/android"); |
| 1381 | ns_android_string = string_table.size() - 1; |
| 1382 | } |
| 1383 | |
| 1384 | if (tname == "manifest") { |
| 1385 | // Updating manifest features |
| 1386 | int32_t attr_uses_feature_string = string_table.find("uses-feature"); |
| 1387 | if (attr_uses_feature_string == -1) { |
| 1388 | string_table.push_back("uses-feature"); |
| 1389 | attr_uses_feature_string = string_table.size() - 1; |
| 1390 | } |
| 1391 | |
| 1392 | int32_t attr_required_string = string_table.find("required"); |
| 1393 | if (attr_required_string == -1) { |
| 1394 | string_table.push_back("required"); |
| 1395 | attr_required_string = string_table.size() - 1; |
| 1396 | } |
| 1397 | |
| 1398 | for (int i = 0; i < features.size(); i++) { |
| 1399 | const String &feature_name = features[i].name; |
| 1400 | bool feature_required = features[i].required; |
| 1401 | String feature_version = features[i].version; |
| 1402 | bool has_version_attribute = !feature_version.is_empty(); |
| 1403 | |
| 1404 | print_line("Adding feature " + feature_name); |
| 1405 | |
| 1406 | int32_t feature_string = string_table.find(feature_name); |
| 1407 | if (feature_string == -1) { |
| 1408 | string_table.push_back(feature_name); |
| 1409 | feature_string = string_table.size() - 1; |
| 1410 | } |
| 1411 | |
| 1412 | String required_value_string = feature_required ? "true" : "false"; |
| 1413 | int32_t required_value = string_table.find(required_value_string); |
| 1414 | if (required_value == -1) { |
| 1415 | string_table.push_back(required_value_string); |
| 1416 | required_value = string_table.size() - 1; |
| 1417 | } |
| 1418 | |
| 1419 | int32_t attr_version_string = -1; |
| 1420 | int32_t version_value = -1; |
| 1421 | int tag_size; |
| 1422 | int attr_count; |
| 1423 | if (has_version_attribute) { |
| 1424 | attr_version_string = string_table.find("version"); |
| 1425 | if (attr_version_string == -1) { |
| 1426 | string_table.push_back("version"); |
| 1427 | attr_version_string = string_table.size() - 1; |
| 1428 | } |
| 1429 | |
| 1430 | version_value = string_table.find(feature_version); |
| 1431 | if (version_value == -1) { |
| 1432 | string_table.push_back(feature_version); |
| 1433 | version_value = string_table.size() - 1; |
| 1434 | } |
| 1435 | |
| 1436 | tag_size = 96; // node and three attrs + end node |
| 1437 | attr_count = 3; |
| 1438 | } else { |
| 1439 | tag_size = 76; // node and two attrs + end node |
| 1440 | attr_count = 2; |
| 1441 | } |
| 1442 | manifest_cur_size += tag_size + 24; |
| 1443 | p_manifest.resize(manifest_cur_size); |
| 1444 | |
| 1445 | // start tag |
| 1446 | encode_uint16(0x102, &p_manifest.write[ofs]); // type |
| 1447 | encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize |
| 1448 | encode_uint32(tag_size, &p_manifest.write[ofs + 4]); // size |
| 1449 | encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno |
| 1450 | encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment |
| 1451 | encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns |
| 1452 | encode_uint32(attr_uses_feature_string, &p_manifest.write[ofs + 20]); // name |
| 1453 | encode_uint16(20, &p_manifest.write[ofs + 24]); // attr_start |
| 1454 | encode_uint16(20, &p_manifest.write[ofs + 26]); // attr_size |
| 1455 | encode_uint16(attr_count, &p_manifest.write[ofs + 28]); // num_attrs |
| 1456 | encode_uint16(0, &p_manifest.write[ofs + 30]); // id_index |
| 1457 | encode_uint16(0, &p_manifest.write[ofs + 32]); // class_index |
| 1458 | encode_uint16(0, &p_manifest.write[ofs + 34]); // style_index |
| 1459 | |
| 1460 | // android:name attribute |
| 1461 | encode_uint32(ns_android_string, &p_manifest.write[ofs + 36]); // ns |
| 1462 | encode_uint32(attr_name_string, &p_manifest.write[ofs + 40]); // 'name' |
| 1463 | encode_uint32(feature_string, &p_manifest.write[ofs + 44]); // raw_value |
| 1464 | encode_uint16(8, &p_manifest.write[ofs + 48]); // typedvalue_size |
| 1465 | p_manifest.write[ofs + 50] = 0; // typedvalue_always0 |
| 1466 | p_manifest.write[ofs + 51] = 0x03; // typedvalue_type (string) |
| 1467 | encode_uint32(feature_string, &p_manifest.write[ofs + 52]); // typedvalue reference |
| 1468 | |
| 1469 | // android:required attribute |
| 1470 | encode_uint32(ns_android_string, &p_manifest.write[ofs + 56]); // ns |
| 1471 | encode_uint32(attr_required_string, &p_manifest.write[ofs + 60]); // 'name' |
| 1472 | encode_uint32(required_value, &p_manifest.write[ofs + 64]); // raw_value |
| 1473 | encode_uint16(8, &p_manifest.write[ofs + 68]); // typedvalue_size |
| 1474 | p_manifest.write[ofs + 70] = 0; // typedvalue_always0 |
| 1475 | p_manifest.write[ofs + 71] = 0x03; // typedvalue_type (string) |
| 1476 | encode_uint32(required_value, &p_manifest.write[ofs + 72]); // typedvalue reference |
| 1477 | |
| 1478 | ofs += 76; |
| 1479 | |
| 1480 | if (has_version_attribute) { |
| 1481 | // android:version attribute |
| 1482 | encode_uint32(ns_android_string, &p_manifest.write[ofs]); // ns |
| 1483 | encode_uint32(attr_version_string, &p_manifest.write[ofs + 4]); // 'name' |
| 1484 | encode_uint32(version_value, &p_manifest.write[ofs + 8]); // raw_value |
| 1485 | encode_uint16(8, &p_manifest.write[ofs + 12]); // typedvalue_size |
| 1486 | p_manifest.write[ofs + 14] = 0; // typedvalue_always0 |
| 1487 | p_manifest.write[ofs + 15] = 0x03; // typedvalue_type (string) |
| 1488 | encode_uint32(version_value, &p_manifest.write[ofs + 16]); // typedvalue reference |
| 1489 | |
| 1490 | ofs += 20; |
| 1491 | } |
| 1492 | |
| 1493 | // end tag |
| 1494 | encode_uint16(0x103, &p_manifest.write[ofs]); // type |
| 1495 | encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize |
| 1496 | encode_uint32(24, &p_manifest.write[ofs + 4]); // size |
| 1497 | encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno |
| 1498 | encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment |
| 1499 | encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns |
| 1500 | encode_uint32(attr_uses_feature_string, &p_manifest.write[ofs + 20]); // name |
| 1501 | |
| 1502 | ofs += 24; |
| 1503 | } |
| 1504 | |
| 1505 | // Updating manifest permissions |
| 1506 | int32_t attr_uses_permission_string = string_table.find("uses-permission"); |
| 1507 | if (attr_uses_permission_string == -1) { |
| 1508 | string_table.push_back("uses-permission"); |
| 1509 | attr_uses_permission_string = string_table.size() - 1; |
| 1510 | } |
| 1511 | |
| 1512 | for (int i = 0; i < perms.size(); ++i) { |
| 1513 | print_line("Adding permission " + perms[i]); |
| 1514 | |
| 1515 | manifest_cur_size += 56 + 24; // node + end node |
| 1516 | p_manifest.resize(manifest_cur_size); |
| 1517 | |
| 1518 | // Add permission to the string pool |
| 1519 | int32_t perm_string = string_table.find(perms[i]); |
| 1520 | if (perm_string == -1) { |
| 1521 | string_table.push_back(perms[i]); |
| 1522 | perm_string = string_table.size() - 1; |
| 1523 | } |
| 1524 | |
| 1525 | // start tag |
| 1526 | encode_uint16(0x102, &p_manifest.write[ofs]); // type |
| 1527 | encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize |
| 1528 | encode_uint32(56, &p_manifest.write[ofs + 4]); // size |
| 1529 | encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno |
| 1530 | encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment |
| 1531 | encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns |
| 1532 | encode_uint32(attr_uses_permission_string, &p_manifest.write[ofs + 20]); // name |
| 1533 | encode_uint16(20, &p_manifest.write[ofs + 24]); // attr_start |
| 1534 | encode_uint16(20, &p_manifest.write[ofs + 26]); // attr_size |
| 1535 | encode_uint16(1, &p_manifest.write[ofs + 28]); // num_attrs |
| 1536 | encode_uint16(0, &p_manifest.write[ofs + 30]); // id_index |
| 1537 | encode_uint16(0, &p_manifest.write[ofs + 32]); // class_index |
| 1538 | encode_uint16(0, &p_manifest.write[ofs + 34]); // style_index |
| 1539 | |
| 1540 | // attribute |
| 1541 | encode_uint32(ns_android_string, &p_manifest.write[ofs + 36]); // ns |
| 1542 | encode_uint32(attr_name_string, &p_manifest.write[ofs + 40]); // 'name' |
| 1543 | encode_uint32(perm_string, &p_manifest.write[ofs + 44]); // raw_value |
| 1544 | encode_uint16(8, &p_manifest.write[ofs + 48]); // typedvalue_size |
| 1545 | p_manifest.write[ofs + 50] = 0; // typedvalue_always0 |
| 1546 | p_manifest.write[ofs + 51] = 0x03; // typedvalue_type (string) |
| 1547 | encode_uint32(perm_string, &p_manifest.write[ofs + 52]); // typedvalue reference |
| 1548 | |
| 1549 | ofs += 56; |
| 1550 | |
| 1551 | // end tag |
| 1552 | encode_uint16(0x103, &p_manifest.write[ofs]); // type |
| 1553 | encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize |
| 1554 | encode_uint32(24, &p_manifest.write[ofs + 4]); // size |
| 1555 | encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno |
| 1556 | encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment |
| 1557 | encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns |
| 1558 | encode_uint32(attr_uses_permission_string, &p_manifest.write[ofs + 20]); // name |
| 1559 | |
| 1560 | ofs += 24; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | if (tname == "application") { |
| 1565 | // Updating application meta-data |
| 1566 | int32_t attr_meta_data_string = string_table.find("meta-data"); |
| 1567 | if (attr_meta_data_string == -1) { |
| 1568 | string_table.push_back("meta-data"); |
| 1569 | attr_meta_data_string = string_table.size() - 1; |
| 1570 | } |
| 1571 | |
| 1572 | int32_t attr_value_string = string_table.find("value"); |
| 1573 | if (attr_value_string == -1) { |
| 1574 | string_table.push_back("value"); |
| 1575 | attr_value_string = string_table.size() - 1; |
| 1576 | } |
| 1577 | |
| 1578 | for (int i = 0; i < manifest_metadata.size(); i++) { |
| 1579 | String meta_data_name = manifest_metadata[i].name; |
| 1580 | String meta_data_value = manifest_metadata[i].value; |
| 1581 | |
| 1582 | print_line("Adding application metadata " + meta_data_name); |
| 1583 | |
| 1584 | int32_t meta_data_name_string = string_table.find(meta_data_name); |
| 1585 | if (meta_data_name_string == -1) { |
| 1586 | string_table.push_back(meta_data_name); |
| 1587 | meta_data_name_string = string_table.size() - 1; |
| 1588 | } |
| 1589 | |
| 1590 | int32_t meta_data_value_string = string_table.find(meta_data_value); |
| 1591 | if (meta_data_value_string == -1) { |
| 1592 | string_table.push_back(meta_data_value); |
| 1593 | meta_data_value_string = string_table.size() - 1; |
| 1594 | } |
| 1595 | |
| 1596 | int tag_size = 76; // node and two attrs + end node |
| 1597 | int attr_count = 2; |
| 1598 | manifest_cur_size += tag_size + 24; |
| 1599 | p_manifest.resize(manifest_cur_size); |
| 1600 | |
| 1601 | // start tag |
| 1602 | encode_uint16(0x102, &p_manifest.write[ofs]); // type |
| 1603 | encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize |
| 1604 | encode_uint32(tag_size, &p_manifest.write[ofs + 4]); // size |
| 1605 | encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno |
| 1606 | encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment |
| 1607 | encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns |
| 1608 | encode_uint32(attr_meta_data_string, &p_manifest.write[ofs + 20]); // name |
| 1609 | encode_uint16(20, &p_manifest.write[ofs + 24]); // attr_start |
| 1610 | encode_uint16(20, &p_manifest.write[ofs + 26]); // attr_size |
| 1611 | encode_uint16(attr_count, &p_manifest.write[ofs + 28]); // num_attrs |
| 1612 | encode_uint16(0, &p_manifest.write[ofs + 30]); // id_index |
| 1613 | encode_uint16(0, &p_manifest.write[ofs + 32]); // class_index |
| 1614 | encode_uint16(0, &p_manifest.write[ofs + 34]); // style_index |
| 1615 | |
| 1616 | // android:name attribute |
| 1617 | encode_uint32(ns_android_string, &p_manifest.write[ofs + 36]); // ns |
| 1618 | encode_uint32(attr_name_string, &p_manifest.write[ofs + 40]); // 'name' |
| 1619 | encode_uint32(meta_data_name_string, &p_manifest.write[ofs + 44]); // raw_value |
| 1620 | encode_uint16(8, &p_manifest.write[ofs + 48]); // typedvalue_size |
| 1621 | p_manifest.write[ofs + 50] = 0; // typedvalue_always0 |
| 1622 | p_manifest.write[ofs + 51] = 0x03; // typedvalue_type (string) |
| 1623 | encode_uint32(meta_data_name_string, &p_manifest.write[ofs + 52]); // typedvalue reference |
| 1624 | |
| 1625 | // android:value attribute |
| 1626 | encode_uint32(ns_android_string, &p_manifest.write[ofs + 56]); // ns |
| 1627 | encode_uint32(attr_value_string, &p_manifest.write[ofs + 60]); // 'value' |
| 1628 | encode_uint32(meta_data_value_string, &p_manifest.write[ofs + 64]); // raw_value |
| 1629 | encode_uint16(8, &p_manifest.write[ofs + 68]); // typedvalue_size |
| 1630 | p_manifest.write[ofs + 70] = 0; // typedvalue_always0 |
| 1631 | p_manifest.write[ofs + 71] = 0x03; // typedvalue_type (string) |
| 1632 | encode_uint32(meta_data_value_string, &p_manifest.write[ofs + 72]); // typedvalue reference |
| 1633 | |
| 1634 | ofs += 76; |
| 1635 | |
| 1636 | // end tag |
| 1637 | encode_uint16(0x103, &p_manifest.write[ofs]); // type |
| 1638 | encode_uint16(16, &p_manifest.write[ofs + 2]); // headersize |
| 1639 | encode_uint32(24, &p_manifest.write[ofs + 4]); // size |
| 1640 | encode_uint32(0, &p_manifest.write[ofs + 8]); // lineno |
| 1641 | encode_uint32(-1, &p_manifest.write[ofs + 12]); // comment |
| 1642 | encode_uint32(-1, &p_manifest.write[ofs + 16]); // ns |
| 1643 | encode_uint32(attr_meta_data_string, &p_manifest.write[ofs + 20]); // name |
| 1644 | |
| 1645 | ofs += 24; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | // copy footer back in |
| 1650 | memcpy(&p_manifest.write[ofs], manifest_end.ptr(), manifest_end.size()); |
| 1651 | } |
| 1652 | } break; |
| 1653 | } |
| 1654 | |
| 1655 | ofs += size; |
| 1656 | } |
| 1657 | |
| 1658 | // Create new android manifest binary. |
| 1659 | |
| 1660 | Vector<uint8_t> ret; |
| 1661 | ret.resize(string_table_begins + string_table.size() * 4); |
| 1662 | |
| 1663 | for (uint32_t i = 0; i < string_table_begins; i++) { |
| 1664 | ret.write[i] = p_manifest[i]; |
| 1665 | } |
| 1666 | |
| 1667 | ofs = 0; |
| 1668 | for (int i = 0; i < string_table.size(); i++) { |
| 1669 | encode_uint32(ofs, &ret.write[string_table_begins + i * 4]); |
| 1670 | ofs += string_table[i].length() * 2 + 2 + 2; |
| 1671 | } |
| 1672 | |
| 1673 | ret.resize(ret.size() + ofs); |
| 1674 | string_data_offset = ret.size() - ofs; |
| 1675 | uint8_t *chars = &ret.write[string_data_offset]; |
| 1676 | for (int i = 0; i < string_table.size(); i++) { |
| 1677 | String s = string_table[i]; |
| 1678 | encode_uint16(s.length(), chars); |
| 1679 | chars += 2; |
| 1680 | for (int j = 0; j < s.length(); j++) { |
| 1681 | encode_uint16(s[j], chars); |
| 1682 | chars += 2; |
| 1683 | } |
| 1684 | encode_uint16(0, chars); |
| 1685 | chars += 2; |
| 1686 | } |
| 1687 | |
| 1688 | for (int i = 0; i < stable_extra.size(); i++) { |
| 1689 | ret.push_back(stable_extra[i]); |
| 1690 | } |
| 1691 | |
| 1692 | //pad |
| 1693 | while (ret.size() % 4) { |
| 1694 | ret.push_back(0); |
| 1695 | } |
| 1696 | |
| 1697 | uint32_t new_stable_end = ret.size(); |
| 1698 | |
| 1699 | uint32_t extra = (p_manifest.size() - string_table_ends); |
| 1700 | ret.resize(new_stable_end + extra); |
| 1701 | for (uint32_t i = 0; i < extra; i++) { |
| 1702 | ret.write[new_stable_end + i] = p_manifest[string_table_ends + i]; |
| 1703 | } |
| 1704 | |
| 1705 | while (ret.size() % 4) { |
| 1706 | ret.push_back(0); |
| 1707 | } |
| 1708 | encode_uint32(ret.size(), &ret.write[4]); //update new file size |
| 1709 | |
| 1710 | encode_uint32(new_stable_end - 8, &ret.write[12]); //update new string table size |
| 1711 | encode_uint32(string_table.size(), &ret.write[16]); //update new number of strings |
| 1712 | encode_uint32(string_data_offset - 8, &ret.write[28]); //update new string data offset |
| 1713 | |
| 1714 | p_manifest = ret; |
| 1715 | } |
| 1716 | |
| 1717 | String EditorExportPlatformAndroid::_get_keystore_path(const Ref<EditorExportPreset> &p_preset, bool p_debug) { |
| 1718 | String keystore_preference = p_debug ? "keystore/debug" : "keystore/release"; |
| 1719 | String keystore_env_variable = p_debug ? ENV_ANDROID_KEYSTORE_DEBUG_PATH : ENV_ANDROID_KEYSTORE_RELEASE_PATH; |
| 1720 | String keystore_path = p_preset->get_or_env(keystore_preference, keystore_env_variable); |
| 1721 | |
| 1722 | return ProjectSettings::get_singleton()->globalize_path(keystore_path).simplify_path(); |
| 1723 | } |
| 1724 | |
| 1725 | String EditorExportPlatformAndroid::_parse_string(const uint8_t *p_bytes, bool p_utf8) { |
| 1726 | uint32_t offset = 0; |
| 1727 | uint32_t len = 0; |
| 1728 | |
| 1729 | if (p_utf8) { |
| 1730 | uint8_t byte = p_bytes[offset]; |
| 1731 | if (byte & 0x80) { |
| 1732 | offset += 2; |
| 1733 | } else { |
| 1734 | offset += 1; |
| 1735 | } |
| 1736 | byte = p_bytes[offset]; |
| 1737 | offset++; |
| 1738 | if (byte & 0x80) { |
| 1739 | len = byte & 0x7F; |
| 1740 | len = (len << 8) + p_bytes[offset]; |
| 1741 | offset++; |
| 1742 | } else { |
| 1743 | len = byte; |
| 1744 | } |
| 1745 | } else { |
| 1746 | len = decode_uint16(&p_bytes[offset]); |
| 1747 | offset += 2; |
| 1748 | if (len & 0x8000) { |
| 1749 | len &= 0x7FFF; |
| 1750 | len = (len << 16) + decode_uint16(&p_bytes[offset]); |
| 1751 | offset += 2; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | if (p_utf8) { |
| 1756 | Vector<uint8_t> str8; |
| 1757 | str8.resize(len + 1); |
| 1758 | for (uint32_t i = 0; i < len; i++) { |
| 1759 | str8.write[i] = p_bytes[offset + i]; |
| 1760 | } |
| 1761 | str8.write[len] = 0; |
| 1762 | return String::utf8((const char *)str8.ptr(), len); |
| 1763 | } else { |
| 1764 | String str; |
| 1765 | for (uint32_t i = 0; i < len; i++) { |
| 1766 | char32_t c = decode_uint16(&p_bytes[offset + i * 2]); |
| 1767 | if (c == 0) { |
| 1768 | break; |
| 1769 | } |
| 1770 | str += String::chr(c); |
| 1771 | } |
| 1772 | return str; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | void EditorExportPlatformAndroid::_fix_resources(const Ref<EditorExportPreset> &p_preset, Vector<uint8_t> &r_manifest) { |
| 1777 | const int UTF8_FLAG = 0x00000100; |
| 1778 | |
| 1779 | uint32_t string_block_len = decode_uint32(&r_manifest[16]); |
| 1780 | uint32_t string_count = decode_uint32(&r_manifest[20]); |
| 1781 | uint32_t string_flags = decode_uint32(&r_manifest[28]); |
| 1782 | const uint32_t string_table_begins = 40; |
| 1783 | |
| 1784 | Vector<String> string_table; |
| 1785 | |
| 1786 | const String project_name = get_project_name(p_preset, p_preset->get("package/name")); |
| 1787 | const Dictionary appnames = get_project_setting(p_preset, "application/config/name_localized"); |
| 1788 | const StringName domain_name = "godot.project_name_localization"; |
| 1789 | Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_or_add_domain(domain_name); |
| 1790 | TranslationServer::get_singleton()->load_project_translations(domain); |
| 1791 | |
| 1792 | for (uint32_t i = 0; i < string_count; i++) { |
| 1793 | uint32_t offset = decode_uint32(&r_manifest[string_table_begins + i * 4]); |
| 1794 | offset += string_table_begins + string_count * 4; |
| 1795 | |
| 1796 | String str = _parse_string(&r_manifest[offset], string_flags & UTF8_FLAG); |
| 1797 | |
| 1798 | if (str == "godot-project-name") { |
| 1799 | str = project_name; |
| 1800 | } else if (str.begins_with("godot-project-name")) { |
| 1801 | String lang = str.substr(str.rfind_char('-') + 1).replace_char('-', '_'); |
| 1802 | |
| 1803 | if (appnames.is_empty()) { |
| 1804 | domain->set_locale_override(lang); |
| 1805 | str = domain->translate(project_name, String()); |
| 1806 | } else { |
| 1807 | str = appnames.get(lang, project_name); |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | string_table.push_back(str); |
| 1812 | } |
| 1813 | |
| 1814 | TranslationServer::get_singleton()->remove_domain(domain_name); |
| 1815 | |
| 1816 | //write a new string table, but use 16 bits |
| 1817 | Vector<uint8_t> ret; |
| 1818 | ret.resize(string_table_begins + string_table.size() * 4); |
| 1819 | |
| 1820 | for (uint32_t i = 0; i < string_table_begins; i++) { |
| 1821 | ret.write[i] = r_manifest[i]; |
| 1822 | } |
| 1823 | |
| 1824 | int ofs = 0; |
| 1825 | for (int i = 0; i < string_table.size(); i++) { |
| 1826 | encode_uint32(ofs, &ret.write[string_table_begins + i * 4]); |
| 1827 | ofs += string_table[i].length() * 2 + 2 + 2; |
| 1828 | } |
| 1829 | |
| 1830 | ret.resize(ret.size() + ofs); |
| 1831 | uint8_t *chars = &ret.write[ret.size() - ofs]; |
| 1832 | for (int i = 0; i < string_table.size(); i++) { |
| 1833 | String s = string_table[i]; |
| 1834 | encode_uint16(s.length(), chars); |
| 1835 | chars += 2; |
| 1836 | for (int j = 0; j < s.length(); j++) { |
| 1837 | encode_uint16(s[j], chars); |
| 1838 | chars += 2; |
| 1839 | } |
| 1840 | encode_uint16(0, chars); |
| 1841 | chars += 2; |
| 1842 | } |
| 1843 | |
| 1844 | //pad |
| 1845 | while (ret.size() % 4) { |
| 1846 | ret.push_back(0); |
| 1847 | } |
| 1848 | |
| 1849 | //change flags to not use utf8 |
| 1850 | encode_uint32(string_flags & ~0x100, &ret.write[28]); |
| 1851 | //change length |
| 1852 | encode_uint32(ret.size() - 12, &ret.write[16]); |
| 1853 | //append the rest... |
| 1854 | int rest_from = 12 + string_block_len; |
| 1855 | int rest_to = ret.size(); |
| 1856 | int rest_len = (r_manifest.size() - rest_from); |
| 1857 | ret.resize(ret.size() + (r_manifest.size() - rest_from)); |
| 1858 | for (int i = 0; i < rest_len; i++) { |
| 1859 | ret.write[rest_to + i] = r_manifest[rest_from + i]; |
| 1860 | } |
| 1861 | //finally update the size |
| 1862 | encode_uint32(ret.size(), &ret.write[4]); |
| 1863 | |
| 1864 | r_manifest = ret; |
| 1865 | //printf("end\n"); |
| 1866 | } |
| 1867 | |
| 1868 | void EditorExportPlatformAndroid::_process_launcher_icons(const String &p_file_name, const Ref<Image> &p_source_image, int dimension, Vector<uint8_t> &p_data) { |
| 1869 | Ref<Image> working_image = p_source_image; |
| 1870 | |
| 1871 | if (p_source_image->get_width() != dimension || p_source_image->get_height() != dimension) { |
| 1872 | working_image = p_source_image->duplicate(); |
| 1873 | working_image->resize(dimension, dimension, Image::Interpolation::INTERPOLATE_LANCZOS); |
| 1874 | } |
| 1875 | |
| 1876 | Vector<uint8_t> buffer = working_image->save_webp_to_buffer(); |
| 1877 | p_data.resize(buffer.size()); |
| 1878 | memcpy(p_data.ptrw(), buffer.ptr(), p_data.size()); |
| 1879 | } |
| 1880 | |
| 1881 | void EditorExportPlatformAndroid::load_icon_refs(const Ref<EditorExportPreset> &p_preset, Ref<Image> &icon, Ref<Image> &foreground, Ref<Image> &background, Ref<Image> &monochrome) { |
| 1882 | String project_icon_path = get_project_setting(p_preset, "application/config/icon"); |
| 1883 | |
| 1884 | Error err = OK; |
| 1885 | |
| 1886 | // Regular icon: user selection -> project icon -> default. |
| 1887 | String path = static_cast<String>(p_preset->get(LAUNCHER_ICON_OPTION)).strip_edges(); |
| 1888 | print_verbose("Loading regular icon from " + path); |
| 1889 | if (!path.is_empty()) { |
| 1890 | icon = _load_icon_or_splash_image(path, &err); |
| 1891 | } |
| 1892 | if (path.is_empty() || err != OK || icon.is_null() || icon->is_empty()) { |
| 1893 | print_verbose("- falling back to project icon: " + project_icon_path); |
| 1894 | if (!project_icon_path.is_empty()) { |
| 1895 | icon = _load_icon_or_splash_image(project_icon_path, &err); |
| 1896 | } else { |
| 1897 | ERR_PRINT("No project icon specified. Please specify one in the Project Settings under Application -> Config -> Icon"); |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // Adaptive foreground: user selection -> regular icon (user selection -> project icon -> default). |
| 1902 | path = static_cast<String>(p_preset->get(LAUNCHER_ADAPTIVE_ICON_FOREGROUND_OPTION)).strip_edges(); |
| 1903 | print_verbose("Loading adaptive foreground icon from " + path); |
| 1904 | if (!path.is_empty()) { |
| 1905 | foreground = _load_icon_or_splash_image(path, &err); |
| 1906 | } |
| 1907 | if (path.is_empty() || err != OK || foreground.is_null() || foreground->is_empty()) { |
| 1908 | print_verbose("- falling back to using the regular icon"); |
| 1909 | foreground = icon; |
| 1910 | } |
| 1911 | |
| 1912 | // Adaptive background: user selection -> default. |
| 1913 | path = static_cast<String>(p_preset->get(LAUNCHER_ADAPTIVE_ICON_BACKGROUND_OPTION)).strip_edges(); |
| 1914 | if (!path.is_empty()) { |
| 1915 | print_verbose("Loading adaptive background icon from " + path); |
| 1916 | background = _load_icon_or_splash_image(path, &err); |
| 1917 | } |
| 1918 | |
| 1919 | // Adaptive monochrome: user selection -> default. |
| 1920 | path = static_cast<String>(p_preset->get(LAUNCHER_ADAPTIVE_ICON_MONOCHROME_OPTION)).strip_edges(); |
| 1921 | if (!path.is_empty()) { |
| 1922 | print_verbose("Loading adaptive monochrome icon from " + path); |
| 1923 | monochrome = _load_icon_or_splash_image(path, &err); |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | void EditorExportPlatformAndroid::_copy_icons_to_gradle_project(const Ref<EditorExportPreset> &p_preset, |
| 1928 | const Ref<Image> &p_main_image, |
| 1929 | const Ref<Image> &p_foreground, |
| 1930 | const Ref<Image> &p_background, |
| 1931 | const Ref<Image> &p_monochrome) { |
| 1932 | String gradle_build_dir = ExportTemplateManager::get_android_build_directory(p_preset); |
| 1933 | |
| 1934 | String monochrome_tag = ""; |
| 1935 | |
| 1936 | // Prepare images to be resized for the icons. If some image ends up being uninitialized, |
| 1937 | // the default image from the export template will be used. |
| 1938 | |
| 1939 | for (int i = 0; i < ICON_DENSITIES_COUNT; ++i) { |
| 1940 | if (p_main_image.is_valid() && !p_main_image->is_empty()) { |
| 1941 | print_verbose("Processing launcher icon for dimension " + itos(LAUNCHER_ICONS[i].dimensions) + " into " + LAUNCHER_ICONS[i].export_path); |
| 1942 | Vector<uint8_t> data; |
| 1943 | _process_launcher_icons(LAUNCHER_ICONS[i].export_path, p_main_image, LAUNCHER_ICONS[i].dimensions, data); |
| 1944 | store_file_at_path(gradle_build_dir.path_join(LAUNCHER_ICONS[i].export_path), data); |
| 1945 | } |
| 1946 | |
| 1947 | if (p_foreground.is_valid() && !p_foreground->is_empty()) { |
| 1948 | print_verbose("Processing launcher adaptive icon p_foreground for dimension " + itos(LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].dimensions) + " into " + LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].export_path); |
| 1949 | Vector<uint8_t> data; |
| 1950 | _process_launcher_icons(LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].export_path, p_foreground, |
| 1951 | LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].dimensions, data); |
| 1952 | store_file_at_path(gradle_build_dir.path_join(LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].export_path), data); |
| 1953 | } |
| 1954 | |
| 1955 | if (p_background.is_valid() && !p_background->is_empty()) { |
| 1956 | print_verbose("Processing launcher adaptive icon p_background for dimension " + itos(LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].dimensions) + " into " + LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].export_path); |
| 1957 | Vector<uint8_t> data; |
| 1958 | _process_launcher_icons(LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].export_path, p_background, |
| 1959 | LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].dimensions, data); |
| 1960 | store_file_at_path(gradle_build_dir.path_join(LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].export_path), data); |
| 1961 | } |
| 1962 | |
| 1963 | if (p_monochrome.is_valid() && !p_monochrome->is_empty()) { |
| 1964 | print_verbose("Processing launcher adaptive icon p_monochrome for dimension " + itos(LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].dimensions) + " into " + LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].export_path); |
| 1965 | Vector<uint8_t> data; |
| 1966 | _process_launcher_icons(LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].export_path, p_monochrome, |
| 1967 | LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].dimensions, data); |
| 1968 | store_file_at_path(gradle_build_dir.path_join(LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].export_path), data); |
| 1969 | monochrome_tag = " <monochrome android:drawable=\"@mipmap/icon_monochrome\"/>\n"; |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | // Finalize the icon.xml by formatting the template with the optional monochrome tag. |
| 1974 | store_string_at_path(gradle_build_dir.path_join(ICON_XML_PATH), vformat(ICON_XML_TEMPLATE, monochrome_tag)); |
| 1975 | } |
| 1976 | |
| 1977 | Vector<EditorExportPlatformAndroid::ABI> EditorExportPlatformAndroid::get_enabled_abis(const Ref<EditorExportPreset> &p_preset) { |
| 1978 | Vector<ABI> abis = get_abis(); |
| 1979 | Vector<ABI> enabled_abis; |
| 1980 | for (int i = 0; i < abis.size(); ++i) { |
| 1981 | bool is_enabled = p_preset->get("architectures/" + abis[i].abi); |
| 1982 | if (is_enabled) { |
| 1983 | enabled_abis.push_back(abis[i]); |
| 1984 | } |
| 1985 | } |
| 1986 | return enabled_abis; |
| 1987 | } |
| 1988 | |
| 1989 | void EditorExportPlatformAndroid::get_preset_features(const Ref<EditorExportPreset> &p_preset, List<String> *r_features) const { |
| 1990 | r_features->push_back("etc2"); |
| 1991 | r_features->push_back("astc"); |
| 1992 | |
| 1993 | if (!p_preset->is_dedicated_server() && p_preset->get("shader_baker/enabled")) { |
| 1994 | // Don't use the shader baker if exporting as a dedicated server, as no rendering is performed. |
| 1995 | r_features->push_back("shader_baker"); |
| 1996 | } |
| 1997 | |
| 1998 | Vector<ABI> abis = get_enabled_abis(p_preset); |
| 1999 | for (int i = 0; i < abis.size(); ++i) { |
| 2000 | r_features->push_back(abis[i].arch); |
| 2001 | } |
| 2002 | } |
| 2003 | |
| 2004 | String EditorExportPlatformAndroid::get_export_option_warning(const EditorExportPreset *p_preset, const StringName &p_name) const { |
| 2005 | if (p_preset) { |
| 2006 | if (p_name == ("apk_expansion/public_key")) { |
| 2007 | bool apk_expansion = p_preset->get("apk_expansion/enable"); |
| 2008 | String apk_expansion_pkey = p_preset->get("apk_expansion/public_key"); |
| 2009 | if (apk_expansion && apk_expansion_pkey.is_empty()) { |
| 2010 | return TTR("Invalid public key for APK expansion."); |
| 2011 | } |
| 2012 | } else if (p_name == "package/unique_name") { |
| 2013 | String pn = p_preset->get("package/unique_name"); |
| 2014 | String pn_err; |
| 2015 | |
| 2016 | if (!is_package_name_valid(Ref<EditorExportPreset>(p_preset), pn, &pn_err)) { |
| 2017 | return TTR("Invalid package name:") + " " + pn_err; |
| 2018 | } |
| 2019 | } else if (p_name == "gesture/swipe_to_dismiss") { |
| 2020 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2021 | if (bool(p_preset->get("gesture/swipe_to_dismiss")) && !gradle_build_enabled) { |
| 2022 | return TTR("\"Use Gradle Build\" is required to enable \"Swipe to dismiss\"."); |
| 2023 | } |
| 2024 | } else if (p_name == "gradle_build/use_gradle_build") { |
| 2025 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2026 | String enabled_deprecated_plugins_names = _get_deprecated_plugins_names(Ref<EditorExportPreset>(p_preset)); |
| 2027 | if (!enabled_deprecated_plugins_names.is_empty() && !gradle_build_enabled) { |
| 2028 | return TTR("\"Use Gradle Build\" must be enabled to use the plugins."); |
| 2029 | } |
| 2030 | #ifdef ANDROID_ENABLED |
| 2031 | if (gradle_build_enabled) { |
| 2032 | return TTR("Support for \"Use Gradle Build\" on Android is currently experimental."); |
| 2033 | } |
| 2034 | #endif // ANDROID_ENABLED |
| 2035 | } else if (p_name == "gradle_build/compress_native_libraries") { |
| 2036 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2037 | if (bool(p_preset->get("gradle_build/compress_native_libraries")) && !gradle_build_enabled) { |
| 2038 | return TTR("\"Compress Native Libraries\" is only valid when \"Use Gradle Build\" is enabled."); |
| 2039 | } |
| 2040 | } else if (p_name == "gradle_build/export_format") { |
| 2041 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2042 | if (int(p_preset->get("gradle_build/export_format")) == EXPORT_FORMAT_AAB && !gradle_build_enabled) { |
| 2043 | return TTR("\"Export AAB\" is only valid when \"Use Gradle Build\" is enabled."); |
| 2044 | } |
| 2045 | } else if (p_name == "gradle_build/min_sdk") { |
| 2046 | String min_sdk_str = p_preset->get("gradle_build/min_sdk"); |
| 2047 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2048 | if (!min_sdk_str.is_empty()) { // Empty means no override, nothing to do. |
| 2049 | if (!gradle_build_enabled) { |
| 2050 | return TTR("\"Min SDK\" can only be overridden when \"Use Gradle Build\" is enabled."); |
| 2051 | } |
| 2052 | if (!min_sdk_str.is_valid_int()) { |
| 2053 | return vformat(TTR("\"Min SDK\" should be a valid integer, but got \"%s\" which is invalid."), min_sdk_str); |
| 2054 | } else { |
| 2055 | int min_sdk_int = min_sdk_str.to_int(); |
| 2056 | if (min_sdk_int < DEFAULT_MIN_SDK_VERSION) { |
| 2057 | return vformat(TTR("\"Min SDK\" cannot be lower than %d, which is the version needed by the Godot library."), DEFAULT_MIN_SDK_VERSION); |
| 2058 | } |
| 2059 | } |
| 2060 | } |
| 2061 | } else if (p_name == "gradle_build/target_sdk") { |
| 2062 | String target_sdk_str = p_preset->get("gradle_build/target_sdk"); |
| 2063 | int target_sdk_int = DEFAULT_TARGET_SDK_VERSION; |
| 2064 | |
| 2065 | String min_sdk_str = p_preset->get("gradle_build/min_sdk"); |
| 2066 | int min_sdk_int = DEFAULT_MIN_SDK_VERSION; |
| 2067 | if (min_sdk_str.is_valid_int()) { |
| 2068 | min_sdk_int = min_sdk_str.to_int(); |
| 2069 | } |
| 2070 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2071 | if (!target_sdk_str.is_empty()) { // Empty means no override, nothing to do. |
| 2072 | if (!gradle_build_enabled) { |
| 2073 | return TTR("\"Target SDK\" can only be overridden when \"Use Gradle Build\" is enabled."); |
| 2074 | } |
| 2075 | if (!target_sdk_str.is_valid_int()) { |
| 2076 | return vformat(TTR("\"Target SDK\" should be a valid integer, but got \"%s\" which is invalid."), target_sdk_str); |
| 2077 | } else { |
| 2078 | target_sdk_int = target_sdk_str.to_int(); |
| 2079 | if (target_sdk_int < min_sdk_int) { |
| 2080 | return TTR("\"Target SDK\" version must be greater or equal to \"Min SDK\" version."); |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | } else if (p_name == "gradle_build/custom_theme_attributes") { |
| 2085 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2086 | if (bool(p_preset->get("gradle_build/custom_theme_attributes")) && !gradle_build_enabled) { |
| 2087 | return TTR("\"Use Gradle Build\" is required to add custom theme attributes."); |
| 2088 | } |
| 2089 | } else if (p_name == "package/show_in_android_tv") { |
| 2090 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2091 | if (bool(p_preset->get("package/show_in_android_tv")) && !gradle_build_enabled) { |
| 2092 | return TTR("\"Use Gradle Build\" must be enabled to enable \"Show In Android Tv\"."); |
| 2093 | } |
| 2094 | } else if (p_name == "package/show_as_launcher_app") { |
| 2095 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2096 | if (bool(p_preset->get("package/show_as_launcher_app")) && !gradle_build_enabled) { |
| 2097 | return TTR("\"Use Gradle Build\" must be enabled to enable \"Show As Launcher App\"."); |
| 2098 | } |
| 2099 | } else if (p_name == "package/show_in_app_library") { |
| 2100 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2101 | if (!bool(p_preset->get("package/show_in_app_library")) && !gradle_build_enabled) { |
| 2102 | return TTR("\"Use Gradle Build\" must be enabled to disable \"Show In App Library\"."); |
| 2103 | } |
| 2104 | } else if (p_name == "shader_baker/enabled" && bool(p_preset->get("shader_baker/enabled"))) { |
| 2105 | String export_renderer = GLOBAL_GET("rendering/renderer/rendering_method.mobile"); |
| 2106 | if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") { |
| 2107 | return TTR("\"Shader Baker\" is not supported when using the Compatibility renderer."); |
| 2108 | } else if (OS::get_singleton()->get_current_rendering_method() != export_renderer) { |
| 2109 | return vformat(TTR("The editor is currently using a different renderer than what the target platform will use. \"Shader Baker\" won't be able to include core shaders. Switch to the \"%s\" renderer temporarily to fix this."), export_renderer); |
| 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | return String(); |
| 2114 | } |
| 2115 | |
| 2116 | void EditorExportPlatformAndroid::get_export_options(List<ExportOption> *r_options) const { |
| 2117 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/debug", PROPERTY_HINT_GLOBAL_FILE, "*.apk"), "")); |
| 2118 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.apk"), "")); |
| 2119 | |
| 2120 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "gradle_build/use_gradle_build"), false, true, false)); |
| 2121 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/gradle_build_directory", PROPERTY_HINT_PLACEHOLDER_TEXT, "res://android"), "", false, false)); |
| 2122 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/android_source_template", PROPERTY_HINT_GLOBAL_FILE, "*.zip"), "")); |
| 2123 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "gradle_build/compress_native_libraries"), false, false, true)); |
| 2124 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "gradle_build/export_format", PROPERTY_HINT_ENUM, "Export APK,Export AAB"), EXPORT_FORMAT_APK, false, true)); |
| 2125 | // Using String instead of int to default to an empty string (no override) with placeholder for instructions (see GH-62465). |
| 2126 | // This implies doing validation that the string is a proper int. |
| 2127 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/min_sdk", PROPERTY_HINT_PLACEHOLDER_TEXT, vformat("%d (default)", DEFAULT_MIN_SDK_VERSION)), "", false, true)); |
| 2128 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "gradle_build/target_sdk", PROPERTY_HINT_PLACEHOLDER_TEXT, vformat("%d (default)", DEFAULT_TARGET_SDK_VERSION)), "", false, true)); |
| 2129 | |
| 2130 | r_options->push_back(ExportOption(PropertyInfo(Variant::DICTIONARY, "gradle_build/custom_theme_attributes", PROPERTY_HINT_DICTIONARY_TYPE, "String;String"), Dictionary())); |
| 2131 | |
| 2132 | #ifndef DISABLE_DEPRECATED |
| 2133 | Vector<PluginConfigAndroid> plugins_configs = get_plugins(); |
| 2134 | for (int i = 0; i < plugins_configs.size(); i++) { |
| 2135 | print_verbose("Found Android plugin " + plugins_configs[i].name); |
| 2136 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("plugins"), plugins_configs[i].name)), false)); |
| 2137 | } |
| 2138 | android_plugins_changed.clear(); |
| 2139 | #endif // DISABLE_DEPRECATED |
| 2140 | |
| 2141 | // Android supports multiple architectures in an app bundle, so |
| 2142 | // we expose each option as a checkbox in the export dialog. |
| 2143 | const Vector<ABI> abis = get_abis(); |
| 2144 | for (int i = 0; i < abis.size(); ++i) { |
| 2145 | const String abi = abis[i].abi; |
| 2146 | // All Android devices supporting Vulkan run 64-bit Android, |
| 2147 | // so there is usually no point in exporting for 32-bit Android. |
| 2148 | const bool is_default = abi == "arm64-v8a"; |
| 2149 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("architectures"), abi)), is_default)); |
| 2150 | } |
| 2151 | |
| 2152 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug", PROPERTY_HINT_GLOBAL_FILE, "*.keystore,*.jks", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), "")); |
| 2153 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug_user", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), "")); |
| 2154 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/debug_password", PROPERTY_HINT_PASSWORD, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), "")); |
| 2155 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release", PROPERTY_HINT_GLOBAL_FILE, "*.keystore,*.jks", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), "")); |
| 2156 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_user", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), "")); |
| 2157 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "keystore/release_password", PROPERTY_HINT_PASSWORD, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SECRET), "")); |
| 2158 | |
| 2159 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/code", PROPERTY_HINT_RANGE, "1,4096,1,or_greater"), 1)); |
| 2160 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "version/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Leave empty to use project version"), "")); |
| 2161 | |
| 2162 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "ext.domain.name"), "com.example.$genname", false, true)); |
| 2163 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name [default if blank]"), "")); |
| 2164 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/signed"), true)); |
| 2165 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "package/app_category", PROPERTY_HINT_ENUM, "Accessibility,Audio,Game,Image,Maps,News,Productivity,Social,Video,Undefined"), APP_CATEGORY_GAME)); |
| 2166 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/retain_data_on_uninstall"), false)); |
| 2167 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/exclude_from_recents"), false)); |
| 2168 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_in_android_tv"), false)); |
| 2169 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_in_app_library"), true)); |
| 2170 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/show_as_launcher_app"), false)); |
| 2171 | |
| 2172 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, LAUNCHER_ICON_OPTION, PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), "")); |
| 2173 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, LAUNCHER_ADAPTIVE_ICON_FOREGROUND_OPTION, PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), "")); |
| 2174 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, LAUNCHER_ADAPTIVE_ICON_BACKGROUND_OPTION, PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), "")); |
| 2175 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, LAUNCHER_ADAPTIVE_ICON_MONOCHROME_OPTION, PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), "")); |
| 2176 | |
| 2177 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "graphics/opengl_debug"), false)); |
| 2178 | |
| 2179 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "shader_baker/enabled"), false)); |
| 2180 | |
| 2181 | #ifndef XR_DISABLED |
| 2182 | r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "xr_features/xr_mode", PROPERTY_HINT_ENUM, "Regular,OpenXR"), XR_MODE_REGULAR, false, true)); |
| 2183 | #endif // XR_DISABLED |
| 2184 | |
| 2185 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "gesture/swipe_to_dismiss"), false)); |
| 2186 | |
| 2187 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/immersive_mode"), true)); |
| 2188 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/edge_to_edge"), false)); |
| 2189 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_small"), true)); |
| 2190 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_normal"), true)); |
| 2191 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_large"), true)); |
| 2192 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "screen/support_xlarge"), true)); |
| 2193 | r_options->push_back(ExportOption(PropertyInfo(Variant::COLOR, "screen/background_color", PROPERTY_HINT_COLOR_NO_ALPHA), Color())); |
| 2194 | |
| 2195 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "user_data_backup/allow"), false)); |
| 2196 | |
| 2197 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "command_line/extra_args", PROPERTY_HINT_NONE, "monospace"), "")); |
| 2198 | |
| 2199 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "apk_expansion/enable"), false, false, true)); |
| 2200 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/SALT"), "")); |
| 2201 | r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "apk_expansion/public_key", PROPERTY_HINT_MULTILINE_TEXT, "monospace,no_wrap"), "", false, true)); |
| 2202 | |
| 2203 | r_options->push_back(ExportOption(PropertyInfo(Variant::PACKED_STRING_ARRAY, "permissions/custom_permissions"), PackedStringArray())); |
| 2204 | |
| 2205 | const char **perms = ANDROID_PERMS; |
| 2206 | while (*perms) { |
| 2207 | r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("permissions"), String(*perms).to_lower())), false)); |
| 2208 | perms++; |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | bool EditorExportPlatformAndroid::get_export_option_visibility(const EditorExportPreset *p_preset, const String &p_option) const { |
| 2213 | if (p_preset == nullptr) { |
| 2214 | return true; |
| 2215 | } |
| 2216 | |
| 2217 | bool advanced_options_enabled = p_preset->are_advanced_options_enabled(); |
| 2218 | if (p_option == "graphics/opengl_debug" || |
| 2219 | p_option == "gradle_build/custom_theme_attributes" || |
| 2220 | p_option == "command_line/extra_args" || |
| 2221 | p_option == "permissions/custom_permissions" || |
| 2222 | p_option == "keystore/debug" || |
| 2223 | p_option == "keystore/debug_user" || |
| 2224 | p_option == "keystore/debug_password" || |
| 2225 | p_option == "package/retain_data_on_uninstall" || |
| 2226 | p_option == "package/exclude_from_recents" || |
| 2227 | p_option == "package/show_in_app_library" || |
| 2228 | p_option == "package/show_as_launcher_app" || |
| 2229 | p_option == "gesture/swipe_to_dismiss" || |
| 2230 | p_option == "apk_expansion/enable" || |
| 2231 | p_option == "apk_expansion/SALT" || |
| 2232 | p_option == "apk_expansion/public_key") { |
| 2233 | return advanced_options_enabled; |
| 2234 | } |
| 2235 | if (p_option == "gradle_build/gradle_build_directory" || p_option == "gradle_build/android_source_template") { |
| 2236 | return advanced_options_enabled && bool(p_preset->get("gradle_build/use_gradle_build")); |
| 2237 | } |
| 2238 | if (p_option == "custom_template/debug" || p_option == "custom_template/release") { |
| 2239 | // The APK templates are ignored if Gradle build is enabled. |
| 2240 | return advanced_options_enabled && !bool(p_preset->get("gradle_build/use_gradle_build")); |
| 2241 | } |
| 2242 | |
| 2243 | // Hide .NET embedding option (always enabled). |
| 2244 | if (p_option == "dotnet/embed_build_outputs") { |
| 2245 | return false; |
| 2246 | } |
| 2247 | |
| 2248 | if (p_option == "dotnet/android_use_linux_bionic") { |
| 2249 | return advanced_options_enabled; |
| 2250 | } |
| 2251 | return true; |
| 2252 | } |
| 2253 | |
| 2254 | String EditorExportPlatformAndroid::get_name() const { |
| 2255 | return "Android"; |
| 2256 | } |
| 2257 | |
| 2258 | String EditorExportPlatformAndroid::get_os_name() const { |
| 2259 | return "Android"; |
| 2260 | } |
| 2261 | |
| 2262 | Ref<Texture2D> EditorExportPlatformAndroid::get_logo() const { |
| 2263 | return logo; |
| 2264 | } |
| 2265 | |
| 2266 | bool EditorExportPlatformAndroid::should_update_export_options() { |
| 2267 | #ifndef DISABLE_DEPRECATED |
| 2268 | if (android_plugins_changed.is_set()) { |
| 2269 | // don't clear unless we're reporting true, to avoid race |
| 2270 | android_plugins_changed.clear(); |
| 2271 | return true; |
| 2272 | } |
| 2273 | #endif // DISABLE_DEPRECATED |
| 2274 | return false; |
| 2275 | } |
| 2276 | |
| 2277 | #ifndef ANDROID_ENABLED |
| 2278 | bool EditorExportPlatformAndroid::poll_export() { |
| 2279 | bool dc = devices_changed.is_set(); |
| 2280 | if (dc) { |
| 2281 | // don't clear unless we're reporting true, to avoid race |
| 2282 | devices_changed.clear(); |
| 2283 | } |
| 2284 | return dc; |
| 2285 | } |
| 2286 | |
| 2287 | int EditorExportPlatformAndroid::get_options_count() const { |
| 2288 | MutexLock lock(device_lock); |
| 2289 | return devices.size() + 1; |
| 2290 | } |
| 2291 | |
| 2292 | Ref<Texture2D> EditorExportPlatformAndroid::get_option_icon(int p_index) const { |
| 2293 | if (p_index == 0) { |
| 2294 | Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme(); |
| 2295 | ERR_FAIL_COND_V(theme.is_null(), Ref<ImageTexture>()); |
| 2296 | return theme->get_icon(use_scrcpy ? SNAME("GuiChecked") : SNAME("GuiUnchecked"), EditorStringName(EditorIcons)); |
| 2297 | } |
| 2298 | return EditorExportPlatform::get_option_icon(p_index - 1); |
| 2299 | } |
| 2300 | |
| 2301 | String EditorExportPlatformAndroid::get_options_tooltip() const { |
| 2302 | return TTR("Select device from the list"); |
| 2303 | } |
| 2304 | |
| 2305 | String EditorExportPlatformAndroid::get_option_label(int p_index) const { |
| 2306 | ERR_FAIL_INDEX_V(p_index, devices.size() + 1, ""); |
| 2307 | if (p_index == 0) { |
| 2308 | return TTR("Mirror Android devices"); |
| 2309 | } |
| 2310 | MutexLock lock(device_lock); |
| 2311 | return devices[p_index - 1].name; |
| 2312 | } |
| 2313 | |
| 2314 | String EditorExportPlatformAndroid::get_option_tooltip(int p_index) const { |
| 2315 | ERR_FAIL_INDEX_V(p_index, devices.size() + 1, ""); |
| 2316 | if (p_index == 0) { |
| 2317 | return TTR("If enabled, \"scrcpy\" is used to start the project and automatically stream device display (or virtual display) content."); |
| 2318 | } |
| 2319 | MutexLock lock(device_lock); |
| 2320 | String s = devices[p_index - 1].description; |
| 2321 | if (devices.size() == 1) { |
| 2322 | // Tooltip will be: |
| 2323 | // Name |
| 2324 | // Description |
| 2325 | s = devices[p_index - 1].name + "\n\n" + s; |
| 2326 | } |
| 2327 | return s; |
| 2328 | } |
| 2329 | |
| 2330 | String EditorExportPlatformAndroid::get_device_architecture(int p_index) const { |
| 2331 | ERR_FAIL_INDEX_V(p_index, devices.size() + 1, ""); |
| 2332 | if (p_index == 0) { |
| 2333 | return String(); |
| 2334 | } |
| 2335 | MutexLock lock(device_lock); |
| 2336 | return devices[p_index - 1].architecture; |
| 2337 | } |
| 2338 | |
| 2339 | Error EditorExportPlatformAndroid::run(const Ref<EditorExportPreset> &p_preset, int p_device, BitField<EditorExportPlatform::DebugFlags> p_debug_flags) { |
| 2340 | ERR_FAIL_INDEX_V(p_device, devices.size() + 1, ERR_INVALID_PARAMETER); |
| 2341 | if (p_device == 0) { |
| 2342 | use_scrcpy = !use_scrcpy; |
| 2343 | EditorSettings::get_singleton()->set_project_metadata("android", "use_scrcpy", use_scrcpy); |
| 2344 | devices_changed.set(); |
| 2345 | return ERR_SKIP; |
| 2346 | } |
| 2347 | |
| 2348 | String can_export_error; |
| 2349 | bool can_export_missing_templates; |
| 2350 | if (!can_export(p_preset, can_export_error, can_export_missing_templates)) { |
| 2351 | add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), can_export_error); |
| 2352 | return ERR_UNCONFIGURED; |
| 2353 | } |
| 2354 | |
| 2355 | MutexLock lock(device_lock); |
| 2356 | |
| 2357 | EditorProgress ep("run", vformat(TTR("Running on %s"), devices[p_device - 1].name), 3); |
| 2358 | |
| 2359 | String adb = get_adb_path(); |
| 2360 | |
| 2361 | // Export_temp APK. |
| 2362 | if (ep.step(TTR("Exporting APK..."), 0)) { |
| 2363 | return ERR_SKIP; |
| 2364 | } |
| 2365 | |
| 2366 | const bool use_wifi_for_remote_debug = EDITOR_GET("export/android/use_wifi_for_remote_debug"); |
| 2367 | const bool use_remote = p_debug_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG) || p_debug_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT); |
| 2368 | const bool use_reverse = !use_wifi_for_remote_debug; |
| 2369 | |
| 2370 | if (use_reverse) { |
| 2371 | p_debug_flags.set_flag(DEBUG_FLAG_REMOTE_DEBUG_LOCALHOST); |
| 2372 | } |
| 2373 | |
| 2374 | String tmp_export_path = EditorPaths::get_singleton()->get_temp_dir().path_join("tmpexport." + uitos(OS::get_singleton()->get_unix_time()) + ".apk"); |
| 2375 | |
| 2376 | #define CLEANUP_AND_RETURN(m_err) \ |
| 2377 | { \ |
| 2378 | DirAccess::remove_file_or_error(tmp_export_path); \ |
| 2379 | if (FileAccess::exists(tmp_export_path + ".idsig")) { \ |
| 2380 | DirAccess::remove_file_or_error(tmp_export_path + ".idsig"); \ |
| 2381 | } \ |
| 2382 | return m_err; \ |
| 2383 | } \ |
| 2384 | ((void)0) |
| 2385 | |
| 2386 | // Export to temporary APK before sending to device. |
| 2387 | Error err = export_project_helper(p_preset, true, tmp_export_path, EXPORT_FORMAT_APK, true, p_debug_flags); |
| 2388 | |
| 2389 | if (err != OK) { |
| 2390 | CLEANUP_AND_RETURN(err); |
| 2391 | } |
| 2392 | |
| 2393 | List<String> args; |
| 2394 | int rv; |
| 2395 | String output; |
| 2396 | |
| 2397 | bool remove_prev = EDITOR_GET("export/android/one_click_deploy_clear_previous_install"); |
| 2398 | String version_name = p_preset->get_version("version/name"); |
| 2399 | String package_name = p_preset->get("package/unique_name"); |
| 2400 | |
| 2401 | if (remove_prev) { |
| 2402 | if (ep.step(TTR("Uninstalling..."), 1)) { |
| 2403 | CLEANUP_AND_RETURN(ERR_SKIP); |
| 2404 | } |
| 2405 | |
| 2406 | print_line("Uninstalling previous version: " + devices[p_device - 1].name); |
| 2407 | |
| 2408 | args.push_back("-s"); |
| 2409 | args.push_back(devices[p_device - 1].id); |
| 2410 | args.push_back("uninstall"); |
| 2411 | if ((bool)EDITOR_GET("export/android/force_system_user") && devices[p_device - 1].api_level >= 17) { |
| 2412 | args.push_back("--user"); |
| 2413 | args.push_back("0"); |
| 2414 | } |
| 2415 | args.push_back(get_package_name(p_preset, package_name)); |
| 2416 | |
| 2417 | output.clear(); |
| 2418 | err = OS::get_singleton()->execute(adb, args, &output, &rv, true); |
| 2419 | print_verbose(output); |
| 2420 | } |
| 2421 | |
| 2422 | print_line("Installing to device (please wait...): " + devices[p_device - 1].name); |
| 2423 | if (ep.step(TTR("Installing to device, please wait..."), 2)) { |
| 2424 | CLEANUP_AND_RETURN(ERR_SKIP); |
| 2425 | } |
| 2426 | |
| 2427 | args.clear(); |
| 2428 | args.push_back("-s"); |
| 2429 | args.push_back(devices[p_device - 1].id); |
| 2430 | args.push_back("install"); |
| 2431 | if ((bool)EDITOR_GET("export/android/force_system_user") && devices[p_device - 1].api_level >= 17) { |
| 2432 | args.push_back("--user"); |
| 2433 | args.push_back("0"); |
| 2434 | } |
| 2435 | args.push_back("-r"); |
| 2436 | args.push_back(tmp_export_path); |
| 2437 | |
| 2438 | output.clear(); |
| 2439 | err = OS::get_singleton()->execute(adb, args, &output, &rv, true); |
| 2440 | print_verbose(output); |
| 2441 | if (err || rv != 0) { |
| 2442 | add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), vformat(TTR("Could not install to device: %s"), output)); |
| 2443 | CLEANUP_AND_RETURN(ERR_CANT_CREATE); |
| 2444 | } |
| 2445 | |
| 2446 | if (use_remote) { |
| 2447 | if (use_reverse) { |
| 2448 | static const char *const msg = "--- Debugging over USB ---"; |
| 2449 | EditorNode::get_singleton()->get_log()->add_message(msg, EditorLog::MSG_TYPE_EDITOR); |
| 2450 | print_line(String(msg).to_upper()); |
| 2451 | |
| 2452 | args.clear(); |
| 2453 | args.push_back("-s"); |
| 2454 | args.push_back(devices[p_device - 1].id); |
| 2455 | args.push_back("reverse"); |
| 2456 | args.push_back("--remove-all"); |
| 2457 | output.clear(); |
| 2458 | OS::get_singleton()->execute(adb, args, &output, &rv, true); |
| 2459 | print_verbose(output); |
| 2460 | |
| 2461 | if (p_debug_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG)) { |
| 2462 | int dbg_port = EDITOR_GET("network/debug/remote_port"); |
| 2463 | args.clear(); |
| 2464 | args.push_back("-s"); |
| 2465 | args.push_back(devices[p_device - 1].id); |
| 2466 | args.push_back("reverse"); |
| 2467 | args.push_back("tcp:" + itos(dbg_port)); |
| 2468 | args.push_back("tcp:" + itos(dbg_port)); |
| 2469 | |
| 2470 | output.clear(); |
| 2471 | OS::get_singleton()->execute(adb, args, &output, &rv, true); |
| 2472 | print_verbose(output); |
| 2473 | print_line("Reverse result: " + itos(rv)); |
| 2474 | } |
| 2475 | |
| 2476 | if (p_debug_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT)) { |
| 2477 | int fs_port = EDITOR_GET("filesystem/file_server/port"); |
| 2478 | |
| 2479 | args.clear(); |
| 2480 | args.push_back("-s"); |
| 2481 | args.push_back(devices[p_device - 1].id); |
| 2482 | args.push_back("reverse"); |
| 2483 | args.push_back("tcp:" + itos(fs_port)); |
| 2484 | args.push_back("tcp:" + itos(fs_port)); |
| 2485 | |
| 2486 | output.clear(); |
| 2487 | err = OS::get_singleton()->execute(adb, args, &output, &rv, true); |
| 2488 | print_verbose(output); |
| 2489 | print_line("Reverse result2: " + itos(rv)); |
| 2490 | } |
| 2491 | } else { |
| 2492 | static const char *const api_version_msg = "--- Debugging over Wi-Fi ---"; |
| 2493 | static const char *const manual_override_msg = "--- Wi-Fi remote debug enabled in project settings; debugging over Wi-Fi ---"; |
| 2494 | |
| 2495 | const char *const msg = use_wifi_for_remote_debug ? manual_override_msg : api_version_msg; |
| 2496 | EditorNode::get_singleton()->get_log()->add_message(msg, EditorLog::MSG_TYPE_EDITOR); |
| 2497 | print_line(String(msg).to_upper()); |
| 2498 | } |
| 2499 | } |
| 2500 | |
| 2501 | if (ep.step(TTR("Running on device..."), 3)) { |
| 2502 | CLEANUP_AND_RETURN(ERR_SKIP); |
| 2503 | } |
| 2504 | |
| 2505 | String scrcpy = "scrcpy"; |
| 2506 | if (!EDITOR_GET("export/android/scrcpy/path").operator String().is_empty()) { |
| 2507 | scrcpy = EDITOR_GET("export/android/scrcpy/path").operator String(); |
| 2508 | } |
| 2509 | |
| 2510 | args.clear(); |
| 2511 | if (use_scrcpy) { |
| 2512 | args.push_back("-s"); |
| 2513 | args.push_back(devices[p_device - 1].id); |
| 2514 | if (EDITOR_GET("export/android/scrcpy/virtual_display").operator bool()) { |
| 2515 | args.push_back("--new-display=" + EDITOR_GET("export/android/scrcpy/screen_size").operator String()); |
| 2516 | if (EDITOR_GET("export/android/scrcpy/local_ime").operator bool()) { |
| 2517 | args.push_back("--display-ime-policy=local"); |
| 2518 | } |
| 2519 | if (EDITOR_GET("export/android/scrcpy/no_decorations").operator bool()) { |
| 2520 | args.push_back("--no-vd-system-decorations"); |
| 2521 | } |
| 2522 | } |
| 2523 | args.push_back("--start-app=+" + get_package_name(p_preset, package_name)); |
| 2524 | |
| 2525 | Dictionary data = OS::get_singleton()->execute_with_pipe(scrcpy, args, false); |
| 2526 | if (!data.has("pid") || data["pid"].operator int() <= 0) { |
| 2527 | add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Could not start scrcpy executable. Configure scrcpy path in the Editor Settings (Export > Android > scrcpy > Path).")); |
| 2528 | CLEANUP_AND_RETURN(ERR_CANT_CREATE); |
| 2529 | } |
| 2530 | bool connected = false; |
| 2531 | uint64_t wait = 3000000; |
| 2532 | uint64_t time = OS::get_singleton()->get_ticks_usec(); |
| 2533 | output.clear(); |
| 2534 | String err_output; |
| 2535 | Ref<FileAccess> fa_out = data["stdio"]; |
| 2536 | Ref<FileAccess> fa_err = data["stderr"]; |
| 2537 | while (fa_out->is_open() && fa_err->is_open() && OS::get_singleton()->get_ticks_usec() - time < wait) { |
| 2538 | PackedByteArray buf; |
| 2539 | |
| 2540 | buf.resize(fa_out->get_length()); |
| 2541 | uint64_t size = fa_out->get_buffer(buf.ptrw(), buf.size()); |
| 2542 | output.append_utf8((const char *)buf.ptr(), size); |
| 2543 | |
| 2544 | buf.resize(fa_err->get_length()); |
| 2545 | size = fa_err->get_buffer(buf.ptrw(), buf.size()); |
| 2546 | err_output.append_utf8((const char *)buf.ptr(), size); |
| 2547 | |
| 2548 | if (output.contains("[server] INFO: Device:")) { |
| 2549 | connected = true; |
| 2550 | break; |
| 2551 | } |
| 2552 | } |
| 2553 | print_verbose(output); |
| 2554 | print_verbose(err_output); |
| 2555 | if (!connected) { |
| 2556 | OS::get_singleton()->kill(data["pid"].operator int()); |
| 2557 | add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Could not execute on device, scrcpy failed with the following error:\n" + err_output)); |
| 2558 | CLEANUP_AND_RETURN(ERR_CANT_CREATE); |
| 2559 | } |
| 2560 | } else { |
| 2561 | args.push_back("-s"); |
| 2562 | args.push_back(devices[p_device - 1].id); |
| 2563 | args.push_back("shell"); |
| 2564 | args.push_back("am"); |
| 2565 | args.push_back("start"); |
| 2566 | if ((bool)EDITOR_GET("export/android/force_system_user") && devices[p_device - 1].api_level >= 17) { |
| 2567 | args.push_back("--user"); |
| 2568 | args.push_back("0"); |
| 2569 | } |
| 2570 | args.push_back("-a"); |
| 2571 | args.push_back("android.intent.action.MAIN"); |
| 2572 | |
| 2573 | // Going with implicit launch first based on the LAUNCHER category and the app's package. |
| 2574 | args.push_back("-c"); |
| 2575 | args.push_back("android.intent.category.LAUNCHER"); |
| 2576 | args.push_back(get_package_name(p_preset, package_name)); |
| 2577 | |
| 2578 | output.clear(); |
| 2579 | err = OS::get_singleton()->execute(adb, args, &output, &rv, true); |
| 2580 | print_verbose(output); |
| 2581 | if (err || rv != 0 || output.contains("Error: Activity not started")) { |
| 2582 | // The implicit launch failed, let's try an explicit launch by specifying the component name before giving up. |
| 2583 | const String component_name = get_package_name(p_preset, package_name) + "/com.godot.game.GodotAppLauncher"; |
| 2584 | print_line("Implicit launch failed... Trying explicit launch using", component_name); |
| 2585 | args.erase(get_package_name(p_preset, package_name)); |
| 2586 | args.push_back("-n"); |
| 2587 | args.push_back(component_name); |
| 2588 | |
| 2589 | output.clear(); |
| 2590 | err = OS::get_singleton()->execute(adb, args, &output, &rv, true); |
| 2591 | print_verbose(output); |
| 2592 | |
| 2593 | if (err || rv != 0 || output.begins_with("Error: Activity not started")) { |
| 2594 | add_message(EXPORT_MESSAGE_ERROR, TTR("Run"), TTR("Could not execute on device.")); |
| 2595 | CLEANUP_AND_RETURN(ERR_CANT_CREATE); |
| 2596 | } |
| 2597 | } |
| 2598 | } |
| 2599 | |
| 2600 | CLEANUP_AND_RETURN(OK); |
| 2601 | #undef CLEANUP_AND_RETURN |
| 2602 | } |
| 2603 | #endif // ANDROID_ENABLED |
| 2604 | |
| 2605 | Ref<Texture2D> EditorExportPlatformAndroid::get_run_icon() const { |
| 2606 | return run_icon; |
| 2607 | } |
| 2608 | |
| 2609 | String EditorExportPlatformAndroid::get_java_path() { |
| 2610 | String exe_ext; |
| 2611 | if (OS::get_singleton()->get_name() == "Windows") { |
| 2612 | exe_ext = ".exe"; |
| 2613 | } |
| 2614 | String java_sdk_path = EDITOR_GET("export/android/java_sdk_path"); |
| 2615 | return java_sdk_path.path_join("bin/java" + exe_ext); |
| 2616 | } |
| 2617 | |
| 2618 | String EditorExportPlatformAndroid::get_keytool_path() { |
| 2619 | String exe_ext; |
| 2620 | if (OS::get_singleton()->get_name() == "Windows") { |
| 2621 | exe_ext = ".exe"; |
| 2622 | } |
| 2623 | String java_sdk_path = EDITOR_GET("export/android/java_sdk_path"); |
| 2624 | return java_sdk_path.path_join("bin/keytool" + exe_ext); |
| 2625 | } |
| 2626 | |
| 2627 | String EditorExportPlatformAndroid::get_adb_path() { |
| 2628 | String exe_ext; |
| 2629 | if (OS::get_singleton()->get_name() == "Windows") { |
| 2630 | exe_ext = ".exe"; |
| 2631 | } |
| 2632 | String sdk_path = EDITOR_GET("export/android/android_sdk_path"); |
| 2633 | return sdk_path.path_join("platform-tools/adb" + exe_ext); |
| 2634 | } |
| 2635 | |
| 2636 | String EditorExportPlatformAndroid::get_apksigner_path(int p_target_sdk, bool p_check_executes) { |
| 2637 | if (p_target_sdk == -1) { |
| 2638 | p_target_sdk = DEFAULT_TARGET_SDK_VERSION; |
| 2639 | } |
| 2640 | String exe_ext; |
| 2641 | if (OS::get_singleton()->get_name() == "Windows") { |
| 2642 | exe_ext = ".bat"; |
| 2643 | } |
| 2644 | String apksigner_command_name = "apksigner" + exe_ext; |
| 2645 | String sdk_path = EDITOR_GET("export/android/android_sdk_path"); |
| 2646 | String apksigner_path; |
| 2647 | |
| 2648 | Error errn; |
| 2649 | String build_tools_dir = sdk_path.path_join("build-tools"); |
| 2650 | Ref<DirAccess> da = DirAccess::open(build_tools_dir, &errn); |
| 2651 | if (errn != OK) { |
| 2652 | print_error("Unable to open Android 'build-tools' directory."); |
| 2653 | return apksigner_path; |
| 2654 | } |
| 2655 | |
| 2656 | // There are additional versions directories we need to go through. |
| 2657 | Vector<String> dir_list = da->get_directories(); |
| 2658 | |
| 2659 | // We need to use the version of build_tools that matches the Target SDK |
| 2660 | // If somehow we can't find that, we see if a version between 28 and the default target SDK exists. |
| 2661 | // We need to avoid versions <= 27 because they fail on Java versions >9 |
| 2662 | // If we can't find that, we just use the first valid version. |
| 2663 | Vector<String> ideal_versions; |
| 2664 | Vector<String> other_versions; |
| 2665 | Vector<String> versions; |
| 2666 | bool found_target_sdk = false; |
| 2667 | // We only allow for versions <= 27 if specifically set |
| 2668 | int min_version = p_target_sdk <= 27 ? p_target_sdk : 28; |
| 2669 | for (String sub_dir : dir_list) { |
| 2670 | if (!sub_dir.begins_with(".")) { |
| 2671 | Vector<String> ver_numbers = sub_dir.split("."); |
| 2672 | // Dir not a version number, will use as last resort |
| 2673 | if (!ver_numbers.size() || !ver_numbers[0].is_valid_int()) { |
| 2674 | other_versions.push_back(sub_dir); |
| 2675 | continue; |
| 2676 | } |
| 2677 | int ver_number = ver_numbers[0].to_int(); |
| 2678 | if (ver_number == p_target_sdk) { |
| 2679 | found_target_sdk = true; |
| 2680 | //ensure this is in front of the ones we check |
| 2681 | versions.push_back(sub_dir); |
| 2682 | } else { |
| 2683 | if (ver_number >= min_version && ver_number <= DEFAULT_TARGET_SDK_VERSION) { |
| 2684 | ideal_versions.push_back(sub_dir); |
| 2685 | } else { |
| 2686 | other_versions.push_back(sub_dir); |
| 2687 | } |
| 2688 | } |
| 2689 | } |
| 2690 | } |
| 2691 | // we will check ideal versions first, then other versions. |
| 2692 | versions.append_array(ideal_versions); |
| 2693 | versions.append_array(other_versions); |
| 2694 | |
| 2695 | if (!versions.size()) { |
| 2696 | print_error("Unable to find the 'apksigner' tool."); |
| 2697 | return apksigner_path; |
| 2698 | } |
| 2699 | |
| 2700 | int i; |
| 2701 | bool failed = false; |
| 2702 | String version_to_use; |
| 2703 | |
| 2704 | String java_sdk_path = EDITOR_GET("export/android/java_sdk_path"); |
| 2705 | if (!java_sdk_path.is_empty()) { |
| 2706 | OS::get_singleton()->set_environment("JAVA_HOME", java_sdk_path); |
| 2707 | |
| 2708 | #ifdef UNIX_ENABLED |
| 2709 | String env_path = OS::get_singleton()->get_environment("PATH"); |
| 2710 | if (!env_path.contains(java_sdk_path)) { |
| 2711 | OS::get_singleton()->set_environment("PATH", java_sdk_path + "/bin:" + env_path); |
| 2712 | } |
| 2713 | #endif |
| 2714 | } |
| 2715 | |
| 2716 | List<String> args; |
| 2717 | args.push_back("--version"); |
| 2718 | String output; |
| 2719 | int retval; |
| 2720 | Error err; |
| 2721 | for (i = 0; i < versions.size(); i++) { |
| 2722 | // Check if the tool is here. |
| 2723 | apksigner_path = build_tools_dir.path_join(versions[i]).path_join(apksigner_command_name); |
| 2724 | if (FileAccess::exists(apksigner_path)) { |
| 2725 | version_to_use = versions[i]; |
| 2726 | // If we aren't exporting, just break here. |
| 2727 | if (!p_check_executes) { |
| 2728 | break; |
| 2729 | } |
| 2730 | // we only check to see if it executes on export because it is slow to load |
| 2731 | err = OS::get_singleton()->execute(apksigner_path, args, &output, &retval, false); |
| 2732 | if (err || retval) { |
| 2733 | failed = true; |
| 2734 | } else { |
| 2735 | break; |
| 2736 | } |
| 2737 | } |
| 2738 | } |
| 2739 | if (i == versions.size()) { |
| 2740 | if (failed) { |
| 2741 | print_error("All located 'apksigner' tools in " + build_tools_dir + " failed to execute"); |
| 2742 | return "<FAILED>"; |
| 2743 | } else { |
| 2744 | print_error("Unable to find the 'apksigner' tool."); |
| 2745 | return ""; |
| 2746 | } |
| 2747 | } |
| 2748 | if (!found_target_sdk) { |
| 2749 | print_line("Could not find version of build tools that matches Target SDK, using " + version_to_use); |
| 2750 | } else if (failed && found_target_sdk) { |
| 2751 | print_line("Version of build tools that matches Target SDK failed to execute, using " + version_to_use); |
| 2752 | } |
| 2753 | |
| 2754 | return apksigner_path; |
| 2755 | } |
| 2756 | |
| 2757 | static bool has_valid_keystore_credentials(String &r_error_str, const String &p_keystore, const String &p_username, const String &p_password, const String &p_type) { |
| 2758 | String output; |
| 2759 | List<String> args; |
| 2760 | args.push_back("-list"); |
| 2761 | args.push_back("-keystore"); |
| 2762 | args.push_back(p_keystore); |
| 2763 | args.push_back("-storepass"); |
| 2764 | args.push_back(p_password); |
| 2765 | args.push_back("-alias"); |
| 2766 | args.push_back(p_username); |
| 2767 | String keytool_path = EditorExportPlatformAndroid::get_keytool_path(); |
| 2768 | Error error = OS::get_singleton()->execute(keytool_path, args, &output, nullptr, true); |
| 2769 | String keytool_error = "keytool error:"; |
| 2770 | bool valid = output.substr(0, keytool_error.length()) != keytool_error; |
| 2771 | |
| 2772 | if (error != OK) { |
| 2773 | r_error_str = TTR("Error: There was a problem validating the keystore username and password"); |
| 2774 | return false; |
| 2775 | } |
| 2776 | if (!valid) { |
| 2777 | r_error_str = TTR(p_type + " Username and/or Password is invalid for the given " + p_type + " Keystore"); |
| 2778 | return false; |
| 2779 | } |
| 2780 | r_error_str = ""; |
| 2781 | return true; |
| 2782 | } |
| 2783 | |
| 2784 | bool EditorExportPlatformAndroid::has_valid_username_and_password(const Ref<EditorExportPreset> &p_preset, String &r_error) { |
| 2785 | String dk = _get_keystore_path(p_preset, true); |
| 2786 | String dk_user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER); |
| 2787 | String dk_password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS); |
| 2788 | String rk = _get_keystore_path(p_preset, false); |
| 2789 | String rk_user = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER); |
| 2790 | String rk_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS); |
| 2791 | |
| 2792 | bool valid = true; |
| 2793 | if (!dk.is_empty() && !dk_user.is_empty() && !dk_password.is_empty()) { |
| 2794 | String err = ""; |
| 2795 | valid = has_valid_keystore_credentials(err, dk, dk_user, dk_password, "Debug"); |
| 2796 | r_error += err; |
| 2797 | } |
| 2798 | if (!rk.is_empty() && !rk_user.is_empty() && !rk_password.is_empty()) { |
| 2799 | String err = ""; |
| 2800 | valid = has_valid_keystore_credentials(err, rk, rk_user, rk_password, "Release"); |
| 2801 | r_error += err; |
| 2802 | } |
| 2803 | return valid; |
| 2804 | } |
| 2805 | |
| 2806 | #ifdef MODULE_MONO_ENABLED |
| 2807 | static uint64_t _last_validate_tfm_time = 0; |
| 2808 | static String _last_validate_tfm = ""; |
| 2809 | |
| 2810 | bool _validate_dotnet_tfm(const String &required_tfm, String &r_error) { |
| 2811 | String assembly_name = Path::get_csharp_project_name(); |
| 2812 | String project_path = ProjectSettings::get_singleton()->globalize_path("res://" + assembly_name + ".csproj"); |
| 2813 | |
| 2814 | if (!FileAccess::exists(project_path)) { |
| 2815 | return true; |
| 2816 | } |
| 2817 | |
| 2818 | uint64_t modified_time = FileAccess::get_modified_time(project_path); |
| 2819 | String tfm; |
| 2820 | |
| 2821 | if (modified_time == _last_validate_tfm_time) { |
| 2822 | tfm = _last_validate_tfm; |
| 2823 | } else { |
| 2824 | String pipe; |
| 2825 | List<String> args; |
| 2826 | args.push_back("build"); |
| 2827 | args.push_back(project_path); |
| 2828 | args.push_back("/p:GodotTargetPlatform=android"); |
| 2829 | args.push_back("--getProperty:TargetFramework"); |
| 2830 | |
| 2831 | int exitcode; |
| 2832 | Error err = OS::get_singleton()->execute("dotnet", args, &pipe, &exitcode, true); |
| 2833 | if (err != OK || exitcode != 0) { |
| 2834 | if (err != OK) { |
| 2835 | WARN_PRINT("Failed to execute dotnet command. Error " + String(error_names[err])); |
| 2836 | } else if (exitcode != 0) { |
| 2837 | print_line(pipe); |
| 2838 | WARN_PRINT("dotnet command exited with code " + itos(exitcode) + ". See output above for more details."); |
| 2839 | } |
| 2840 | r_error += vformat(TTR("Unable to determine the C# project's TFM, it may be incompatible. The export template only supports '%s'. Make sure the project targets '%s' or consider using gradle builds instead."), required_tfm, required_tfm) + "\n"; |
| 2841 | return true; |
| 2842 | } else { |
| 2843 | tfm = pipe.strip_edges(); |
| 2844 | _last_validate_tfm_time = modified_time; |
| 2845 | _last_validate_tfm = tfm; |
| 2846 | } |
| 2847 | } |
| 2848 | |
| 2849 | if (tfm != required_tfm) { |
| 2850 | r_error += vformat(TTR("C# project targets '%s' but the export template only supports '%s'. Consider using gradle builds instead."), tfm, required_tfm) + "\n"; |
| 2851 | return false; |
| 2852 | } |
| 2853 | |
| 2854 | return true; |
| 2855 | } |
| 2856 | #endif |
| 2857 | |
| 2858 | bool EditorExportPlatformAndroid::has_valid_export_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const { |
| 2859 | String err; |
| 2860 | bool valid = false; |
| 2861 | const bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 2862 | |
| 2863 | #ifdef MODULE_MONO_ENABLED |
| 2864 | // Android export is still a work in progress, keep a message as a warning. |
| 2865 | err += TTR("Exporting to Android when using C#/.NET is experimental.") + "\n"; |
| 2866 | |
| 2867 | if (!gradle_build_enabled) { |
| 2868 | // For template exports we only support .NET 9 because the template |
| 2869 | // includes .jar dependencies that may only be compatible with .NET 9. |
| 2870 | if (!_validate_dotnet_tfm("net9.0", err)) { |
| 2871 | r_error = err; |
| 2872 | return false; |
| 2873 | } |
| 2874 | } |
| 2875 | #endif |
| 2876 | |
| 2877 | // Look for export templates (first official, and if defined custom templates). |
| 2878 | |
| 2879 | if (!gradle_build_enabled) { |
| 2880 | String template_err; |
| 2881 | bool dvalid = false; |
| 2882 | bool rvalid = false; |
| 2883 | bool has_export_templates = false; |
| 2884 | |
| 2885 | if (p_preset->get("custom_template/debug") != "") { |
| 2886 | dvalid = FileAccess::exists(p_preset->get("custom_template/debug")); |
| 2887 | if (!dvalid) { |
| 2888 | template_err += TTR("Custom debug template not found.") + "\n"; |
| 2889 | } |
| 2890 | has_export_templates |= dvalid; |
| 2891 | } else { |
| 2892 | has_export_templates |= exists_export_template("android_debug.apk", &template_err); |
| 2893 | } |
| 2894 | |
| 2895 | if (p_preset->get("custom_template/release") != "") { |
| 2896 | rvalid = FileAccess::exists(p_preset->get("custom_template/release")); |
| 2897 | if (!rvalid) { |
| 2898 | template_err += TTR("Custom release template not found.") + "\n"; |
| 2899 | } |
| 2900 | has_export_templates |= rvalid; |
| 2901 | } else { |
| 2902 | has_export_templates |= exists_export_template("android_release.apk", &template_err); |
| 2903 | } |
| 2904 | |
| 2905 | r_missing_templates = !has_export_templates; |
| 2906 | valid = dvalid || rvalid || has_export_templates; |
| 2907 | if (!valid) { |
| 2908 | err += template_err; |
| 2909 | } |
| 2910 | } else { |
| 2911 | // Validate the custom gradle android source template. |
| 2912 | bool android_source_template_valid = false; |
| 2913 | const String android_source_template = p_preset->get("gradle_build/android_source_template"); |
| 2914 | if (!android_source_template.is_empty()) { |
| 2915 | android_source_template_valid = FileAccess::exists(android_source_template); |
| 2916 | if (!android_source_template_valid) { |
| 2917 | err += TTR("Custom Android source template not found.") + "\n"; |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | // Validate the installed build template. |
| 2922 | bool installed_android_build_template = FileAccess::exists(ExportTemplateManager::get_android_build_directory(p_preset).path_join("build.gradle")); |
| 2923 | if (!installed_android_build_template) { |
| 2924 | if (!android_source_template_valid) { |
| 2925 | r_missing_templates = !exists_export_template("android_source.zip", &err); |
| 2926 | } |
| 2927 | err += TTR("Android build template not installed in the project. Install it from the Project menu.") + "\n"; |
| 2928 | } else { |
| 2929 | r_missing_templates = false; |
| 2930 | } |
| 2931 | |
| 2932 | valid = installed_android_build_template && !r_missing_templates; |
| 2933 | } |
| 2934 | |
| 2935 | // Validate the rest of the export configuration. |
| 2936 | |
| 2937 | if (p_debug) { |
| 2938 | String dk = _get_keystore_path(p_preset, true); |
| 2939 | String dk_user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER); |
| 2940 | String dk_password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS); |
| 2941 | |
| 2942 | if ((dk.is_empty() || dk_user.is_empty() || dk_password.is_empty()) && (!dk.is_empty() || !dk_user.is_empty() || !dk_password.is_empty())) { |
| 2943 | valid = false; |
| 2944 | err += TTR("Either Debug Keystore, Debug User AND Debug Password settings must be configured OR none of them.") + "\n"; |
| 2945 | } |
| 2946 | |
| 2947 | // Use OR to make the export UI able to show this error. |
| 2948 | if (!dk.is_empty() && !FileAccess::exists(dk)) { |
| 2949 | dk = EDITOR_GET("export/android/debug_keystore"); |
| 2950 | if (!FileAccess::exists(dk)) { |
| 2951 | valid = false; |
| 2952 | err += TTR("Debug keystore not configured in the Editor Settings nor in the preset.") + "\n"; |
| 2953 | } |
| 2954 | } |
| 2955 | } else { |
| 2956 | String rk = _get_keystore_path(p_preset, false); |
| 2957 | String rk_user = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER); |
| 2958 | String rk_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS); |
| 2959 | |
| 2960 | if ((rk.is_empty() || rk_user.is_empty() || rk_password.is_empty()) && (!rk.is_empty() || !rk_user.is_empty() || !rk_password.is_empty())) { |
| 2961 | valid = false; |
| 2962 | err += TTR("Either Release Keystore, Release User AND Release Password settings must be configured OR none of them.") + "\n"; |
| 2963 | } |
| 2964 | |
| 2965 | if (!rk.is_empty() && !FileAccess::exists(rk)) { |
| 2966 | valid = false; |
| 2967 | err += TTR("Release keystore incorrectly configured in the export preset.") + "\n"; |
| 2968 | } |
| 2969 | } |
| 2970 | |
| 2971 | #ifndef ANDROID_ENABLED |
| 2972 | String java_sdk_path = EDITOR_GET("export/android/java_sdk_path"); |
| 2973 | if (java_sdk_path.is_empty()) { |
| 2974 | err += TTR("A valid Java SDK path is required in Editor Settings.") + "\n"; |
| 2975 | valid = false; |
| 2976 | } else { |
| 2977 | // Validate the given path by checking that `java` is present under the `bin` directory. |
| 2978 | Error errn; |
| 2979 | // Check for the bin directory. |
| 2980 | Ref<DirAccess> da = DirAccess::open(java_sdk_path.path_join("bin"), &errn); |
| 2981 | if (errn != OK) { |
| 2982 | err += TTR("Invalid Java SDK path in Editor Settings.") + " "; |
| 2983 | err += TTR("Missing 'bin' directory!"); |
| 2984 | err += "\n"; |
| 2985 | valid = false; |
| 2986 | } else { |
| 2987 | // Check for the `java` command. |
| 2988 | String java_path = get_java_path(); |
| 2989 | if (!FileAccess::exists(java_path)) { |
| 2990 | err += TTR("Unable to find 'java' command using the Java SDK path.") + " "; |
| 2991 | err += TTR("Please check the Java SDK directory specified in Editor Settings."); |
| 2992 | err += "\n"; |
| 2993 | valid = false; |
| 2994 | } |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | String sdk_path = EDITOR_GET("export/android/android_sdk_path"); |
| 2999 | if (sdk_path.is_empty()) { |
| 3000 | err += TTR("A valid Android SDK path is required in Editor Settings.") + "\n"; |
| 3001 | valid = false; |
| 3002 | } else { |
| 3003 | Error errn; |
| 3004 | // Check for the platform-tools directory. |
| 3005 | Ref<DirAccess> da = DirAccess::open(sdk_path.path_join("platform-tools"), &errn); |
| 3006 | if (errn != OK) { |
| 3007 | err += TTR("Invalid Android SDK path in Editor Settings.") + " "; |
| 3008 | err += TTR("Missing 'platform-tools' directory!"); |
| 3009 | err += "\n"; |
| 3010 | valid = false; |
| 3011 | } |
| 3012 | |
| 3013 | // Validate that adb is available. |
| 3014 | String adb_path = get_adb_path(); |
| 3015 | if (!FileAccess::exists(adb_path)) { |
| 3016 | err += TTR("Unable to find Android SDK platform-tools' adb command.") + " "; |
| 3017 | err += TTR("Please check in the Android SDK directory specified in Editor Settings."); |
| 3018 | err += "\n"; |
| 3019 | valid = false; |
| 3020 | } |
| 3021 | |
| 3022 | // Check for the build-tools directory. |
| 3023 | Ref<DirAccess> build_tools_da = DirAccess::open(sdk_path.path_join("build-tools"), &errn); |
| 3024 | if (errn != OK) { |
| 3025 | err += TTR("Invalid Android SDK path in Editor Settings.") + " "; |
| 3026 | err += TTR("Missing 'build-tools' directory!"); |
| 3027 | err += "\n"; |
| 3028 | valid = false; |
| 3029 | } |
| 3030 | |
| 3031 | String target_sdk_version = p_preset->get("gradle_build/target_sdk"); |
| 3032 | if (!target_sdk_version.is_valid_int()) { |
| 3033 | target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION); |
| 3034 | } |
| 3035 | // Validate that apksigner is available. |
| 3036 | String apksigner_path = get_apksigner_path(target_sdk_version.to_int()); |
| 3037 | if (!FileAccess::exists(apksigner_path)) { |
| 3038 | err += TTR("Unable to find Android SDK build-tools' apksigner command.") + " "; |
| 3039 | err += TTR("Please check in the Android SDK directory specified in Editor Settings."); |
| 3040 | err += "\n"; |
| 3041 | valid = false; |
| 3042 | } |
| 3043 | } |
| 3044 | #endif |
| 3045 | |
| 3046 | if (!err.is_empty()) { |
| 3047 | r_error = err; |
| 3048 | } |
| 3049 | |
| 3050 | return valid; |
| 3051 | } |
| 3052 | |
| 3053 | bool EditorExportPlatformAndroid::has_valid_project_configuration(const Ref<EditorExportPreset> &p_preset, String &r_error) const { |
| 3054 | String err; |
| 3055 | bool valid = true; |
| 3056 | |
| 3057 | List<ExportOption> options; |
| 3058 | get_export_options(&options); |
| 3059 | for (const EditorExportPlatform::ExportOption &E : options) { |
| 3060 | if (get_export_option_visibility(p_preset.ptr(), E.option.name)) { |
| 3061 | String warn = get_export_option_warning(p_preset.ptr(), E.option.name); |
| 3062 | if (!warn.is_empty()) { |
| 3063 | err += warn + "\n"; |
| 3064 | if (E.required) { |
| 3065 | valid = false; |
| 3066 | } |
| 3067 | } |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | if (!ResourceImporterTextureSettings::should_import_etc2_astc()) { |
| 3072 | valid = false; |
| 3073 | } |
| 3074 | |
| 3075 | bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); |
| 3076 | if (gradle_build_enabled) { |
| 3077 | String build_version_path = ExportTemplateManager::get_android_build_directory(p_preset).get_base_dir().path_join(".build_version"); |
| 3078 | Ref<FileAccess> f = FileAccess::open(build_version_path, FileAccess::READ); |
| 3079 | if (f.is_valid()) { |
| 3080 | String current_version = ExportTemplateManager::get_android_template_identifier(p_preset); |
| 3081 | String installed_version = f->get_line().strip_edges(); |
| 3082 | if (current_version != installed_version) { |
| 3083 | err += vformat(TTR(MISMATCHED_VERSIONS_MESSAGE), installed_version, current_version); |
| 3084 | err += "\n"; |
| 3085 | } |
| 3086 | } |
| 3087 | } else { |
| 3088 | if (_is_transparency_allowed(p_preset)) { |
| 3089 | // Warning only, so don't override `valid`. |
| 3090 | err += vformat(TTR("\"Use Gradle Build\" is required for transparent background on Android")); |
| 3091 | err += "\n"; |
| 3092 | } |
| 3093 | } |
| 3094 | |
| 3095 | String target_sdk_str = p_preset->get("gradle_build/target_sdk"); |
| 3096 | int target_sdk_int = DEFAULT_TARGET_SDK_VERSION; |
| 3097 | if (!target_sdk_str.is_empty()) { // Empty means no override, nothing to do. |
| 3098 | if (target_sdk_str.is_valid_int()) { |
| 3099 | target_sdk_int = target_sdk_str.to_int(); |
| 3100 | if (target_sdk_int > DEFAULT_TARGET_SDK_VERSION) { |
| 3101 | // Warning only, so don't override `valid`. |
| 3102 | err += vformat(TTR("\"Target SDK\" %d is higher than the default version %d. This may work, but wasn't tested and may be unstable."), target_sdk_int, DEFAULT_TARGET_SDK_VERSION); |
| 3103 | err += "\n"; |
| 3104 | } |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | String current_renderer = get_project_setting(p_preset, "rendering/renderer/rendering_method.mobile"); |
| 3109 | if (current_renderer == "forward_plus") { |
| 3110 | // Warning only, so don't override `valid`. |
| 3111 | err += vformat(TTR("The \"%s\" renderer is designed for Desktop devices, and is not suitable for Android devices."), current_renderer); |
| 3112 | err += "\n"; |
| 3113 | } |
| 3114 | |
| 3115 | String package_name = p_preset->get("package/unique_name"); |
| 3116 | if (package_name.contains("$genname") && !is_project_name_valid(p_preset)) { |
| 3117 | // Warning only, so don't override `valid`. |
| 3118 | err += vformat(TTR("The project name does not meet the requirement for the package name format and will be updated to \"%s\". Please explicitly specify the package name if needed."), get_valid_basename(p_preset)); |
| 3119 | err += "\n"; |
| 3120 | } |
| 3121 | |
| 3122 | r_error = err; |
| 3123 | return valid; |
| 3124 | } |
| 3125 | |
| 3126 | List<String> EditorExportPlatformAndroid::get_binary_extensions(const Ref<EditorExportPreset> &p_preset) const { |
| 3127 | List<String> list; |
| 3128 | int export_format = int(p_preset->get("gradle_build/export_format")); |
| 3129 | if (export_format == EXPORT_FORMAT_AAB) { |
| 3130 | list.push_back("aab"); |
| 3131 | } else { |
| 3132 | list.push_back("apk"); |
| 3133 | } |
| 3134 | return list; |
| 3135 | } |
| 3136 | |
| 3137 | String EditorExportPlatformAndroid::get_apk_expansion_fullpath(const Ref<EditorExportPreset> &p_preset, const String &p_path) { |
| 3138 | int version_code = p_preset->get("version/code"); |
| 3139 | String package_name = p_preset->get("package/unique_name"); |
| 3140 | String apk_file_name = "main." + itos(version_code) + "." + get_package_name(p_preset, package_name) + ".obb"; |
| 3141 | String fullpath = p_path.get_base_dir().path_join(apk_file_name); |
| 3142 | return fullpath; |
| 3143 | } |
| 3144 | |
| 3145 | Error EditorExportPlatformAndroid::save_apk_expansion_file(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path) { |
| 3146 | String fullpath = get_apk_expansion_fullpath(p_preset, p_path); |
| 3147 | Error err = save_pack(p_preset, p_debug, fullpath); |
| 3148 | return err; |
| 3149 | } |
| 3150 | |
| 3151 | void EditorExportPlatformAndroid::get_command_line_flags(const Ref<EditorExportPreset> &p_preset, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags, Vector<uint8_t> &r_command_line_flags) { |
| 3152 | String cmdline = p_preset->get("command_line/extra_args"); |
| 3153 | Vector<String> command_line_strings = cmdline.strip_edges().split(" "); |
| 3154 | for (int i = 0; i < command_line_strings.size(); i++) { |
| 3155 | if (command_line_strings[i].strip_edges().length() == 0) { |
| 3156 | command_line_strings.remove_at(i); |
| 3157 | i--; |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | command_line_strings.append_array(gen_export_flags(p_flags)); |
| 3162 | |
| 3163 | bool apk_expansion = p_preset->get("apk_expansion/enable"); |
| 3164 | if (apk_expansion) { |
| 3165 | String fullpath = get_apk_expansion_fullpath(p_preset, p_path); |
| 3166 | String apk_expansion_public_key = p_preset->get("apk_expansion/public_key"); |
| 3167 | |
| 3168 | command_line_strings.push_back("--use_apk_expansion"); |
| 3169 | command_line_strings.push_back("--apk_expansion_md5"); |
| 3170 | command_line_strings.push_back(FileAccess::get_md5(fullpath)); |
| 3171 | command_line_strings.push_back("--apk_expansion_key"); |
| 3172 | command_line_strings.push_back(apk_expansion_public_key.strip_edges()); |
| 3173 | } |
| 3174 | |
| 3175 | #ifndef XR_DISABLED |
| 3176 | int xr_mode_index = p_preset->get("xr_features/xr_mode"); |
| 3177 | if (xr_mode_index == XR_MODE_OPENXR) { |
| 3178 | command_line_strings.push_back("--xr_mode_openxr"); |
| 3179 | } else { // XRMode.REGULAR is the default. |
| 3180 | command_line_strings.push_back("--xr_mode_regular"); |
| 3181 | |
| 3182 | // Also override the 'xr/openxr/enabled' project setting. |
| 3183 | // This is useful for multi-platforms projects supporting both XR and non-XR devices. The project would need |
| 3184 | // to enable openxr for development, and would create multiple XR and non-XR export presets. |
| 3185 | // These command line args ensure that the non-XR export presets will have openxr disabled. |
| 3186 | command_line_strings.push_back("--xr-mode"); |
| 3187 | command_line_strings.push_back("off"); |
| 3188 | } |
| 3189 | #endif // XR_DISABLED |
| 3190 | |
| 3191 | bool immersive = p_preset->get("screen/immersive_mode"); |
| 3192 | if (immersive) { |
| 3193 | command_line_strings.push_back("--fullscreen"); |
| 3194 | } |
| 3195 | |
| 3196 | bool edge_to_edge = p_preset->get("screen/edge_to_edge"); |
| 3197 | if (edge_to_edge) { |
| 3198 | command_line_strings.push_back("--edge_to_edge"); |
| 3199 | } |
| 3200 | |
| 3201 | String background_color = "#" + p_preset->get("screen/background_color").operator Color().to_html(false); |
| 3202 | |
| 3203 | // For Gradle build, _fix_themes_xml() sets background to transparent if _is_transparency_allowed(). |
| 3204 | // Overriding to transparent here too as it's used as fallback for system bar appearance. |
| 3205 | if (_is_transparency_allowed(p_preset) && p_preset->get("gradle_build/use_gradle_build")) { |
| 3206 | background_color = "#00000000"; |
| 3207 | } |
| 3208 | command_line_strings.push_back("--background_color"); |
| 3209 | command_line_strings.push_back(background_color); |
| 3210 | |
| 3211 | bool debug_opengl = p_preset->get("graphics/opengl_debug"); |
| 3212 | if (debug_opengl) { |
| 3213 | command_line_strings.push_back("--debug_opengl"); |
| 3214 | } |
| 3215 | |
| 3216 | if (command_line_strings.size()) { |
| 3217 | r_command_line_flags.resize(4); |
| 3218 | encode_uint32(command_line_strings.size(), &r_command_line_flags.write[0]); |
| 3219 | for (int i = 0; i < command_line_strings.size(); i++) { |
| 3220 | print_line(itos(i) + " param: " + command_line_strings[i]); |
| 3221 | CharString command_line_argument = command_line_strings[i].utf8(); |
| 3222 | int base = r_command_line_flags.size(); |
| 3223 | int length = command_line_argument.length(); |
| 3224 | if (length == 0) { |
| 3225 | continue; |
| 3226 | } |
| 3227 | r_command_line_flags.resize(base + 4 + length); |
| 3228 | encode_uint32(length, &r_command_line_flags.write[base]); |
| 3229 | memcpy(&r_command_line_flags.write[base + 4], command_line_argument.ptr(), length); |
| 3230 | } |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | Error EditorExportPlatformAndroid::sign_apk(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &export_path, EditorProgress &ep) { |
| 3235 | int export_format = int(p_preset->get("gradle_build/export_format")); |
| 3236 | if (export_format == EXPORT_FORMAT_AAB) { |
| 3237 | add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("AAB signing is not supported")); |
| 3238 | return FAILED; |
| 3239 | } |
| 3240 | |
| 3241 | String keystore; |
| 3242 | String password; |
| 3243 | String user; |
| 3244 | if (p_debug) { |
| 3245 | keystore = _get_keystore_path(p_preset, true); |
| 3246 | password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS); |
| 3247 | user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER); |
| 3248 | |
| 3249 | if (keystore.is_empty()) { |
| 3250 | keystore = EDITOR_GET("export/android/debug_keystore"); |
| 3251 | password = EDITOR_GET("export/android/debug_keystore_pass"); |
| 3252 | user = EDITOR_GET("export/android/debug_keystore_user"); |
| 3253 | } |
| 3254 | |
| 3255 | if (ep.step(TTR("Signing debug APK..."), 104)) { |
| 3256 | return ERR_SKIP; |
| 3257 | } |
| 3258 | } else { |
| 3259 | keystore = _get_keystore_path(p_preset, false); |
| 3260 | password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS); |
| 3261 | user = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER); |
| 3262 | |
| 3263 | if (ep.step(TTR("Signing release APK..."), 104)) { |
| 3264 | return ERR_SKIP; |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | if (!FileAccess::exists(keystore)) { |
| 3269 | if (p_debug) { |
| 3270 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not find debug keystore, unable to export.")); |
| 3271 | } else { |
| 3272 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not find release keystore, unable to export.")); |
| 3273 | } |
| 3274 | return ERR_FILE_CANT_OPEN; |
| 3275 | } |
| 3276 | |
| 3277 | String apk_path = export_path; |
| 3278 | if (apk_path.is_relative_path()) { |
| 3279 | apk_path = OS::get_singleton()->get_resource_dir().path_join(apk_path); |
| 3280 | } |
| 3281 | apk_path = ProjectSettings::get_singleton()->globalize_path(apk_path).simplify_path(); |
| 3282 | |
| 3283 | Error err; |
| 3284 | #ifdef ANDROID_ENABLED |
| 3285 | err = OS_Android::get_singleton()->sign_apk(apk_path, apk_path, keystore, user, password); |
| 3286 | if (err != OK) { |
| 3287 | add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Unable to sign apk.")); |
| 3288 | return err; |
| 3289 | } |
| 3290 | #else |
| 3291 | String target_sdk_version = p_preset->get("gradle_build/target_sdk"); |
| 3292 | if (!target_sdk_version.is_valid_int()) { |
| 3293 | target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION); |
| 3294 | } |
| 3295 | |
| 3296 | String apksigner = get_apksigner_path(target_sdk_version.to_int(), true); |
| 3297 | print_verbose("Starting signing of the APK binary using " + apksigner); |
| 3298 | if (apksigner == "<FAILED>") { |
| 3299 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("All 'apksigner' tools located in Android SDK 'build-tools' directory failed to execute. Please check that you have the correct version installed for your target sdk version. The resulting APK is unsigned.")); |
| 3300 | return OK; |
| 3301 | } |
| 3302 | if (!FileAccess::exists(apksigner)) { |
| 3303 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("'apksigner' could not be found. Please check that the command is available in the Android SDK build-tools directory. The resulting APK is unsigned.")); |
| 3304 | return OK; |
| 3305 | } |
| 3306 | |
| 3307 | String output; |
| 3308 | List<String> args; |
| 3309 | args.push_back("sign"); |
| 3310 | args.push_back("--verbose"); |
| 3311 | args.push_back("--ks"); |
| 3312 | args.push_back(keystore); |
| 3313 | args.push_back("--ks-pass"); |
| 3314 | args.push_back("pass:" + password); |
| 3315 | args.push_back("--ks-key-alias"); |
| 3316 | args.push_back(user); |
| 3317 | args.push_back(apk_path); |
| 3318 | if (OS::get_singleton()->is_stdout_verbose() && p_debug) { |
| 3319 | // We only print verbose logs with credentials for debug builds to avoid leaking release keystore credentials. |
| 3320 | print_verbose("Signing debug binary using: " + String("\n") + apksigner + " " + join_list(args, String(" "))); |
| 3321 | } else { |
| 3322 | List<String> redacted_args = List<String>(args); |
| 3323 | redacted_args.find(keystore)->set("<REDACTED>"); |
| 3324 | redacted_args.find("pass:" + password)->set("pass:<REDACTED>"); |
| 3325 | redacted_args.find(user)->set("<REDACTED>"); |
| 3326 | print_line("Signing binary using: " + String("\n") + apksigner + " " + join_list(redacted_args, String(" "))); |
| 3327 | } |
| 3328 | int retval; |
| 3329 | err = OS::get_singleton()->execute(apksigner, args, &output, &retval, true); |
| 3330 | if (err != OK) { |
| 3331 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start apksigner executable.")); |
| 3332 | return err; |
| 3333 | } |
| 3334 | // By design, apksigner does not output credentials in its output unless --verbose is used |
| 3335 | print_line(output); |
| 3336 | if (retval) { |
| 3337 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("'apksigner' returned with error #%d"), retval)); |
| 3338 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("output: \n%s"), output)); |
| 3339 | return ERR_CANT_CREATE; |
| 3340 | } |
| 3341 | #endif |
| 3342 | |
| 3343 | if (ep.step(TTR("Verifying APK..."), 105)) { |
| 3344 | return ERR_SKIP; |
| 3345 | } |
| 3346 | |
| 3347 | #ifdef ANDROID_ENABLED |
| 3348 | err = OS_Android::get_singleton()->verify_apk(apk_path); |
| 3349 | if (err != OK) { |
| 3350 | add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Unable to verify signed apk.")); |
| 3351 | return err; |
| 3352 | } |
| 3353 | #else |
| 3354 | args.clear(); |
| 3355 | args.push_back("verify"); |
| 3356 | args.push_back("--verbose"); |
| 3357 | args.push_back(apk_path); |
| 3358 | if (p_debug) { |
| 3359 | print_verbose("Verifying signed build using: " + String("\n") + apksigner + " " + join_list(args, String(" "))); |
| 3360 | } |
| 3361 | |
| 3362 | output.clear(); |
| 3363 | err = OS::get_singleton()->execute(apksigner, args, &output, &retval, true); |
| 3364 | if (err != OK) { |
| 3365 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("Could not start apksigner executable.")); |
| 3366 | return err; |
| 3367 | } |
| 3368 | print_verbose(output); |
| 3369 | if (retval) { |
| 3370 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), TTR("'apksigner' verification of APK failed.")); |
| 3371 | add_message(EXPORT_MESSAGE_WARNING, TTR("Code Signing"), vformat(TTR("output: \n%s"), output)); |
| 3372 | return ERR_CANT_CREATE; |
| 3373 | } |
| 3374 | #endif |
| 3375 | |
| 3376 | print_verbose("Successfully completed signing build."); |
| 3377 | |
| 3378 | #ifdef ANDROID_ENABLED |
| 3379 | bool prompt_apk_install = EDITOR_GET("export/android/install_exported_apk"); |
| 3380 | if (prompt_apk_install) { |
| 3381 | OS_Android::get_singleton()->shell_open(apk_path); |
| 3382 | } |
| 3383 | #endif |
| 3384 | |
| 3385 | return OK; |
| 3386 | } |
| 3387 | |
| 3388 | void EditorExportPlatformAndroid::_clear_assets_directory(const Ref<EditorExportPreset> &p_preset) { |
| 3389 | Ref<DirAccess> da_res = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 3390 | String gradle_build_directory = ExportTemplateManager::get_android_build_directory(p_preset); |
| 3391 | |
| 3392 | // Clear the APK assets directory |
| 3393 | String apk_assets_directory = gradle_build_directory.path_join(APK_ASSETS_DIRECTORY); |
| 3394 | if (da_res->dir_exists(apk_assets_directory)) { |
| 3395 | print_verbose("Clearing APK assets directory..."); |
| 3396 | Ref<DirAccess> da_assets = DirAccess::open(apk_assets_directory); |
| 3397 | ERR_FAIL_COND(da_assets.is_null()); |
| 3398 | |
| 3399 | da_assets->erase_contents_recursive(); |
| 3400 | da_res->remove(apk_assets_directory); |
| 3401 | } |
| 3402 | |
| 3403 | // Clear the AAB assets directory |
| 3404 | String aab_assets_directory = gradle_build_directory.path_join(AAB_ASSETS_DIRECTORY); |
| 3405 | if (da_res->dir_exists(aab_assets_directory)) { |
| 3406 | print_verbose("Clearing AAB assets directory..."); |
| 3407 | Ref<DirAccess> da_assets = DirAccess::open(aab_assets_directory); |
| 3408 | ERR_FAIL_COND(da_assets.is_null()); |
| 3409 | |
| 3410 | da_assets->erase_contents_recursive(); |
| 3411 | da_res->remove(aab_assets_directory); |
| 3412 | } |
| 3413 | } |
| 3414 | |
| 3415 | void EditorExportPlatformAndroid::_remove_copied_libs(String p_gdextension_libs_path) { |
| 3416 | print_verbose("Removing previously installed libraries..."); |
| 3417 | Error error; |
| 3418 | String libs_json = FileAccess::get_file_as_string(p_gdextension_libs_path, &error); |
| 3419 | if (error || libs_json.is_empty()) { |
| 3420 | print_verbose("No previously installed libraries found"); |
| 3421 | return; |
| 3422 | } |
| 3423 | |
| 3424 | JSON json; |
| 3425 | error = json.parse(libs_json); |
| 3426 | ERR_FAIL_COND_MSG(error, "Error parsing \"" + libs_json + "\" on line " + itos(json.get_error_line()) + ": " + json.get_error_message()); |
| 3427 | |
| 3428 | Vector<String> libs = json.get_data(); |
| 3429 | Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 3430 | for (int i = 0; i < libs.size(); i++) { |
| 3431 | print_verbose("Removing previously installed library " + libs[i]); |
| 3432 | da->remove(libs[i]); |
| 3433 | } |
| 3434 | da->remove(p_gdextension_libs_path); |
| 3435 | } |
| 3436 | |
| 3437 | String EditorExportPlatformAndroid::join_list(const List<String> &p_parts, const String &p_separator) { |
| 3438 | String ret; |
| 3439 | for (List<String>::ConstIterator itr = p_parts.begin(); itr != p_parts.end(); ++itr) { |
| 3440 | if (itr != p_parts.begin()) { |
| 3441 | ret += p_separator; |
| 3442 | } |
| 3443 | ret += *itr; |
| 3444 | } |
| 3445 | return ret; |
| 3446 | } |
| 3447 | |
| 3448 | String EditorExportPlatformAndroid::join_abis(const Vector<EditorExportPlatformAndroid::ABI> &p_parts, const String &p_separator, bool p_use_arch) { |
| 3449 | String ret; |
| 3450 | for (int i = 0; i < p_parts.size(); ++i) { |
| 3451 | if (i > 0) { |
| 3452 | ret += p_separator; |
| 3453 | } |
| 3454 | ret += (p_use_arch) ? p_parts[i].arch : p_parts[i].abi; |
| 3455 | } |
| 3456 | return ret; |
| 3457 | } |
| 3458 | |
| 3459 | String EditorExportPlatformAndroid::_get_deprecated_plugins_names(const Ref<EditorExportPreset> &p_preset) const { |
| 3460 | Vector<String> names; |
| 3461 | |
| 3462 | #ifndef DISABLE_DEPRECATED |
| 3463 | PluginConfigAndroid::get_plugins_names(get_enabled_plugins(p_preset), names); |
| 3464 | #endif // DISABLE_DEPRECATED |
| 3465 | |
| 3466 | String plugins_names = String("|").join(names); |
| 3467 | return plugins_names; |
| 3468 | } |
| 3469 | |
| 3470 | String EditorExportPlatformAndroid::_get_plugins_names(const Ref<EditorExportPreset> &p_preset) const { |
| 3471 | Vector<String> names; |
| 3472 | |
| 3473 | #ifndef DISABLE_DEPRECATED |
| 3474 | PluginConfigAndroid::get_plugins_names(get_enabled_plugins(p_preset), names); |
| 3475 | #endif // DISABLE_DEPRECATED |
| 3476 | |
| 3477 | Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins(); |
| 3478 | for (int i = 0; i < export_plugins.size(); i++) { |
| 3479 | if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) { |
| 3480 | names.push_back(export_plugins[i]->get_name()); |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | String plugins_names = String("|").join(names); |
| 3485 | return plugins_names; |
| 3486 | } |
| 3487 | |
| 3488 | String EditorExportPlatformAndroid::_resolve_export_plugin_android_library_path(const String &p_android_library_path) const { |
| 3489 | String absolute_path; |
| 3490 | if (!p_android_library_path.is_empty()) { |
| 3491 | if (p_android_library_path.is_absolute_path()) { |
| 3492 | absolute_path = ProjectSettings::get_singleton()->globalize_path(p_android_library_path); |
| 3493 | } else { |
| 3494 | const String export_plugin_absolute_path = String("res://addons/").path_join(p_android_library_path); |
| 3495 | absolute_path = ProjectSettings::get_singleton()->globalize_path(export_plugin_absolute_path); |
| 3496 | } |
| 3497 | } |
| 3498 | return absolute_path; |
| 3499 | } |
| 3500 | |
| 3501 | bool EditorExportPlatformAndroid::_is_clean_build_required(const Ref<EditorExportPreset> &p_preset) { |
| 3502 | bool first_build = last_gradle_build_time == 0; |
| 3503 | bool have_plugins_changed = false; |
| 3504 | String gradle_build_dir = ExportTemplateManager::get_android_build_directory(p_preset); |
| 3505 | bool has_build_dir_changed = last_gradle_build_dir != gradle_build_dir; |
| 3506 | |
| 3507 | String plugin_names = _get_plugins_names(p_preset); |
| 3508 | |
| 3509 | if (!first_build) { |
| 3510 | have_plugins_changed = plugin_names != last_plugin_names; |
| 3511 | #ifndef DISABLE_DEPRECATED |
| 3512 | if (!have_plugins_changed) { |
| 3513 | Vector<PluginConfigAndroid> enabled_plugins = get_enabled_plugins(p_preset); |
| 3514 | for (int i = 0; i < enabled_plugins.size(); i++) { |
| 3515 | if (enabled_plugins.get(i).last_updated > last_gradle_build_time) { |
| 3516 | have_plugins_changed = true; |
| 3517 | break; |
| 3518 | } |
| 3519 | } |
| 3520 | } |
| 3521 | #endif // DISABLE_DEPRECATED |
| 3522 | } |
| 3523 | |
| 3524 | last_gradle_build_time = OS::get_singleton()->get_unix_time(); |
| 3525 | last_gradle_build_dir = gradle_build_dir; |
| 3526 | last_plugin_names = plugin_names; |
| 3527 | |
| 3528 | return have_plugins_changed || has_build_dir_changed || first_build; |
| 3529 | } |
| 3530 | |
| 3531 | Error EditorExportPlatformAndroid::export_project(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, BitField<EditorExportPlatform::DebugFlags> p_flags) { |
| 3532 | int export_format = int(p_preset->get("gradle_build/export_format")); |
| 3533 | bool should_sign = p_preset->get("package/signed"); |
| 3534 | return export_project_helper(p_preset, p_debug, p_path, export_format, should_sign, p_flags); |
| 3535 | } |
| 3536 | |
| 3537 | Error EditorExportPlatformAndroid::_generate_sparse_pck_metadata(const Ref<EditorExportPreset> &p_preset, PackData &p_pack_data, Vector<uint8_t> &r_data) { |
| 3538 | Error err; |
| 3539 | Ref<FileAccess> ftmp = FileAccess::create_temp(FileAccess::WRITE_READ, "export_index", "tmp", false, &err); |
| 3540 | if (err != OK) { |
| 3541 | add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not create temporary file!")); |
| 3542 | return err; |
| 3543 | } |
| 3544 | int64_t pck_start_pos = ftmp->get_position(); |
| 3545 | uint64_t file_base_ofs = 0; |
| 3546 | uint64_t dir_base_ofs = 0; |
| 3547 | EditorExportPlatform::_store_header(ftmp, p_preset->get_enc_pck() && p_preset->get_enc_directory(), true, file_base_ofs, dir_base_ofs); |
| 3548 | |
| 3549 | // Write directory. |
| 3550 | uint64_t dir_offset = ftmp->get_position(); |
| 3551 | ftmp->seek(dir_base_ofs); |
| 3552 | ftmp->store_64(dir_offset - pck_start_pos); |
| 3553 | ftmp->seek(dir_offset); |
| 3554 | |
| 3555 | Vector<uint8_t> key; |
| 3556 | if (p_preset->get_enc_pck() && p_preset->get_enc_directory()) { |
| 3557 | String script_key = _get_script_encryption_key(p_preset); |
| 3558 | key.resize(32); |
| 3559 | if (script_key.length() == 64) { |
| 3560 | for (int i = 0; i < 32; i++) { |
| 3561 | int v = 0; |
| 3562 | if (i * 2 < script_key.length()) { |
| 3563 | char32_t ct = script_key[i * 2]; |
| 3564 | if (is_digit(ct)) { |
| 3565 | ct = ct - '0'; |
| 3566 | } else if (ct >= 'a' && ct <= 'f') { |
| 3567 | ct = 10 + ct - 'a'; |
| 3568 | } |
| 3569 | v |= ct << 4; |
| 3570 | } |
| 3571 | |
| 3572 | if (i * 2 + 1 < script_key.length()) { |
| 3573 | char32_t ct = script_key[i * 2 + 1]; |
| 3574 | if (is_digit(ct)) { |
| 3575 | ct = ct - '0'; |
| 3576 | } else if (ct >= 'a' && ct <= 'f') { |
| 3577 | ct = 10 + ct - 'a'; |
| 3578 | } |
| 3579 | v |= ct; |
| 3580 | } |
| 3581 | key.write[i] = v; |
| 3582 | } |
| 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | if (!EditorExportPlatform::_encrypt_and_store_directory(ftmp, p_pack_data, key, p_preset->get_seed(), 0)) { |
| 3587 | add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Can't create encrypted file.")); |
| 3588 | return ERR_CANT_CREATE; |
| 3589 | } |
| 3590 | |
| 3591 | r_data.resize(ftmp->get_length()); |
| 3592 | ftmp->seek(0); |
| 3593 | ftmp->get_buffer(r_data.ptrw(), r_data.size()); |
| 3594 | ftmp.unref(); |
| 3595 | |
| 3596 | return OK; |
| 3597 | } |
| 3598 | |
| 3599 | #ifdef ANDROID_ENABLED |
| 3600 | // Copies the given keystore to temp file. |
| 3601 | // Returns the new path on success, or an empty String on failure. |
| 3602 | static String _copy_keystore_to_temp(const String &p_keystore_path, const String &p_build_path, const String &p_name) { |
| 3603 | Error err; |
| 3604 | PackedByteArray keystore_data = FileAccess::get_file_as_bytes(p_keystore_path, &err); |
| 3605 | if (err != OK) { |
| 3606 | return String(); |
| 3607 | } |
| 3608 | |
| 3609 | String temp_dir = p_build_path + "/.android"; |
| 3610 | String temp_filename = temp_dir + "/" + p_name; |
| 3611 | |
| 3612 | DirAccess::make_dir_recursive_absolute(temp_dir); |
| 3613 | Ref<FileAccess> temp_file = FileAccess::open(temp_filename, FileAccess::WRITE); |
| 3614 | if (!temp_file.is_valid()) { |
| 3615 | return String(); |
| 3616 | } |
| 3617 | |
| 3618 | temp_file->store_buffer(keystore_data); |
| 3619 | return temp_filename; |
| 3620 | } |
| 3621 | #endif |
| 3622 | |
| 3623 | Error EditorExportPlatformAndroid::export_project_helper(const Ref<EditorExportPreset> &p_preset, bool p_debug, const String &p_path, int export_format, bool should_sign, BitField<EditorExportPlatform::DebugFlags> p_flags) { |
| 3624 | ExportNotifier notifier(*this, p_preset, p_debug, p_path, p_flags); |
| 3625 | |
| 3626 | const String base_dir = p_path.get_base_dir(); |
| 3627 | if (!DirAccess::exists(base_dir)) { |
| 3628 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Target folder does not exist or is inaccessible: \"%s\""), base_dir)); |
| 3629 | return ERR_FILE_BAD_PATH; |
| 3630 | } |
| 3631 | |
| 3632 | String src_apk; |
| 3633 | Error err; |
| 3634 | |
| 3635 | EditorProgress ep("export", TTR("Exporting for Android"), 105, true); |
| 3636 | |
| 3637 | bool use_gradle_build = bool(p_preset->get("gradle_build/use_gradle_build")); |
| 3638 | String gradle_build_directory = use_gradle_build ? ExportTemplateManager::get_android_build_directory(p_preset) : ""; |
| 3639 | bool p_give_internet = p_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT) || p_flags.has_flag(DEBUG_FLAG_REMOTE_DEBUG); |
| 3640 | bool apk_expansion = p_preset->get("apk_expansion/enable"); |
| 3641 | Vector<ABI> enabled_abis = get_enabled_abis(p_preset); |
| 3642 | |
| 3643 | print_verbose("Exporting for Android..."); |
| 3644 | print_verbose("- debug build: " + bool_to_string(p_debug)); |
| 3645 | print_verbose("- export path: " + p_path); |
| 3646 | print_verbose("- export format: " + itos(export_format)); |
| 3647 | print_verbose("- sign build: " + bool_to_string(should_sign)); |
| 3648 | print_verbose("- gradle build enabled: " + bool_to_string(use_gradle_build)); |
| 3649 | print_verbose("- apk expansion enabled: " + bool_to_string(apk_expansion)); |
| 3650 | print_verbose("- enabled abis: " + join_abis(enabled_abis, ",", false)); |
| 3651 | print_verbose("- export filter: " + itos(p_preset->get_export_filter())); |
| 3652 | print_verbose("- include filter: " + p_preset->get_include_filter()); |
| 3653 | print_verbose("- exclude filter: " + p_preset->get_exclude_filter()); |
| 3654 | |
| 3655 | Ref<Image> main_image; |
| 3656 | Ref<Image> foreground; |
| 3657 | Ref<Image> background; |
| 3658 | Ref<Image> monochrome; |
| 3659 | |
| 3660 | load_icon_refs(p_preset, main_image, foreground, background, monochrome); |
| 3661 | |
| 3662 | Vector<uint8_t> command_line_flags; |
| 3663 | // Write command line flags into the command_line_flags variable. |
| 3664 | get_command_line_flags(p_preset, p_path, p_flags, command_line_flags); |
| 3665 | |
| 3666 | if (export_format == EXPORT_FORMAT_AAB) { |
| 3667 | if (!p_path.ends_with(".aab")) { |
| 3668 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Invalid filename! Android App Bundle requires the *.aab extension.")); |
| 3669 | return ERR_UNCONFIGURED; |
| 3670 | } |
| 3671 | if (apk_expansion) { |
| 3672 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("APK Expansion not compatible with Android App Bundle.")); |
| 3673 | return ERR_UNCONFIGURED; |
| 3674 | } |
| 3675 | } |
| 3676 | if (export_format == EXPORT_FORMAT_APK && !p_path.ends_with(".apk")) { |
| 3677 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Invalid filename! Android APK requires the *.apk extension.")); |
| 3678 | return ERR_UNCONFIGURED; |
| 3679 | } |
| 3680 | if (export_format > EXPORT_FORMAT_AAB || export_format < EXPORT_FORMAT_APK) { |
| 3681 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unsupported export format!")); |
| 3682 | return ERR_UNCONFIGURED; |
| 3683 | } |
| 3684 | String err_string; |
| 3685 | if (!has_valid_username_and_password(p_preset, err_string)) { |
| 3686 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR(err_string)); |
| 3687 | return ERR_UNCONFIGURED; |
| 3688 | } |
| 3689 | |
| 3690 | if (use_gradle_build) { |
| 3691 | print_verbose("Starting gradle build..."); |
| 3692 | //test that installed build version is alright |
| 3693 | { |
| 3694 | print_verbose("Checking build version..."); |
| 3695 | String gradle_base_directory = gradle_build_directory.get_base_dir(); |
| 3696 | Ref<FileAccess> f = FileAccess::open(gradle_base_directory.path_join(".build_version"), FileAccess::READ); |
| 3697 | if (f.is_null()) { |
| 3698 | 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.")); |
| 3699 | return ERR_UNCONFIGURED; |
| 3700 | } |
| 3701 | String current_version = ExportTemplateManager::get_android_template_identifier(p_preset); |
| 3702 | String installed_version = f->get_line().strip_edges(); |
| 3703 | print_verbose("- build version: " + installed_version); |
| 3704 | if (installed_version != current_version) { |
| 3705 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR(MISMATCHED_VERSIONS_MESSAGE), installed_version, current_version)); |
| 3706 | return ERR_UNCONFIGURED; |
| 3707 | } |
| 3708 | } |
| 3709 | const String assets_directory = get_assets_directory(p_preset, export_format); |
| 3710 | #ifndef ANDROID_ENABLED |
| 3711 | String java_sdk_path = EDITOR_GET("export/android/java_sdk_path"); |
| 3712 | if (java_sdk_path.is_empty()) { |
| 3713 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Java SDK path must be configured in Editor Settings at 'export/android/java_sdk_path'.")); |
| 3714 | return ERR_UNCONFIGURED; |
| 3715 | } |
| 3716 | print_verbose("Java sdk path: " + java_sdk_path); |
| 3717 | |
| 3718 | String sdk_path = EDITOR_GET("export/android/android_sdk_path"); |
| 3719 | if (sdk_path.is_empty()) { |
| 3720 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Android SDK path must be configured in Editor Settings at 'export/android/android_sdk_path'.")); |
| 3721 | return ERR_UNCONFIGURED; |
| 3722 | } |
| 3723 | print_verbose("Android sdk path: " + sdk_path); |
| 3724 | #endif |
| 3725 | |
| 3726 | // TODO: should we use "package/name" or "application/config/name"? |
| 3727 | String project_name = get_project_name(p_preset, p_preset->get("package/name")); |
| 3728 | err = _create_project_name_strings_files(p_preset, project_name, gradle_build_directory, get_project_setting(p_preset, "application/config/name_localized")); //project name localization. |
| 3729 | if (err != OK) { |
| 3730 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unable to overwrite res/*.xml files with project name.")); |
| 3731 | } |
| 3732 | // Copies the project icon files into the appropriate Gradle project directory. |
| 3733 | _copy_icons_to_gradle_project(p_preset, main_image, foreground, background, monochrome); |
| 3734 | // Write an AndroidManifest.xml file into the Gradle project directory. |
| 3735 | _write_tmp_manifest(p_preset, p_give_internet, p_debug); |
| 3736 | // Modify res/values/themes.xml file. |
| 3737 | _fix_themes_xml(p_preset); |
| 3738 | |
| 3739 | //stores all the project files inside the Gradle project directory. Also includes all ABIs |
| 3740 | _clear_assets_directory(p_preset); |
| 3741 | String gdextension_libs_path = gradle_build_directory.path_join(GDEXTENSION_LIBS_PATH); |
| 3742 | _remove_copied_libs(gdextension_libs_path); |
| 3743 | if (!apk_expansion) { |
| 3744 | print_verbose("Exporting project files..."); |
| 3745 | CustomExportData user_data; |
| 3746 | user_data.assets_directory = assets_directory; |
| 3747 | user_data.libs_directory = gradle_build_directory.path_join("libs"); |
| 3748 | user_data.debug = p_debug; |
| 3749 | if (p_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT)) { |
| 3750 | err = export_project_files(p_preset, p_debug, ignore_apk_file, nullptr, &user_data, copy_gradle_so); |
| 3751 | } else { |
| 3752 | user_data.pd.path = "assets.sparsepck"; |
| 3753 | user_data.pd.use_sparse_pck = true; |
| 3754 | err = export_project_files(p_preset, p_debug, rename_and_store_file_in_gradle_project, nullptr, &user_data, copy_gradle_so); |
| 3755 | |
| 3756 | Vector<uint8_t> enc_data; |
| 3757 | err = _generate_sparse_pck_metadata(p_preset, user_data.pd, enc_data); |
| 3758 | if (err != OK) { |
| 3759 | add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not generate sparse pck metadata!")); |
| 3760 | return err; |
| 3761 | } |
| 3762 | |
| 3763 | err = store_file_at_path(user_data.assets_directory + "/assets.sparsepck", enc_data); |
| 3764 | if (err != OK) { |
| 3765 | add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not write PCK directory!")); |
| 3766 | return err; |
| 3767 | } |
| 3768 | } |
| 3769 | if (err != OK) { |
| 3770 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not export project files to gradle project.")); |
| 3771 | return err; |
| 3772 | } |
| 3773 | if (user_data.libs.size() > 0) { |
| 3774 | Ref<FileAccess> fa = FileAccess::open(gdextension_libs_path, FileAccess::WRITE); |
| 3775 | fa->store_string(JSON::stringify(user_data.libs, "\t")); |
| 3776 | } |
| 3777 | } else { |
| 3778 | print_verbose("Saving apk expansion file..."); |
| 3779 | err = save_apk_expansion_file(p_preset, p_debug, p_path); |
| 3780 | if (err != OK) { |
| 3781 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not write expansion package file!")); |
| 3782 | return err; |
| 3783 | } |
| 3784 | } |
| 3785 | |
| 3786 | print_verbose("Storing command line flags..."); |
| 3787 | store_file_at_path(assets_directory + "/_cl_", command_line_flags); |
| 3788 | |
| 3789 | #ifndef ANDROID_ENABLED |
| 3790 | print_verbose("Updating JAVA_HOME environment to " + java_sdk_path); |
| 3791 | OS::get_singleton()->set_environment("JAVA_HOME", java_sdk_path); |
| 3792 | |
| 3793 | print_verbose("Updating ANDROID_HOME environment to " + sdk_path); |
| 3794 | OS::get_singleton()->set_environment("ANDROID_HOME", sdk_path); |
| 3795 | #endif |
| 3796 | String build_command; |
| 3797 | |
| 3798 | #ifdef WINDOWS_ENABLED |
| 3799 | build_command = "gradlew.bat"; |
| 3800 | #else |
| 3801 | build_command = "gradlew"; |
| 3802 | #endif |
| 3803 | |
| 3804 | String build_path = ProjectSettings::get_singleton()->globalize_path(gradle_build_directory); |
| 3805 | build_command = build_path.path_join(build_command); |
| 3806 | |
| 3807 | String package_name = get_package_name(p_preset, p_preset->get("package/unique_name")); |
| 3808 | String version_code = itos(p_preset->get("version/code")); |
| 3809 | String version_name = p_preset->get_version("version/name"); |
| 3810 | String min_sdk_version = p_preset->get("gradle_build/min_sdk"); |
| 3811 | if (!min_sdk_version.is_valid_int()) { |
| 3812 | min_sdk_version = itos(DEFAULT_MIN_SDK_VERSION); |
| 3813 | } |
| 3814 | String target_sdk_version = p_preset->get("gradle_build/target_sdk"); |
| 3815 | if (!target_sdk_version.is_valid_int()) { |
| 3816 | target_sdk_version = itos(DEFAULT_TARGET_SDK_VERSION); |
| 3817 | } |
| 3818 | String enabled_abi_string = join_abis(enabled_abis, "|", false); |
| 3819 | String sign_flag = bool_to_string(should_sign); |
| 3820 | String zipalign_flag = "true"; |
| 3821 | String compress_native_libraries_flag = bool_to_string(p_preset->get("gradle_build/compress_native_libraries")); |
| 3822 | |
| 3823 | Vector<String> android_libraries; |
| 3824 | Vector<String> android_dependencies; |
| 3825 | Vector<String> android_dependencies_maven_repos; |
| 3826 | |
| 3827 | #ifndef DISABLE_DEPRECATED |
| 3828 | Vector<PluginConfigAndroid> enabled_plugins = get_enabled_plugins(p_preset); |
| 3829 | PluginConfigAndroid::get_plugins_binaries(PluginConfigAndroid::BINARY_TYPE_LOCAL, enabled_plugins, android_libraries); |
| 3830 | PluginConfigAndroid::get_plugins_binaries(PluginConfigAndroid::BINARY_TYPE_REMOTE, enabled_plugins, android_dependencies); |
| 3831 | PluginConfigAndroid::get_plugins_custom_maven_repos(enabled_plugins, android_dependencies_maven_repos); |
| 3832 | #endif // DISABLE_DEPRECATED |
| 3833 | |
| 3834 | bool has_dotnet_project = false; |
| 3835 | Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins(); |
| 3836 | for (int i = 0; i < export_plugins.size(); i++) { |
| 3837 | if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) { |
| 3838 | PackedStringArray export_plugin_android_libraries = export_plugins[i]->get_android_libraries(Ref<EditorExportPlatform>(this), p_debug); |
| 3839 | for (int k = 0; k < export_plugin_android_libraries.size(); k++) { |
| 3840 | const String resolved_android_library_path = _resolve_export_plugin_android_library_path(export_plugin_android_libraries[k]); |
| 3841 | if (!resolved_android_library_path.is_empty()) { |
| 3842 | android_libraries.push_back(resolved_android_library_path); |
| 3843 | } |
| 3844 | } |
| 3845 | |
| 3846 | PackedStringArray export_plugin_android_dependencies = export_plugins[i]->get_android_dependencies(Ref<EditorExportPlatform>(this), p_debug); |
| 3847 | android_dependencies.append_array(export_plugin_android_dependencies); |
| 3848 | |
| 3849 | PackedStringArray export_plugin_android_dependencies_maven_repos = export_plugins[i]->get_android_dependencies_maven_repos(Ref<EditorExportPlatform>(this), p_debug); |
| 3850 | android_dependencies_maven_repos.append_array(export_plugin_android_dependencies_maven_repos); |
| 3851 | } |
| 3852 | |
| 3853 | PackedStringArray features = export_plugins[i]->get_export_features(Ref<EditorExportPlatform>(this), p_debug); |
| 3854 | if (features.has("dotnet")) { |
| 3855 | has_dotnet_project = true; |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | bool clean_build_required = _is_clean_build_required(p_preset); |
| 3860 | String combined_android_libraries = String("|").join(android_libraries); |
| 3861 | String combined_android_dependencies = String("|").join(android_dependencies); |
| 3862 | String combined_android_dependencies_maven_repos = String("|").join(android_dependencies_maven_repos); |
| 3863 | |
| 3864 | List<String> cmdline; |
| 3865 | cmdline.push_back("validateJavaVersion"); |
| 3866 | if (clean_build_required) { |
| 3867 | cmdline.push_back("clean"); |
| 3868 | } |
| 3869 | |
| 3870 | String edition = has_dotnet_project ? "Mono" : "Standard"; |
| 3871 | String build_type = p_debug ? "Debug" : "Release"; |
| 3872 | if (export_format == EXPORT_FORMAT_AAB) { |
| 3873 | String bundle_build_command = vformat("bundle%s%s", edition, build_type); |
| 3874 | cmdline.push_back(bundle_build_command); |
| 3875 | } else if (export_format == EXPORT_FORMAT_APK) { |
| 3876 | String apk_build_command = vformat("assemble%s%s", edition, build_type); |
| 3877 | cmdline.push_back(apk_build_command); |
| 3878 | } |
| 3879 | |
| 3880 | String addons_directory = ProjectSettings::get_singleton()->globalize_path("res://addons"); |
| 3881 | |
| 3882 | #ifndef ANDROID_ENABLED |
| 3883 | cmdline.push_back("-p"); // argument to specify the start directory. |
| 3884 | cmdline.push_back(build_path); // start directory. |
| 3885 | #endif |
| 3886 | cmdline.push_back("-Paddons_directory=" + addons_directory); // path to the addon directory as it may contain jar or aar dependencies |
| 3887 | cmdline.push_back("-Pexport_package_name=" + package_name); // argument to specify the package name. |
| 3888 | cmdline.push_back("-Pexport_version_code=" + version_code); // argument to specify the version code. |
| 3889 | cmdline.push_back("-Pexport_version_name=" + version_name); // argument to specify the version name. |
| 3890 | cmdline.push_back("-Pexport_version_min_sdk=" + min_sdk_version); // argument to specify the min sdk. |
| 3891 | cmdline.push_back("-Pexport_version_target_sdk=" + target_sdk_version); // argument to specify the target sdk. |
| 3892 | cmdline.push_back("-Pexport_enabled_abis=" + enabled_abi_string); // argument to specify enabled ABIs. |
| 3893 | cmdline.push_back("-Pplugins_local_binaries=" + combined_android_libraries); // argument to specify the list of android libraries provided by plugins. |
| 3894 | cmdline.push_back("-Pplugins_remote_binaries=" + combined_android_dependencies); // argument to specify the list of android dependencies provided by plugins. |
| 3895 | cmdline.push_back("-Pplugins_maven_repos=" + combined_android_dependencies_maven_repos); // argument to specify the list of maven repos for android dependencies provided by plugins. |
| 3896 | cmdline.push_back("-Pperform_zipalign=" + zipalign_flag); // argument to specify whether the build should be zipaligned. |
| 3897 | cmdline.push_back("-Pperform_signing=" + sign_flag); // argument to specify whether the build should be signed. |
| 3898 | cmdline.push_back("-Pcompress_native_libraries=" + compress_native_libraries_flag); // argument to specify whether the build should compress native libraries. |
| 3899 | |
| 3900 | // NOTE: The release keystore is not included in the verbose logging |
| 3901 | // to avoid accidentally leaking sensitive information when sharing verbose logs for troubleshooting. |
| 3902 | // Any non-sensitive additions to the command line arguments must be done above this section. |
| 3903 | // Sensitive additions must be done below the logging statement. |
| 3904 | print_verbose("Build Android project using gradle command: " + String("\n") + build_command + " " + join_list(cmdline, String(" "))); |
| 3905 | |
| 3906 | if (should_sign) { |
| 3907 | if (p_debug) { |
| 3908 | String debug_keystore = _get_keystore_path(p_preset, true); |
| 3909 | String debug_password = p_preset->get_or_env("keystore/debug_password", ENV_ANDROID_KEYSTORE_DEBUG_PASS); |
| 3910 | String debug_user = p_preset->get_or_env("keystore/debug_user", ENV_ANDROID_KEYSTORE_DEBUG_USER); |
| 3911 | |
| 3912 | if (debug_keystore.is_empty()) { |
| 3913 | debug_keystore = EDITOR_GET("export/android/debug_keystore"); |
| 3914 | debug_password = EDITOR_GET("export/android/debug_keystore_pass"); |
| 3915 | debug_user = EDITOR_GET("export/android/debug_keystore_user"); |
| 3916 | } |
| 3917 | if (debug_keystore.is_relative_path()) { |
| 3918 | debug_keystore = OS::get_singleton()->get_resource_dir().path_join(debug_keystore).simplify_path(); |
| 3919 | } |
| 3920 | if (!FileAccess::exists(debug_keystore)) { |
| 3921 | add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Could not find debug keystore, unable to export.")); |
| 3922 | return ERR_FILE_CANT_OPEN; |
| 3923 | } |
| 3924 | #ifdef ANDROID_ENABLED |
| 3925 | // The GABE app only has access to the project directory. |
| 3926 | // Since the keystore can be anywhere in the filesystem, so we need to copy this to temp file. |
| 3927 | String new_debug_keystore = _copy_keystore_to_temp(debug_keystore, build_path, "debug.keystore"); |
| 3928 | if (new_debug_keystore.is_empty()) { |
| 3929 | add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Failed to copy debug keystore to temp directory.")); |
| 3930 | } else { |
| 3931 | debug_keystore = new_debug_keystore; |
| 3932 | } |
| 3933 | #endif |
| 3934 | |
| 3935 | cmdline.push_back("-Pdebug_keystore_file=" + debug_keystore); // argument to specify the debug keystore file. |
| 3936 | cmdline.push_back("-Pdebug_keystore_alias=" + debug_user); // argument to specify the debug keystore alias. |
| 3937 | cmdline.push_back("-Pdebug_keystore_password=" + debug_password); // argument to specify the debug keystore password. |
| 3938 | } else { |
| 3939 | // Pass the release keystore info as well |
| 3940 | String release_keystore = _get_keystore_path(p_preset, false); |
| 3941 | String release_username = p_preset->get_or_env("keystore/release_user", ENV_ANDROID_KEYSTORE_RELEASE_USER); |
| 3942 | String release_password = p_preset->get_or_env("keystore/release_password", ENV_ANDROID_KEYSTORE_RELEASE_PASS); |
| 3943 | if (release_keystore.is_relative_path()) { |
| 3944 | release_keystore = OS::get_singleton()->get_resource_dir().path_join(release_keystore).simplify_path(); |
| 3945 | } |
| 3946 | if (!FileAccess::exists(release_keystore)) { |
| 3947 | add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Could not find release keystore, unable to export.")); |
| 3948 | return ERR_FILE_CANT_OPEN; |
| 3949 | } |
| 3950 | #ifdef ANDROID_ENABLED |
| 3951 | // The GABE app only has access to the project directory. |
| 3952 | // Since the keystore can be anywhere in the filesystem, so we need to copy this to temp file. |
| 3953 | String new_release_keystore = _copy_keystore_to_temp(release_keystore, build_path, "release.keystore"); |
| 3954 | if (new_release_keystore.is_empty()) { |
| 3955 | add_message(EXPORT_MESSAGE_ERROR, TTR("Code Signing"), TTR("Failed to copy release keystore to temp directory.")); |
| 3956 | } else { |
| 3957 | release_keystore = new_release_keystore; |
| 3958 | } |
| 3959 | #endif |
| 3960 | |
| 3961 | cmdline.push_back("-Prelease_keystore_file=" + release_keystore); // argument to specify the release keystore file. |
| 3962 | cmdline.push_back("-Prelease_keystore_alias=" + release_username); // argument to specify the release keystore alias. |
| 3963 | cmdline.push_back("-Prelease_keystore_password=" + release_password); // argument to specify the release keystore password. |
| 3964 | } |
| 3965 | } |
| 3966 | |
| 3967 | List<String> copy_args; |
| 3968 | String copy_command = "copyAndRenameBinary"; |
| 3969 | copy_args.push_back(copy_command); |
| 3970 | |
| 3971 | #ifndef ANDROID_ENABLED |
| 3972 | copy_args.push_back("-p"); // argument to specify the start directory. |
| 3973 | copy_args.push_back(build_path); // start directory. |
| 3974 | #endif |
| 3975 | |
| 3976 | copy_args.push_back("-Pexport_edition=" + edition.to_lower()); |
| 3977 | |
| 3978 | copy_args.push_back("-Pexport_build_type=" + build_type.to_lower()); |
| 3979 | |
| 3980 | String export_format_arg = export_format == EXPORT_FORMAT_AAB ? "aab" : "apk"; |
| 3981 | copy_args.push_back("-Pexport_format=" + export_format_arg); |
| 3982 | |
| 3983 | String export_filename = p_path.get_file(); |
| 3984 | String export_path = p_path.get_base_dir(); |
| 3985 | if (export_path.is_relative_path()) { |
| 3986 | export_path = OS::get_singleton()->get_resource_dir().path_join(export_path); |
| 3987 | } |
| 3988 | export_path = ProjectSettings::get_singleton()->globalize_path(export_path).simplify_path(); |
| 3989 | |
| 3990 | copy_args.push_back("-Pexport_path=file:" + export_path); |
| 3991 | copy_args.push_back("-Pexport_filename=" + export_filename); |
| 3992 | |
| 3993 | #ifdef ANDROID_ENABLED |
| 3994 | String project_path = ProjectSettings::get_singleton()->globalize_path("res://"); |
| 3995 | android_editor_gradle_runner->run_gradle( |
| 3996 | project_path, |
| 3997 | build_path.substr(project_path.length()), |
| 3998 | export_path.path_join(export_filename), |
| 3999 | export_format_arg, |
| 4000 | cmdline, |
| 4001 | copy_args); |
| 4002 | #else |
| 4003 | String build_project_output; |
| 4004 | int result = EditorNode::get_singleton()->execute_and_show_output(TTR("Building Android Project (gradle)"), build_command, cmdline, true, false, &build_project_output); |
| 4005 | if (result != 0) { |
| 4006 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Building of Android project failed, check output for the error:") + "\n\n" + build_project_output); |
| 4007 | return ERR_CANT_CREATE; |
| 4008 | } else { |
| 4009 | print_verbose(build_project_output); |
| 4010 | } |
| 4011 | |
| 4012 | print_verbose("Copying Android binary using gradle command: " + String("\n") + build_command + " " + join_list(copy_args, String(" "))); |
| 4013 | String copy_binary_output; |
| 4014 | int copy_result = EditorNode::get_singleton()->execute_and_show_output(TTR("Moving output"), build_command, copy_args, true, false, ©_binary_output); |
| 4015 | if (copy_result != 0) { |
| 4016 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Unable to copy and rename export file:") + "\n\n" + copy_binary_output); |
| 4017 | return ERR_CANT_CREATE; |
| 4018 | } else { |
| 4019 | print_verbose(copy_binary_output); |
| 4020 | } |
| 4021 | |
| 4022 | print_verbose("Successfully completed Android gradle build."); |
| 4023 | #endif |
| 4024 | return OK; |
| 4025 | } |
| 4026 | // This is the start of the Legacy build system |
| 4027 | print_verbose("Starting legacy build system..."); |
| 4028 | if (p_debug) { |
| 4029 | src_apk = p_preset->get("custom_template/debug"); |
| 4030 | } else { |
| 4031 | src_apk = p_preset->get("custom_template/release"); |
| 4032 | } |
| 4033 | src_apk = src_apk.strip_edges(); |
| 4034 | if (src_apk.is_empty()) { |
| 4035 | if (p_debug) { |
| 4036 | src_apk = find_export_template("android_debug.apk"); |
| 4037 | } else { |
| 4038 | src_apk = find_export_template("android_release.apk"); |
| 4039 | } |
| 4040 | if (src_apk.is_empty()) { |
| 4041 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(p_debug ? TTR("Debug export template not found: \"%s\".") : TTR("Release export template not found: \"%s\"."), src_apk)); |
| 4042 | return ERR_FILE_NOT_FOUND; |
| 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | Ref<FileAccess> io_fa; |
| 4047 | zlib_filefunc_def io = zipio_create_io(&io_fa); |
| 4048 | |
| 4049 | if (ep.step(TTR("Creating APK..."), 0)) { |
| 4050 | return ERR_SKIP; |
| 4051 | } |
| 4052 | |
| 4053 | unzFile pkg = unzOpen2(src_apk.utf8().get_data(), &io); |
| 4054 | if (!pkg) { |
| 4055 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not find template APK to export: \"%s\"."), src_apk)); |
| 4056 | return ERR_FILE_NOT_FOUND; |
| 4057 | } |
| 4058 | |
| 4059 | int ret = unzGoToFirstFile(pkg); |
| 4060 | |
| 4061 | Ref<FileAccess> io2_fa; |
| 4062 | zlib_filefunc_def io2 = zipio_create_io(&io2_fa); |
| 4063 | |
| 4064 | String tmp_unaligned_path = EditorPaths::get_singleton()->get_temp_dir().path_join("tmpexport-unaligned." + uitos(OS::get_singleton()->get_unix_time()) + ".apk"); |
| 4065 | |
| 4066 | #define CLEANUP_AND_RETURN(m_err) \ |
| 4067 | { \ |
| 4068 | DirAccess::remove_file_or_error(tmp_unaligned_path); \ |
| 4069 | return m_err; \ |
| 4070 | } \ |
| 4071 | ((void)0) |
| 4072 | |
| 4073 | zipFile unaligned_apk = zipOpen2(tmp_unaligned_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io2); |
| 4074 | |
| 4075 | String cmdline = p_preset->get("command_line/extra_args"); |
| 4076 | |
| 4077 | String version_name = p_preset->get_version("version/name"); |
| 4078 | String package_name = p_preset->get("package/unique_name"); |
| 4079 | |
| 4080 | String apk_expansion_pkey = p_preset->get("apk_expansion/public_key"); |
| 4081 | |
| 4082 | Vector<ABI> invalid_abis(enabled_abis); |
| 4083 | |
| 4084 | //To temporarily store icon xml data. |
| 4085 | Vector<uint8_t> themed_icon_xml_data; |
| 4086 | int icon_xml_compression_method = -1; |
| 4087 | |
| 4088 | while (ret == UNZ_OK) { |
| 4089 | //get filename |
| 4090 | unz_file_info info; |
| 4091 | char fname[16384]; |
| 4092 | ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, nullptr, 0, nullptr, 0); |
| 4093 | if (ret != UNZ_OK) { |
| 4094 | break; |
| 4095 | } |
| 4096 | |
| 4097 | bool skip = false; |
| 4098 | |
| 4099 | String file = String::utf8(fname); |
| 4100 | |
| 4101 | Vector<uint8_t> data; |
| 4102 | data.resize(info.uncompressed_size); |
| 4103 | |
| 4104 | //read |
| 4105 | unzOpenCurrentFile(pkg); |
| 4106 | unzReadCurrentFile(pkg, data.ptrw(), data.size()); |
| 4107 | unzCloseCurrentFile(pkg); |
| 4108 | |
| 4109 | //write |
| 4110 | if (file == "AndroidManifest.xml") { |
| 4111 | _fix_manifest(p_preset, data, p_give_internet); |
| 4112 | |
| 4113 | // Allow editor export plugins to update the prebuilt manifest as needed. |
| 4114 | Vector<Ref<EditorExportPlugin>> export_plugins = EditorExport::get_singleton()->get_export_plugins(); |
| 4115 | for (int i = 0; i < export_plugins.size(); i++) { |
| 4116 | if (export_plugins[i]->supports_platform(Ref<EditorExportPlatform>(this))) { |
| 4117 | PackedByteArray export_plugin_data = export_plugins[i]->update_android_prebuilt_manifest(Ref<EditorExportPlatform>(this), data); |
| 4118 | if (!export_plugin_data.is_empty()) { |
| 4119 | data = export_plugin_data; |
| 4120 | } |
| 4121 | } |
| 4122 | } |
| 4123 | } |
| 4124 | if (file == "resources.arsc") { |
| 4125 | _fix_resources(p_preset, data); |
| 4126 | } |
| 4127 | |
| 4128 | if (file == THEMED_ICON_XML_PATH) { |
| 4129 | // Store themed_icon.xml data. |
| 4130 | themed_icon_xml_data = data; |
| 4131 | skip = true; |
| 4132 | } |
| 4133 | |
| 4134 | if (file == ICON_XML_PATH) { |
| 4135 | if (monochrome.is_valid() && !monochrome->is_empty()) { |
| 4136 | // Defer processing of icon.xml until after themed_icon.xml is read. |
| 4137 | icon_xml_compression_method = info.compression_method; |
| 4138 | skip = true; |
| 4139 | } |
| 4140 | } |
| 4141 | |
| 4142 | if ((file.ends_with(".webp") || file.ends_with(".png")) && file.contains("mipmap")) { |
| 4143 | for (int i = 0; i < ICON_DENSITIES_COUNT; ++i) { |
| 4144 | if (main_image.is_valid() && !main_image->is_empty()) { |
| 4145 | if (file == LAUNCHER_ICONS[i].export_path) { |
| 4146 | _process_launcher_icons(file, main_image, LAUNCHER_ICONS[i].dimensions, data); |
| 4147 | } |
| 4148 | } |
| 4149 | if (foreground.is_valid() && !foreground->is_empty()) { |
| 4150 | if (file == LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].export_path) { |
| 4151 | _process_launcher_icons(file, foreground, LAUNCHER_ADAPTIVE_ICON_FOREGROUNDS[i].dimensions, data); |
| 4152 | } |
| 4153 | } |
| 4154 | if (background.is_valid() && !background->is_empty()) { |
| 4155 | if (file == LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].export_path) { |
| 4156 | _process_launcher_icons(file, background, LAUNCHER_ADAPTIVE_ICON_BACKGROUNDS[i].dimensions, data); |
| 4157 | } |
| 4158 | } |
| 4159 | if (monochrome.is_valid() && !monochrome->is_empty()) { |
| 4160 | if (file == LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].export_path) { |
| 4161 | _process_launcher_icons(file, monochrome, LAUNCHER_ADAPTIVE_ICON_MONOCHROMES[i].dimensions, data); |
| 4162 | } |
| 4163 | } |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | if (file.ends_with(".so")) { |
| 4168 | bool enabled = false; |
| 4169 | for (int i = 0; i < enabled_abis.size(); ++i) { |
| 4170 | if (file.begins_with("lib/" + enabled_abis[i].abi + "/")) { |
| 4171 | invalid_abis.erase(enabled_abis[i]); |
| 4172 | enabled = true; |
| 4173 | break; |
| 4174 | } |
| 4175 | } |
| 4176 | if (!enabled) { |
| 4177 | skip = true; |
| 4178 | } |
| 4179 | } |
| 4180 | |
| 4181 | if (file.begins_with("META-INF") && should_sign) { |
| 4182 | skip = true; |
| 4183 | } |
| 4184 | |
| 4185 | if (!skip) { |
| 4186 | print_line("ADDING: " + file); |
| 4187 | |
| 4188 | // Respect decision on compression made by AAPT for the export template |
| 4189 | const bool uncompressed = info.compression_method == 0; |
| 4190 | |
| 4191 | zip_fileinfo zipfi = get_zip_fileinfo(); |
| 4192 | |
| 4193 | zipOpenNewFileInZip(unaligned_apk, |
| 4194 | file.utf8().get_data(), |
| 4195 | &zipfi, |
| 4196 | nullptr, |
| 4197 | 0, |
| 4198 | nullptr, |
| 4199 | 0, |
| 4200 | nullptr, |
| 4201 | uncompressed ? 0 : Z_DEFLATED, |
| 4202 | Z_DEFAULT_COMPRESSION); |
| 4203 | |
| 4204 | zipWriteInFileInZip(unaligned_apk, data.ptr(), data.size()); |
| 4205 | zipCloseFileInZip(unaligned_apk); |
| 4206 | } |
| 4207 | |
| 4208 | ret = unzGoToNextFile(pkg); |
| 4209 | } |
| 4210 | |
| 4211 | // Process deferred icon.xml and replace it's data with themed_icon.xml. |
| 4212 | if (monochrome.is_valid() && !monochrome->is_empty()) { |
| 4213 | print_line("ADDING: " + ICON_XML_PATH + " (replacing with themed_icon.xml data)"); |
| 4214 | |
| 4215 | const bool uncompressed = icon_xml_compression_method == 0; |
| 4216 | zip_fileinfo zipfi = get_zip_fileinfo(); |
| 4217 | |
| 4218 | zipOpenNewFileInZip(unaligned_apk, |
| 4219 | ICON_XML_PATH.utf8().get_data(), |
| 4220 | &zipfi, |
| 4221 | nullptr, |
| 4222 | 0, |
| 4223 | nullptr, |
| 4224 | 0, |
| 4225 | nullptr, |
| 4226 | uncompressed ? 0 : Z_DEFLATED, |
| 4227 | Z_DEFAULT_COMPRESSION); |
| 4228 | |
| 4229 | zipWriteInFileInZip(unaligned_apk, themed_icon_xml_data.ptr(), themed_icon_xml_data.size()); |
| 4230 | zipCloseFileInZip(unaligned_apk); |
| 4231 | } |
| 4232 | |
| 4233 | if (!invalid_abis.is_empty()) { |
| 4234 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Missing libraries in the export template for the selected architectures: %s. Please build a template with all required libraries, or uncheck the missing architectures in the export preset."), join_abis(invalid_abis, ", ", false))); |
| 4235 | CLEANUP_AND_RETURN(ERR_FILE_NOT_FOUND); |
| 4236 | } |
| 4237 | |
| 4238 | if (ep.step(TTR("Adding files..."), 1)) { |
| 4239 | CLEANUP_AND_RETURN(ERR_SKIP); |
| 4240 | } |
| 4241 | err = OK; |
| 4242 | |
| 4243 | if (p_flags.has_flag(DEBUG_FLAG_DUMB_CLIENT)) { |
| 4244 | APKExportData ed; |
| 4245 | ed.ep = &ep; |
| 4246 | ed.apk = unaligned_apk; |
| 4247 | err = export_project_files(p_preset, p_debug, ignore_apk_file, nullptr, &ed, save_apk_so); |
| 4248 | } else { |
| 4249 | if (apk_expansion) { |
| 4250 | err = save_apk_expansion_file(p_preset, p_debug, p_path); |
| 4251 | if (err != OK) { |
| 4252 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), TTR("Could not write expansion package file!")); |
| 4253 | return err; |
| 4254 | } |
| 4255 | } else { |
| 4256 | APKExportData ed; |
| 4257 | ed.ep = &ep; |
| 4258 | ed.apk = unaligned_apk; |
| 4259 | ed.pd.path = "assets.sparsepck"; |
| 4260 | ed.pd.use_sparse_pck = true; |
| 4261 | err = export_project_files(p_preset, p_debug, save_apk_file, nullptr, &ed, save_apk_so); |
| 4262 | |
| 4263 | Vector<uint8_t> enc_data; |
| 4264 | err = _generate_sparse_pck_metadata(p_preset, ed.pd, enc_data); |
| 4265 | if (err != OK) { |
| 4266 | add_message(EXPORT_MESSAGE_ERROR, TTR("Save PCK"), TTR("Could not generate sparse pck metadata!")); |
| 4267 | return err; |
| 4268 | } |
| 4269 | |
| 4270 | store_in_apk(&ed, "assets/assets.sparsepck", enc_data, 0); |
| 4271 | } |
| 4272 | } |
| 4273 | |
| 4274 | if (err != OK) { |
| 4275 | unzClose(pkg); |
| 4276 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not export project files."))); |
| 4277 | CLEANUP_AND_RETURN(ERR_SKIP); |
| 4278 | } |
| 4279 | |
| 4280 | zip_fileinfo zipfi = get_zip_fileinfo(); |
| 4281 | zipOpenNewFileInZip(unaligned_apk, |
| 4282 | "assets/_cl_", |
| 4283 | &zipfi, |
| 4284 | nullptr, |
| 4285 | 0, |
| 4286 | nullptr, |
| 4287 | 0, |
| 4288 | nullptr, |
| 4289 | 0, // No compress (little size gain and potentially slower startup) |
| 4290 | Z_DEFAULT_COMPRESSION); |
| 4291 | zipWriteInFileInZip(unaligned_apk, command_line_flags.ptr(), command_line_flags.size()); |
| 4292 | zipCloseFileInZip(unaligned_apk); |
| 4293 | zipClose(unaligned_apk, nullptr); |
| 4294 | unzClose(pkg); |
| 4295 | |
| 4296 | // Let's zip-align (must be done before signing) |
| 4297 | |
| 4298 | static const int PAGE_SIZE_KB = 16 * 1024; |
| 4299 | static const int ZIP_ALIGNMENT = 4; |
| 4300 | |
| 4301 | // If we're not signing the apk, then the next step should be the last. |
| 4302 | const int next_step = should_sign ? 103 : 105; |
| 4303 | if (ep.step(TTR("Aligning APK..."), next_step)) { |
| 4304 | CLEANUP_AND_RETURN(ERR_SKIP); |
| 4305 | } |
| 4306 | |
| 4307 | unzFile tmp_unaligned = unzOpen2(tmp_unaligned_path.utf8().get_data(), &io); |
| 4308 | if (!tmp_unaligned) { |
| 4309 | add_message(EXPORT_MESSAGE_ERROR, TTR("Export"), vformat(TTR("Could not unzip temporary unaligned APK."))); |
| 4310 | CLEANUP_AND_RETURN(ERR_FILE_NOT_FOUND); |
| 4311 | } |
| 4312 | |
| 4313 | ret = unzGoToFirstFile(tmp_unaligned); |
| 4314 | |
| 4315 | io2 = zipio_create_io(&io2_fa); |
| 4316 | zipFile final_apk = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io2); |
| 4317 | |
| 4318 | // Take files from the unaligned APK and write them out to the aligned one |
| 4319 | // in raw mode, i.e. not uncompressing and recompressing, aligning them as needed, |
| 4320 | // following what is done in https://github.com/android/platform_build/blob/master/tools/zipalign/ZipAlign.cpp |
| 4321 | int bias = 0; |
| 4322 | while (ret == UNZ_OK) { |
| 4323 | unz_file_info info; |
| 4324 | memset(&info, 0, sizeof(info)); |
| 4325 | |
| 4326 | char fname[16384]; |
| 4327 | char extra[16384]; |
| 4328 | ret = unzGetCurrentFileInfo(tmp_unaligned, &info, fname, 16384, extra, 16384 - ZIP_ALIGNMENT, nullptr, 0); |
| 4329 | if (ret != UNZ_OK) { |
| 4330 | break; |
| 4331 | } |
| 4332 | |
| 4333 | String file = String::utf8(fname); |
| 4334 | |
| 4335 | Vector<uint8_t> data; |
| 4336 | data.resize(info.compressed_size); |
| 4337 | |
| 4338 | // read |
| 4339 | int method, level; |
| 4340 | unzOpenCurrentFile2(tmp_unaligned, &method, &level, 1); // raw read |
| 4341 | long file_offset = unzGetCurrentFileZStreamPos64(tmp_unaligned); |
| 4342 | unzReadCurrentFile(tmp_unaligned, data.ptrw(), data.size()); |
| 4343 | unzCloseCurrentFile(tmp_unaligned); |
| 4344 | |
| 4345 | // align |
| 4346 | int padding = 0; |
| 4347 | if (!info.compression_method) { |
| 4348 | // Uncompressed file => Align |
| 4349 | long new_offset = file_offset + bias; |
| 4350 | const char *ext = strrchr(fname, '.'); |
| 4351 | if (ext && strcmp(ext, ".so") == 0) { |
| 4352 | padding = (PAGE_SIZE_KB - (new_offset % PAGE_SIZE_KB)) % PAGE_SIZE_KB; |
| 4353 | } else { |
| 4354 | padding = (ZIP_ALIGNMENT - (new_offset % ZIP_ALIGNMENT)) % ZIP_ALIGNMENT; |
| 4355 | } |
| 4356 | } |
| 4357 | |
| 4358 | memset(extra + info.size_file_extra, 0, padding); |
| 4359 | |
| 4360 | zip_fileinfo fileinfo = get_zip_fileinfo(); |
| 4361 | zipOpenNewFileInZip2(final_apk, |
| 4362 | file.utf8().get_data(), |
| 4363 | &fileinfo, |
| 4364 | extra, |
| 4365 | info.size_file_extra + padding, |
| 4366 | nullptr, |
| 4367 | 0, |
| 4368 | nullptr, |
| 4369 | method, |
| 4370 | level, |
| 4371 | 1); // raw write |
| 4372 | zipWriteInFileInZip(final_apk, data.ptr(), data.size()); |
| 4373 | zipCloseFileInZipRaw(final_apk, info.uncompressed_size, info.crc); |
| 4374 | |
| 4375 | bias += padding; |
| 4376 | |
| 4377 | ret = unzGoToNextFile(tmp_unaligned); |
| 4378 | } |
| 4379 | |
| 4380 | zipClose(final_apk, nullptr); |
| 4381 | unzClose(tmp_unaligned); |
| 4382 | |
| 4383 | if (should_sign) { |
| 4384 | // Signing must be done last as any additional modifications to the |
| 4385 | // file will invalidate the signature. |
| 4386 | err = sign_apk(p_preset, p_debug, p_path, ep); |
| 4387 | if (err != OK) { |
| 4388 | // Message is supplied by the subroutine method. |
| 4389 | CLEANUP_AND_RETURN(err); |
| 4390 | } |
| 4391 | } |
| 4392 | |
| 4393 | CLEANUP_AND_RETURN(OK); |
| 4394 | } |
| 4395 | |
| 4396 | void EditorExportPlatformAndroid::get_platform_features(List<String> *r_features) const { |
| 4397 | r_features->push_back("mobile"); |
| 4398 | r_features->push_back("android"); |
| 4399 | } |
| 4400 | |
| 4401 | void EditorExportPlatformAndroid::resolve_platform_feature_priorities(const Ref<EditorExportPreset> &p_preset, HashSet<String> &p_features) { |
| 4402 | } |
| 4403 | |
| 4404 | void EditorExportPlatformAndroid::initialize() { |
| 4405 | if (EditorNode::get_singleton()) { |
| 4406 | Ref<Image> img = memnew(Image); |
| 4407 | const bool upsample = !Math::is_equal_approx(Math::round(EDSCALE), EDSCALE); |
| 4408 | |
| 4409 | ImageLoaderSVG::create_image_from_string(img, _android_logo_svg, EDSCALE, upsample, false); |
| 4410 | logo = ImageTexture::create_from_image(img); |
| 4411 | |
| 4412 | ImageLoaderSVG::create_image_from_string(img, _android_run_icon_svg, EDSCALE, upsample, false); |
| 4413 | run_icon = ImageTexture::create_from_image(img); |
| 4414 | |
| 4415 | #ifndef DISABLE_DEPRECATED |
| 4416 | android_plugins_changed.set(); |
| 4417 | #endif // DISABLE_DEPRECATED |
| 4418 | #ifndef ANDROID_ENABLED |
| 4419 | devices_changed.set(); |
| 4420 | _create_editor_debug_keystore_if_needed(); |
| 4421 | _update_preset_status(); |
| 4422 | check_for_changes_thread.start(_check_for_changes_poll_thread, this); |
| 4423 | use_scrcpy = EditorSettings::get_singleton()->get_project_metadata("android", "use_scrcpy", false); |
| 4424 | #else // ANDROID_ENABLED |
| 4425 | android_editor_gradle_runner = memnew(AndroidEditorGradleRunner); |
| 4426 | #endif // ANDROID_ENABLED |
| 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | EditorExportPlatformAndroid::~EditorExportPlatformAndroid() { |
| 4431 | #ifndef ANDROID_ENABLED |
| 4432 | quit_request.set(); |
| 4433 | if (check_for_changes_thread.is_started()) { |
| 4434 | check_for_changes_thread.wait_to_finish(); |
| 4435 | } |
| 4436 | #else |
| 4437 | if (android_editor_gradle_runner) { |
| 4438 | memdelete(android_editor_gradle_runner); |
| 4439 | } |
| 4440 | #endif |
| 4441 | } |