From 35a98d305bf4133fdfc33c18d269226bff5aa416 Mon Sep 17 00:00:00 2001 From: ne0fhyk Date: Thu, 9 Sep 2021 19:55:42 -0700 Subject: [PATCH] Add support for Play Asset Delivery. This only adds support for a subset of Play Asset Delivery: this causes a single install-time asset pack to always be present, but doesn't add support for dynamically downloaded asset packs. --- platform/android/export/export_plugin.cpp | 30 +++++++++++++++---- platform/android/export/export_plugin.h | 7 ++--- .../android/export/gradle_export_util.cpp | 3 +- platform/android/export/gradle_export_util.h | 6 ++++ .../app/assetPacks/installTime/build.gradle | 8 +++++ platform/android/java/app/build.gradle | 2 ++ platform/android/java/app/settings.gradle | 4 +-- platform/android/java/settings.gradle | 3 ++ 8 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 platform/android/java/app/assetPacks/installTime/build.gradle diff --git a/platform/android/export/export_plugin.cpp b/platform/android/export/export_plugin.cpp index 7ff43cd7baa6..e73c274b0938 100644 --- a/platform/android/export/export_plugin.cpp +++ b/platform/android/export/export_plugin.cpp @@ -221,6 +221,9 @@ static const LauncherIcon launcher_adaptive_icon_backgrounds[icon_densities_coun static const int EXPORT_FORMAT_APK = 0; static const int EXPORT_FORMAT_AAB = 1; +static const char *APK_ASSETS_DIRECTORY = "res://android/build/assets"; +static const char *AAB_ASSETS_DIRECTORY = "res://android/build/assetPacks/installTime/src/main/assets"; + void EditorExportPlatformAndroid::_check_for_changes_poll_thread(void *ud) { EditorExportPlatformAndroid *ea = (EditorExportPlatformAndroid *)ud; @@ -430,6 +433,11 @@ String EditorExportPlatformAndroid::get_package_name(const String &p_package) co return pname; } +String EditorExportPlatformAndroid::get_assets_directory(const Ref &p_preset) const { + int export_format = int(p_preset->get("custom_template/export_format")); + return export_format == EXPORT_FORMAT_AAB ? AAB_ASSETS_DIRECTORY : APK_ASSETS_DIRECTORY; +} + bool EditorExportPlatformAndroid::is_package_name_valid(const String &p_package, String *r_error) const { String pname = p_package; @@ -2659,11 +2667,21 @@ Error EditorExportPlatformAndroid::sign_apk(const Ref &p_pre void EditorExportPlatformAndroid::_clear_assets_directory() { DirAccessRef da_res = DirAccess::create(DirAccess::ACCESS_RESOURCES); - if (da_res->dir_exists("res://android/build/assets")) { - print_verbose("Clearing assets directory.."); - DirAccessRef da_assets = DirAccess::open("res://android/build/assets"); + + // Clear the APK assets directory + if (da_res->dir_exists(APK_ASSETS_DIRECTORY)) { + print_verbose("Clearing APK assets directory.."); + DirAccessRef da_assets = DirAccess::open(APK_ASSETS_DIRECTORY); + da_assets->erase_contents_recursive(); + da_res->remove(APK_ASSETS_DIRECTORY); + } + + // Clear the AAB assets directory + if (da_res->dir_exists(AAB_ASSETS_DIRECTORY)) { + print_verbose("Clearing AAB assets directory.."); + DirAccessRef da_assets = DirAccess::open(AAB_ASSETS_DIRECTORY); da_assets->erase_contents_recursive(); - da_res->remove("res://android/build/assets"); + da_res->remove(AAB_ASSETS_DIRECTORY); } } @@ -2785,6 +2803,7 @@ Error EditorExportPlatformAndroid::export_project_helper(const Refset_environment("ANDROID_HOME", sdk_path); //set and overwrite if required diff --git a/platform/android/export/export_plugin.h b/platform/android/export/export_plugin.h index 8fcad419c25a..4f3612d34f5b 100644 --- a/platform/android/export/export_plugin.h +++ b/platform/android/export/export_plugin.h @@ -87,11 +87,6 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { EditorProgress *ep; }; - struct CustomExportData { - bool debug; - Vector libs; - }; - Vector plugins; String last_plugin_names; uint64_t last_custom_build_time = 0; @@ -109,6 +104,8 @@ class EditorExportPlatformAndroid : public EditorExportPlatform { String get_package_name(const String &p_package) const; + String get_assets_directory(const Ref &p_preset) const; + bool is_package_name_valid(const String &p_package, String *r_error = nullptr) const; static bool _should_compress_asset(const String &p_path, const Vector &p_data); diff --git a/platform/android/export/gradle_export_util.cpp b/platform/android/export/gradle_export_util.cpp index a946550eac2e..2920833827ac 100644 --- a/platform/android/export/gradle_export_util.cpp +++ b/platform/android/export/gradle_export_util.cpp @@ -121,7 +121,8 @@ Error store_string_at_path(const String &p_path, const String &p_data) { // It's functionality mirrors that of the method save_apk_file. // This method will be called ONLY when custom build is enabled. Error rename_and_store_file_in_gradle_project(void *p_userdata, const String &p_path, const Vector &p_data, int p_file, int p_total) { - String dst_path = p_path.replace_first("res://", "res://android/build/assets/"); + CustomExportData *export_data = (CustomExportData *)p_userdata; + String dst_path = p_path.replace_first("res://", export_data->assets_directory + "/"); print_verbose("Saving project files from " + p_path + " into " + dst_path); Error err = store_file_at_path(dst_path, p_data); return err; diff --git a/platform/android/export/gradle_export_util.h b/platform/android/export/gradle_export_util.h index 32ad8a9a84e9..924a53b6442a 100644 --- a/platform/android/export/gradle_export_util.h +++ b/platform/android/export/gradle_export_util.h @@ -44,6 +44,12 @@ const String godot_project_name_xml_string = R"( )"; +struct CustomExportData { + String assets_directory; + bool debug; + Vector libs; +}; + int _get_android_orientation_value(OS::ScreenOrientation screen_orientation); String _get_android_orientation_label(OS::ScreenOrientation screen_orientation); diff --git a/platform/android/java/app/assetPacks/installTime/build.gradle b/platform/android/java/app/assetPacks/installTime/build.gradle new file mode 100644 index 000000000000..b06faac37441 --- /dev/null +++ b/platform/android/java/app/assetPacks/installTime/build.gradle @@ -0,0 +1,8 @@ +apply plugin: 'com.android.asset-pack' + +assetPack { + packName = "installTime" // Directory name for the asset pack + dynamicDelivery { + deliveryType = "install-time" // Delivery mode + } +} diff --git a/platform/android/java/app/build.gradle b/platform/android/java/app/build.gradle index 9bf69bd59ac1..856f23ad8e84 100644 --- a/platform/android/java/app/build.gradle +++ b/platform/android/java/app/build.gradle @@ -85,6 +85,8 @@ android { targetCompatibility versions.javaVersion } + assetPacks = [":assetPacks:installTime"] + defaultConfig { // The default ignore pattern for the 'assets' directory includes hidden files and directories which are used by Godot projects. aaptOptions { diff --git a/platform/android/java/app/settings.gradle b/platform/android/java/app/settings.gradle index 33b863c7bf3a..e38d7b2ba62e 100644 --- a/platform/android/java/app/settings.gradle +++ b/platform/android/java/app/settings.gradle @@ -1,2 +1,2 @@ -// Empty settings.gradle file to denote this directory as being the root project -// of the Godot custom build. +// This is the root directory of the Godot custom build. +include ':assetPacks:installTime' diff --git a/platform/android/java/settings.gradle b/platform/android/java/settings.gradle index 524031d93f46..584b62690098 100644 --- a/platform/android/java/settings.gradle +++ b/platform/android/java/settings.gradle @@ -4,3 +4,6 @@ rootProject.name = "Godot" include ':app' include ':lib' include ':nativeSrcsConfigs' + +include ':assetPacks:installTime' +project(':assetPacks:installTime').projectDir = file("app/assetPacks/installTime")