From a69a585c91f70e4e3443073483d256e868f57156 Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Thu, 15 Feb 2024 17:43:32 -0800 Subject: [PATCH] Add export setting to specify whether the native libraries should be compressed for the gradle build --- .../EditorExportPlatformAndroid.xml | 4 ++++ platform/android/export/export_plugin.cpp | 8 +++++++ platform/android/java/app/build.gradle | 8 +++++++ platform/android/java/app/config.gradle | 23 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/platform/android/doc_classes/EditorExportPlatformAndroid.xml b/platform/android/doc_classes/EditorExportPlatformAndroid.xml index a6f92158f94..dd4c2974a44 100644 --- a/platform/android/doc_classes/EditorExportPlatformAndroid.xml +++ b/platform/android/doc_classes/EditorExportPlatformAndroid.xml @@ -42,6 +42,10 @@ Path to the custom export template. If left empty, default template is used. + + If [code]true[/code], native libraries are compressed when performing a Gradle build. + [b]Note:[/b] Although your binary may be smaller, your application may load slower because the native libraries are not loaded directly from the binary at runtime. + Export format for Gradle build. diff --git a/platform/android/export/export_plugin.cpp b/platform/android/export/export_plugin.cpp index 459f5a5983d..fa24893b529 100644 --- a/platform/android/export/export_plugin.cpp +++ b/platform/android/export/export_plugin.cpp @@ -1744,6 +1744,11 @@ String EditorExportPlatformAndroid::get_export_option_warning(const EditorExport if (xr_mode_index == XR_MODE_OPENXR && !gradle_build_enabled) { return TTR("OpenXR requires \"Use Gradle Build\" to be enabled"); } + } else if (p_name == "gradle_build/compress_native_libraries") { + bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); + if (bool(p_preset->get("gradle_build/compress_native_libraries")) && !gradle_build_enabled) { + return TTR("\"Compress Native Libraries\" is only valid when \"Use Gradle Build\" is enabled."); + } } else if (p_name == "gradle_build/export_format") { bool gradle_build_enabled = p_preset->get("gradle_build/use_gradle_build"); if (int(p_preset->get("gradle_build/export_format")) == EXPORT_FORMAT_AAB && !gradle_build_enabled) { @@ -1799,6 +1804,7 @@ void EditorExportPlatformAndroid::get_export_options(List *r_optio r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "custom_template/release", PROPERTY_HINT_GLOBAL_FILE, "*.apk"), "")); r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "gradle_build/use_gradle_build"), false, false, true)); + r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "gradle_build/compress_native_libraries"), false, false, true)); r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "gradle_build/export_format", PROPERTY_HINT_ENUM, "Export APK,Export AAB"), EXPORT_FORMAT_APK, false, true)); // Using String instead of int to default to an empty string (no override) with placeholder for instructions (see GH-62465). // This implies doing validation that the string is a proper int. @@ -3040,6 +3046,7 @@ Error EditorExportPlatformAndroid::export_project_helper(const Refget("gradle_build/compress_native_libraries")) ? "true" : "false"; Vector android_libraries; Vector android_dependencies; @@ -3104,6 +3111,7 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref + int minSdk = getExportMinSdkVersion() + if (minSdk < 23) { + // Enforce the default behavior for compatibility with device running api < 23 + return true + } + + String legacyPackagingFlag = project.hasProperty("compress_native_libraries") ? project.property("compress_native_libraries") : "" + if (legacyPackagingFlag != null && !legacyPackagingFlag.isEmpty()) { + return Boolean.parseBoolean(legacyPackagingFlag) + } + + // Default behavior for minSdk >= 23 + return false +}