From 65cfa28cce5f64d2aa7434e61b70512fa15aed64 Mon Sep 17 00:00:00 2001 From: Fredia Huya-Kouadio Date: Tue, 7 Dec 2021 14:38:33 -0800 Subject: [PATCH] Add configs to specify the min and target sdk versions. --- platform/android/export/export_plugin.cpp | 39 +++++++++++++++++++++-- platform/android/java/app/build.gradle | 4 +-- platform/android/java/app/config.gradle | 28 ++++++++++++++-- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/platform/android/export/export_plugin.cpp b/platform/android/export/export_plugin.cpp index 4b10ae7425f..892457d70a8 100644 --- a/platform/android/export/export_plugin.cpp +++ b/platform/android/export/export_plugin.cpp @@ -224,6 +224,10 @@ 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"; +static const int DEFAULT_MIN_SDK_VERSION = 19; // Should match the value in 'platform/android/java/app/config.gradle#minSdk' +static const int DEFAULT_TARGET_SDK_VERSION = 30; // Should match the value in 'platform/android/java/app/config.gradle#targetSdk' +const String SDK_VERSION_RANGE = vformat("%s,%s,1", DEFAULT_MIN_SDK_VERSION, DEFAULT_TARGET_SDK_VERSION); + void EditorExportPlatformAndroid::_check_for_changes_poll_thread(void *ud) { EditorExportPlatformAndroid *ea = (EditorExportPlatformAndroid *)ud; @@ -1691,6 +1695,9 @@ void EditorExportPlatformAndroid::get_export_options(List *r_optio r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/code", PROPERTY_HINT_RANGE, "1,4096,1,or_greater"), 1)); r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "version/name"), "1.0")); + r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/min_sdk", PROPERTY_HINT_RANGE, SDK_VERSION_RANGE), DEFAULT_MIN_SDK_VERSION)); + r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "version/target_sdk", PROPERTY_HINT_RANGE, SDK_VERSION_RANGE), DEFAULT_TARGET_SDK_VERSION)); + r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/unique_name", PROPERTY_HINT_PLACEHOLDER_TEXT, "ext.domain.name"), "org.godotengine.$genname")); r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "package/name", PROPERTY_HINT_PLACEHOLDER_TEXT, "Game Name [default if blank]"), "")); r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "package/signed"), true)); @@ -2022,10 +2029,11 @@ String EditorExportPlatformAndroid::get_apksigner_path() { bool EditorExportPlatformAndroid::can_export(const Ref &p_preset, String &r_error, bool &r_missing_templates) const { String err; bool valid = false; + const bool custom_build_enabled = p_preset->get("custom_template/use_custom_build"); // Look for export templates (first official, and if defined custom templates). - if (!bool(p_preset->get("custom_template/use_custom_build"))) { + if (!custom_build_enabled) { String template_err; bool dvalid = false; bool rvalid = false; @@ -2184,7 +2192,6 @@ bool EditorExportPlatformAndroid::can_export(const Ref &p_pr // Ensure that `Use Custom Build` is enabled if a plugin is selected. String enabled_plugins_names = PluginConfigAndroid::get_plugins_names(get_enabled_plugins(p_preset)); - bool custom_build_enabled = p_preset->get("custom_template/use_custom_build"); if (!enabled_plugins_names.empty() && !custom_build_enabled) { valid = false; err += TTR("\"Use Custom Build\" must be enabled to use the plugins."); @@ -2212,12 +2219,34 @@ bool EditorExportPlatformAndroid::can_export(const Ref &p_pr } if (int(p_preset->get("custom_template/export_format")) == EXPORT_FORMAT_AAB && - !bool(p_preset->get("custom_template/use_custom_build"))) { + !custom_build_enabled) { valid = false; err += TTR("\"Export AAB\" is only valid when \"Use Custom Build\" is enabled."); err += "\n"; } + // Check the min sdk version + int min_sdk_version = p_preset->get("version/min_sdk"); + if (min_sdk_version != DEFAULT_MIN_SDK_VERSION && !custom_build_enabled) { + valid = false; + err += TTR("Changing the \"Min Sdk\" is only valid when \"Use Custom Build\" is enabled."); + err += "\n"; + } + + // Check the target sdk version + int target_sdk_version = p_preset->get("version/target_sdk"); + if (target_sdk_version != DEFAULT_TARGET_SDK_VERSION && !custom_build_enabled) { + valid = false; + err += TTR("Changing the \"Target Sdk\" is only valid when \"Use Custom Build\" is enabled."); + err += "\n"; + } + + if (target_sdk_version < min_sdk_version) { + valid = false; + err += TTR("\"Target Sdk\" version must be greater or equal to \"Min Sdk\" version."); + err += "\n"; + } + r_error = err; return valid; } @@ -2878,6 +2907,8 @@ Error EditorExportPlatformAndroid::export_project_helper(const Refget("package/unique_name")); String version_code = itos(p_preset->get("version/code")); String version_name = p_preset->get("version/name"); + String min_sdk_version = itos(p_preset->get("version/min_sdk")); + String target_sdk_version = itos(p_preset->get("version/target_sdk")); String enabled_abi_string = String("|").join(enabled_abis); String sign_flag = should_sign ? "true" : "false"; String zipalign_flag = "true"; @@ -2907,6 +2938,8 @@ Error EditorExportPlatformAndroid::export_project_helper(const Ref + String targetSdkVersion = project.hasProperty("export_version_target_sdk") ? project.property("export_version_target_sdk") : "" + if (targetSdkVersion == null || targetSdkVersion.isEmpty()) { + targetSdkVersion = "$versions.targetSdk" + } + try { + return Integer.parseInt(targetSdkVersion) + } catch (NumberFormatException ignored) { + return versions.targetSdk + } +} + ext.getGodotEditorVersion = { -> String editorVersion = project.hasProperty("godot_editor_version") ? project.property("godot_editor_version") : "" if (editorVersion == null || editorVersion.isEmpty()) {