From 70ff213de5c214af108596f230ae026bb8edb2fa Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Sun, 9 Feb 2025 19:28:02 +0100 Subject: [PATCH] C#: Validate project TFM for Android template exports The C# Android export template includes `.jar` dependencies from .NET 8.0, so other TFMs are not supported. --- platform/android/export/export_plugin.cpp | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/platform/android/export/export_plugin.cpp b/platform/android/export/export_plugin.cpp index 578f723e700..0f50f725258 100644 --- a/platform/android/export/export_plugin.cpp +++ b/platform/android/export/export_plugin.cpp @@ -55,6 +55,10 @@ #include "modules/modules_enabled.gen.h" // For mono. #include "modules/svg/image_loader_svg.h" +#ifdef MODULE_MONO_ENABLED +#include "modules/mono/utils/path_utils.h" +#endif + #ifdef ANDROID_ENABLED #include "../os_android.h" #endif @@ -2526,6 +2530,43 @@ bool EditorExportPlatformAndroid::has_valid_username_and_password(const Refglobalize_path("res://" + assembly_name + ".csproj"); + + if (!FileAccess::exists(project_path)) { + return true; + } + + String pipe; + List args; + args.push_back("build"); + args.push_back(project_path); + args.push_back("--getProperty:TargetFramework"); + + int exitcode; + Error err = OS::get_singleton()->execute("dotnet", args, &pipe, &exitcode, true); + if (err != OK || exitcode != 0) { + if (err != OK) { + WARN_PRINT("Failed to execute dotnet command. Error " + String(error_names[err])); + } else if (exitcode != 0) { + print_line(pipe); + WARN_PRINT("dotnet command exited with code " + itos(exitcode) + ". See output above for more details."); + } + 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"; + } else { + String tfm = pipe.strip_edges(); + if (tfm != required_tfm) { + r_error += vformat(TTR("C# project targets '%s' but the export template only supports '%s'. Consider using gradle builds instead."), tfm, required_tfm) + "\n"; + return false; + } + } + + return true; +} +#endif + bool EditorExportPlatformAndroid::has_valid_export_configuration(const Ref &p_preset, String &r_error, bool &r_missing_templates, bool p_debug) const { String err; bool valid = false; @@ -2534,6 +2575,15 @@ bool EditorExportPlatformAndroid::has_valid_export_configuration(const Ref