Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Fallback to CoreCLR/MonoVM hosting APIs when hostfxr/NativeAOT fails #88803

Merged
merged 1 commit into from
Sep 17, 2024

Conversation

raulsntos
Copy link
Member

@raulsntos raulsntos commented Feb 25, 2024

Custom builds to test this PR can be downloaded from #88803 (comment)


Some platforms don't support hostfxr but we can use the coreclr/monosgen library directly to initialize the runtime.

Android exports now use the android runtime identifier instead of linux-bionic, this removes the restrictions we previously had:

  • Adds support for all Android architectures (arm32, arm64, x32, and x64), previously only the 64-bit architectures were supported.
  • Loads System.Security.Cryptography.Native.Android (the .NET library that binds to the Android OS crypto functions).

@@ -58,6 +58,9 @@ dependencies {
if (pluginsBinaries != null && pluginsBinaries.size() > 0) {
implementation files(pluginsBinaries)
}

// .NET dependencies
implementation files('../../../../modules/mono/thirdparty/libSystem.Security.Cryptography.Native.Android.jar')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like including the .jar like this. This .jar comes from the .NET runtime pack, but we don't have it at the time of building the export template and adding a .jar when exporting the project doesn't seem to be possible without forcing the user to enable Use Gradle Build.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the comment about using flavor to guard the dependency, the jar file also needs to be within the app directory otherwise this will break editor gradle builds.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For gradle builds we probably want to get the jar file from the built project. But for now I can just move the jar to the app directory, should I put it in any specific subdirectory?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would typically go into the libs directory, but the contents of that directory get deleted during a clean build, so let's create a new monoLibs directory and include the jar there.

For gradle builds we probably want to get the jar file from the built project

How would that work? Does the user project include the jar file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would that work? Does the user project include the jar file?

When the C# project is built, during the restore step it will retrieve all the dependencies packages. For android builds, this means it will retrieve the Mono runtime package (e.g.: Microsoft.NETCore.App.Runtime.Mono.android-arm for android-arm32). This package contains the jar file for the runtime your project targets and we can retrieve it from your NuGet packages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the assemble gradle task for building and it's structured like this: assemble[flavors...][buildtypes...].
So by default if you only specify the assemble task, it builds all the flavors and build types; if you specify the build type (e.g: assembleDebug), then it builds all the flavors; and so on.. So if you don't change anything, the mono flavor will be included as part of the build, and if you only want to build the mono flavor, you can add it as a prefix to the assemble task.

After you configure the flavor, you can use the Gradle tool window (or the following command) to see the tasks that are being added.

And I guess I have to add a standard flavor for the non-mono version?

Yes you would need to add a standard flavor as an alternative to the mono flavor.

Do I have to duplicate every copy{Target}BinaryToBin task for the mono flavor?

As currently setup, you would need to duplicate the copy tasks. Let me take a look and see if I can clean that setup.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me take a look and see if I can clean that setup.

@raulsntos I've cleaned the gradle build setup in #91271. Feel free to comment on the PR if you have questions on the update.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, is something like raulsntos@ad0f8fe what you had in mind? Or should the generateBuildTasks function take an extra edition parameter to only include a single edition build task? If so, should I create a new generateGodotTemplatesMono task for this or can the generateGodotTemplates task use parameters?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with the second approach and have generateBuildTasks take an extra edition parameter and add a generateGodotMonoTemplates task that invokes it.

That matches our current build setup, where we have separate sections for standard and mono builds. And it allows users to only build the edition they need.

cc @akien-mga as this'll affect the android build scripts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m4gr3d
Copy link
Contributor

m4gr3d commented Mar 28, 2024

@raulsntos We'll need to create a separate mono flavor for the app module that includes the mono dependencies.

The addition of that flavor means we'll be able to guard mono specific logic in the java code by checking for the flavor:

if (BuildConfig.FLAVOR.equals("mono")) {
    // load mono library
}

This will also result in the creation of additional build templates specifically for the mono builds. @akien-mga we'll need to update the build tools logic for the mono build templates.

@Zamir7
Copy link

Zamir7 commented Apr 27, 2024

Could you explain what this means and how long to wait for corrections, and then review?

@raulsntos raulsntos force-pushed the dotnet/android-monovm branch from e79e5ba to cc86826 Compare April 28, 2024 00:54
@raulsntos
Copy link
Member Author

raulsntos commented Apr 28, 2024

@Zamir7 As explained in the PR description, this adds support for 32-bit Android architectures and Android OS APIs (mainly crypto, see the linked issue) to C# projects.

This PR is marked as a draft, which means it's still a work-in-progress. It is tentatively planned for 4.4 (as you can see in the assigned milestone).

Copy link
Contributor

@m4gr3d m4gr3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good!

I haven't been able to test, so additional testing and validation would be needed, but that should be the easy part :)

platform/android/java/app/src/com/godot/game/GodotApp.java Outdated Show resolved Hide resolved
@m4gr3d
Copy link
Contributor

m4gr3d commented Apr 30, 2024

There's one more set of changes that'll be needed to support gradle builds.

export_plugin uses the assemble command for the gradle builds, so that logic will need to be updated to use assembleStandard on standard builds and assembleMono on mono builds.

The crypto jar file will also need to be copied to the build directory after it's downloaded. Alternatively if you know the path where it'll be downloaded, then app/build.gradlecan be updated to look in that path in addition to the path it's checking now.

platform/android/java/app/build.gradle Show resolved Hide resolved
platform/android/java/build.gradle Outdated Show resolved Hide resolved
@raulsntos raulsntos force-pushed the dotnet/android-monovm branch from d2956c7 to 7c80ec4 Compare May 1, 2024 04:24
@raulsntos
Copy link
Member Author

The crypto jar file will also need to be copied to the build directory after it's downloaded. Alternatively if you know the path where it'll be downloaded, then app/build.gradle can be updated to look in that path in addition to the path it's checking now.

It should be copied because this file is retrieved when building the .NET project and it's stored in a temporary directory that will be deleted after the export. We can copy it as part of the ExportPlugin implementation, but I'd need a way to get the path to the gradle build directory as well as a way to detect if the export is part of a gradle build.

It looks like I can check if it's a gradle build with get_option("gradle_build/use_gradle_build"), and then use add_shared_object to copy the .jar file. It copies it to the res://android/libs/{BUILD_TYPE}/{ARCH} directory which I assume is enough for it to be included in the gradle build.

@raulsntos
Copy link
Member Author

@felipejfc Thanks for testing. From that stack trace, it looks like it's trying to use hostfxr to initialize the .NET runtime which shouldn't be possible since the Android export shouldn't contain libhostfxr.so. Can you share the csproj and the APK?

I'd like to see the csproj so I can check if there's anything that could affect the publish process. For example, something like UseMonoRuntime or PublishAot.

And I'd like to see the APK to check what native libraries were included in the lib directory, specifically I'd expect to see libmonosgen-2.0.so there. You can check yourself opening the APK in Android Studio.

I do have native android and ios plugins enabled. The iOS one was built with 4.3 headers, would this cause problem?

I don't think so. It looks like the error is caused by failing to initialize the .NET runtime, not sure what may be causing it. I'd like to see what native libraries are included in the APK. And, if libmonosgen-2.0 is missing, we'd need to investigate why that's the case.

@felipejfc
Copy link

felipejfc commented Sep 2, 2024

@felipejfc Thanks for testing. From that stack trace, it looks like it's trying to use hostfxr to initialize the .NET runtime which shouldn't be possible since the Android export shouldn't contain libhostfxr.so. Can you share the csproj and the APK?

I'd like to see the csproj so I can check if there's anything that could affect the publish process. For example, something like UseMonoRuntime or PublishAot.

And I'd like to see the APK to check what native libraries were included in the lib directory, specifically I'd expect to see libmonosgen-2.0.so there. You can check yourself opening the APK in Android Studio.

I do have native android and ios plugins enabled. The iOS one was built with 4.3 headers, would this cause problem?

I don't think so. It looks like the error is caused by failing to initialize the .NET runtime, not sure what may be causing it. I'd like to see what native libraries are included in the APK. And, if libmonosgen-2.0 is missing, we'd need to investigate why that's the case.

Sadly I can't share the whole files since this is a private company project, however, I can provide all info you asked for:

<Project Sdk="Godot.NET.Sdk/4.4.0-android-monovm">
  <PropertyGroup>
    <EnableAot>false</EnableAot>
    <EnableAot Condition="'$(GodotTargetPlatform)' == 'ios'">true</EnableAot>
    <PublishAotUsingRuntimePack>$(EnableAot)</PublishAotUsingRuntimePack>
    <PublishAot Condition=" ('$(NETCoreSdkRuntimeIdentifier)' == 'linux-x64' or $(NETCoreSdkRuntimeIdentifier.StartsWith('osx')) ) and ('$(GodotTargetPlatform)' == 'ios' or '$(GodotTargetPlatform)' == 'android') and '$(EnableAot)' == 'true' ">true</PublishAot>
    <TargetFramework>net8.0</TargetFramework>
    <EnableDynamicLoading>true</EnableDynamicLoading>
    <JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
    <RootNamespace>RoyalMetagameGodot</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <!-- Root the assemblies to avoid trimming. -->
    <PackageReference Include="Google.Protobuf" Version="3.27.3" />
    <PackageReference Include="UniTask" Version="2.5.5" />
    <TrimmerRootAssembly Include="GodotSharp" />
    <TrimmerRootAssembly Include="$(TargetName)" />
    <!-- Add reference to the source generator project -->
    <ProjectReference Include="sourcegenerator/SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    <ProjectReference Include="./sharedtypes/sharedtypes.csproj" />
  </ItemGroup>
  <ItemGroup>
    <Compile Remove="sourcegenerator/**/*.cs" />
    <Compile Remove="sharedtypes/**/*.cs" />
  </ItemGroup>
  <Target Name="WarnIfAndroidWithoutAot" BeforeTargets="Build">
    <Warning Text="AOT is not being used in the generated APK. AOT for Android is only supported when building from macOS or Linux. Current Host RuntimeIdentifier: $(NETCoreSdkRuntimeIdentifier)" Condition="'$(GodotTargetPlatform)' == 'android' and '$(EnableAot)' == 'true' and !('$(NETCoreSdkRuntimeIdentifier)' == 'linux-x64' or $(NETCoreSdkRuntimeIdentifier.StartsWith('osx')))" />
  </Target>
  <Import Project="android.targets" />
</Project>%

This is the content of android.targets:

<Project>
  <PropertyGroup>
    <_NdkSysrootAbi Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-arm64' or '$(RuntimeIdentifier)' == 'android-arm64' ">aarch64-linux-android</_NdkSysrootAbi>
    <_NdkClangPrefix Condition=" '$(RuntimeIdentifier)' == 'linux-bionic-arm64' or '$(RuntimeIdentifier)' == 'android-arm64' ">aarch64-linux-android21-</_NdkClangPrefix>
    <_NdkPrebuiltAbi Condition=" $(NETCoreSdkRuntimeIdentifier.StartsWith('osx')) ">darwin-x86_64</_NdkPrebuiltAbi>
    <_NdkPrebuiltAbi Condition=" '$(NETCoreSdkRuntimeIdentifier)' == 'linux-x64' ">linux-x86_64</_NdkPrebuiltAbi>
    <_NdkPrebuiltAbi Condition=" '$(NETCoreSdkRuntimeIdentifier)' == 'win-x64' ">windows-x86_64</_NdkPrebuiltAbi>
    <_NdkSysrootDir>$(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)</_NdkSysrootDir>
    <_NdkBinDir>$(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin</_NdkBinDir>
  </PropertyGroup>

  <PropertyGroup Condition="'$(EnableAOT)' == 'true' and ($(RuntimeIdentifier.StartsWith('linux-bionic')) Or $(RuntimeIdentifier.StartsWith('android-arm64')))">
    <CppCompilerAndLinker>$(_NdkBinDir)/$(_NdkClangPrefix)clang++</CppCompilerAndLinker>
    <ObjCopyName>$(_NdkBinDir)/llvm-objcopy</ObjCopyName>
  </PropertyGroup>

  <ItemGroup Condition="'$(EnableAOT)' == 'true' and ($(RuntimeIdentifier.StartsWith('linux-bionic')) Or $(RuntimeIdentifier.StartsWith('android-arm64')))">
    <LinkerArg Include="-Wl,--undefined-version" />
  </ItemGroup>

  <Target Name="_ValidateEnvironment"
      BeforeTargets="Build"
      Condition="'$(EnableAOT)' == 'true' and ($(RuntimeIdentifier.StartsWith('linux-bionic')) or $(RuntimeIdentifier.StartsWith('android-arm64'))) and ('$(NETCoreSdkRuntimeIdentifier)' == 'linux-x64' or $(NETCoreSdkRuntimeIdentifier.StartsWith('osx'))) ">
    <Error
        Condition=" '$(ANDROID_NDK_HOME)' == '' Or !Exists($(ANDROID_NDK_HOME)) "
        Text="Set the %24ANDROID_NDK_HOME environment variable to the path of the Android NDK."
     />
    <Error
        Condition=" !Exists($(_NdkSysrootDir))"
        Text="NDK 'sysroot' dir `$(_NdkSysrootDir)` does not exist. You're on your own."
    />
  </Target>
 </Project>

This should only be used if NativeAOT + Android build which I'm not setting right now as you can see, only for iOS, but it's there as a placeholder once I want to start building android with it.

As for the libs, seems to be there:

image

More logging from debugging with android studio:

  I  Godot Engine v4.4.android-monovm.mono.custom_build.95c5436e3 - https://godotengine.org
2024-09-02 20:29:19.417 30752-30845 godot                   com.example.godot                    I  Vulkan 1.3.274 - Forward Mobile - Using Device #0: ARM - Mali-G78
2024-09-02 20:29:19.420 30752-30845 gralloc4                com.example.godot                    E  ERROR: Format allocation info not found for format: 3b
2024-09-02 20:29:19.420 30752-30845 gralloc4                com.example.godot                    E  ERROR: Format allocation info not found for format: 0
2024-09-02 20:29:19.421 30752-30845 gralloc4                com.example.godot                    E  Invalid base format! req_base_format = (<unrecognized format> 0x0), req_format = (<unrecognized format> 0x3b), type = 0x0
2024-09-02 20:29:19.421 30752-30845 gralloc4                com.example.godot                    E  ERROR: Unrecognized and/or unsupported format (<unrecognized format> 0x3b) and usage (CPU_READ_NEVER|CPU_WRITE_NEVER|GPU_TEXTURE|GPU_RENDER_TARGET|COMPOSER_OVERLAY 0xb00)
2024-09-02 20:29:19.421 30752-30845 gralloc4                com.example.godot                    E  ERROR: Format allocation info not found for format: 3b
2024-09-02 20:29:19.421 30752-30845 gralloc4                com.example.godot                    E  ERROR: Format allocation info not found for format: 0
2024-09-02 20:29:19.421 30752-30845 gralloc4                com.example.godot                    E  Invalid base format! req_base_format = (<unrecognized format> 0x0), req_format = (<unrecognized format> 0x3b), type = 0x0
2024-09-02 20:29:19.421 30752-30845 gralloc4                com.example.godot                    E  ERROR: Unrecognized and/or unsupported format (<unrecognized format> 0x3b) and usage (CPU_READ_NEVER|CPU_WRITE_NEVER|GPU_TEXTURE|GPU_RENDER_TARGET|COMPOSER_OVERLAY 0xb00)
2024-09-02 20:29:19.515 30752-30845 m.example.godot         com.example.godot                    D  PlayerBase::PlayerBase()
2024-09-02 20:29:19.519 30752-30845 m.example.godot         com.example.godot                    D  TrackPlayerBase::TrackPlayerBase()
2024-09-02 20:29:19.519 30752-30845 libOpenSLES             com.example.godot                    I  Emulating old channel mask behavior (ignoring positional mask 0x3, using default mask 0x3 based on channel count of 2)
2024-09-02 20:29:19.532 30752-30845 godot                   com.example.godot                    I  
2024-09-02 20:29:19.656 30752-30752 VkThread                com.example.godot                    W  type=1400 audit(0.0:24003): avc:  granted  { execute } for  path="/data/data/com.example.godot/cache/data_RoyalMetagameGodot_android_arm64/libhostfxr.so" dev="dm-56" ino=40929 scontext=u:r:untrusted_app:s0:c123,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c123,c257,c512,c768 tclass=file app=com.example.godot
2024-09-02 20:29:19.656 30752-30752 VkThread                com.example.godot                    W  type=1400 audit(0.0:24004): avc:  granted  { execute } for  path="/data/data/com.example.godot/cache/data_RoyalMetagameGodot_android_arm64/libhostpolicy.so" dev="dm-56" ino=37304 scontext=u:r:untrusted_app:s0:c123,c257,c512,c768 tcontext=u:object_r:app_data_file:s0:c123,c257,c512,c768 tclass=file app=com.example.godot
2024-09-02 20:29:19.664 30752-30845 godot                   com.example.godot                    E  USER ERROR: hostfxr_initialize_for_dotnet_command_line failed with code: -2147450745
2024-09-02 20:29:19.664 30752-30845 godot                   com.example.godot                    E     at: initialize_hostfxr_self_contained (modules/mono/mono_gd/gd_mono.cpp:328)
2024-09-02 20:29:19.664 30752-30845 godot                   com.example.godot                    E  USER ERROR: Parameter "load_assembly_and_get_function_pointer" is null.
2024-09-02 20:29:19.664 30752-30845 godot                   com.example.godot                    E     at: initialize_hostfxr_and_godot_plugins (modules/mono/mono_gd/gd_mono.cpp:395)
2024-09-02 20:29:19.664 30752-30845 godot                   com.example.godot                    E  USER ERROR: Parameter "godot_plugins_initialize" is null.
2024-09-02 20:29:19.664 30752-30845 godot                   com.example.godot                    E     at: initialize (modules/mono/mono_gd/gd_mono.cpp:536)
2024-09-02 20:29:19.690 30752-30845 libc                    com.example.godot                    A  Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 30845 (VkThread), pid 30752 (m.example.godot)
2024-09-02 20:29:19.722 30752-30888 ProfileInstaller        com.example.godot                    D  Installing profile for com.example.godot
2024-09-02 20:29:19.789 30752-30752 InteractionJankMonitor  com.example.godot                    W  Initializing without READ_DEVICE_CONFIG permission. enabled=false, interval=1, missedFrameThreshold=3, frameTimeThreshold=64, package=com.example.godot

Same csproj etc works with 4.3, by just reimporting

@raulsntos
Copy link
Member Author

Thanks for sharing, I don't see anything in the csproj or android.targets files that seems problematic.

From the screenshot you shared of the APK, I'd say the publish process was successful. It's weird that it's trying to use hostfxr since there's no libhostfxr.so.

By any chance have you exported this game before with a previous version of Godot? Since Godot extracts the contents of the APK to a cache directory, it's possible that previously cached files are still there causing issues.

It looks like the cache path is /data/data/com.example.godot/cache/data_RoyalMetagameGodot_android_arm64, could you try to delete that directory before executing the game again? Alternatively, changing the Android application ID before exporting may also work.

@felipejfc
Copy link

felipejfc commented Sep 3, 2024

Spot on! Deleted cache and uninstalled the app, installed it again and it works! We will do good then adding this disclaimer that the application needs to be installed with a fresh cache if the player had a version generated with an old godot build installed

When building for armv7 I got the following error Unable to find package Microsoft.NETCore.App.Runtime.linux-bionic-arm in some csprojs:

Which was fixed by dotnet/android#8170 (comment)

Tomorrow I will test on the armv7 device and report result

@raulsntos
Copy link
Member Author

We will do good then adding this disclaimer that the application needs to be installed with a fresh cache if the player had a version generated with an old godot build installed

We'll fix it so it automatically clears those cached files if the extracted .dotnet-publish-manifest file is outdated, so the disclaimer won't be needed.

When building for armv7 I got the following error Unable to find package Microsoft.NETCore.App.Runtime.linux-bionic-arm in some csprojs:

Which was fixed by dotnet/android#8170 (comment)

Thanks a lot, that's really interesting. As mentioned in that issue, I suspect your referenced projects (i.e.: ./sharedtypes/sharedtypes.csproj) are not setting UseMonoRuntime=true.

We set UseMonoRuntime=true in the Godot.NET.Sdk, and I assume your referenced projects are using Microsoft.NET.Sdk. Perhaps we can add the workaround you linked in the Godot.NET.Sdk, as a temporary measure until the issue is fixed upstream.

@felipejfc
Copy link

Thanks a lot, that's really interesting. As mentioned in that issue, I suspect your referenced projects (i.e.: ./sharedtypes/sharedtypes.csproj) are not setting UseMonoRuntime=true.

They are not! Should I reference it?

@Delsin-Yu
Copy link
Contributor

We'll fix it so it automatically clears those cached files if the extracted .dotnet-publish-manifest file is outdated, so the disclaimer won't be needed.

I want to mention that the Windows Single File export feature, which involves embedding script files, is currently encountering a similar issue. Specifically, the single-file executable stops working after switching to AOT because the outdated managed assemblies inside the cache directory are loaded again upon launching the new AOT executable.

@raulsntos
Copy link
Member Author

They are not! Should I reference it?

@felipejfc If the workaround mentioned in the issue you linked doesn't work, you could try doing that. Ideally you shouldn't have to do anything manually.

I want to mention that the Windows Single File export feature, which involves embedding script files, is currently encountering a similar issue.

@Delsin-Yu Yeah, this is an existing issue. It's reported in #96299.

@felipejfc
Copy link

felipejfc commented Sep 4, 2024

@raulsntos Tested in a Motorola ARMv7 device with Adreno GPU

1 - Build crashed with Mobile rendered (because of vulkan I assume)

godot E  USER ERROR: .NET: Failed to get GodotPlugins initialization function pointer
      E     at: initialize_coreclr_and_godot_plugins (modules/mono/mono_gd/gd_mono.cpp:485)
      E  USER ERROR: .NET: Failed to load hostfxr
      E     at: initialize (modules/mono/mono_gd/gd_mono.cpp:549)
      E  USER ERROR: BUG: Unreferenced static string to 0: ShaderCompilation
      E     at: unref (core/string/string_name.cpp:127)
libc  F  FORTIFY: pthread_mutex_lock called on a destroyed mutex (0x81bb9e74)`

2 - When built with OpenGL the game worked fine!

Idk if the crash has to do with godot or with vulkan driver incompatibility but I would guess the later

@raulsntos
Copy link
Member Author

I would not expect to see the Failed to load hostfxr error, it sounds like there may be a libhostfxr.so in the cache again1.

Footnotes

  1. https://github.com/godotengine/godot/pull/88803#issuecomment-2325401156

@raulsntos raulsntos force-pushed the dotnet/android-monovm branch 2 times, most recently from c9702c4 to f78d359 Compare September 12, 2024 11:48
@raulsntos
Copy link
Member Author

raulsntos commented Sep 12, 2024

Rebased and made new custom builds to test, the cached data should no longer be an issue since #96301 was merged:

🏁 Windows 🍎 macOS 🐧 Linux
Editor x86_64 Editor universal Editor x86_64
Editor x86_32 Editor x86_32

And the Android templates you'll need to use to export:

🤖 Android
Templates

Note

Remember to push the provided nupkgs to a local NuGet source so .NET can find the custom packages, see the documentation for more details.

@scgm0
Copy link
Contributor

scgm0 commented Sep 12, 2024

I’m curious, after the PR is merged, can I still use this change #86791 and export it to Android via aot?

@raulsntos
Copy link
Member Author

raulsntos commented Sep 12, 2024

NativeAOT should not be affected by this PR. If your csproj contains <PublishAot>true</PublishAot>, then it won't use the Mono runtime, the libmonosgen-2.0.so library won't be in the exported project so the changes in this PR will not take place.

Keep in mind NativeAOT for Android is still experimental upstream, and will likely require more changes to make it work properly but that's outside of the scope of this PR which only focuses on enabling the Mono runtime.

You may also try to enable MonoAOT with <RunAOTCompilation>true</RunAOTCompilation> but I haven't tested it and will likely not work.

@felipejfc
Copy link

Any chance we backport this to 4.3? Using native binding for handling http calls in android is a hassle

@raulsntos
Copy link
Member Author

I don't think so, this is a pretty big change to C# Android exports that builds on top of a number of other changes to the Android platform that I don't expect to see backported to 4.3 either.

Issues with crypto functions not being available in 4.3 can be worked around by avoiding crypto APIs from the BCL or thirdparty C# libraries, and instead relying on the crypto APIs provided by Godot (i.e.: Use Godot.HttpClient instead of System.Net.Http.HttpClient).

Alternatively, you can include the required native libraries (libssl.so and libcrypto.so) in the exported APK and that should work too but I reckon it's even more of a hassle to do this.

@akien-mga
Copy link
Member

Needs rebase after #96967.

Some platforms don't support hostfxr but we can use the coreclr/monosgen library directly to initialize the runtime.

Android exports now use the `android` runtime identifier instead of `linux-bionic`, this removes the restrictions we previously had:
- Adds support for all Android architectures (arm32, arm64, x32, and x64), previously only the 64-bit architectures were supported.
- Loads `System.Security.Cryptography.Native.Android` (the .NET library that binds to the Android OS crypto functions).
@raulsntos raulsntos force-pushed the dotnet/android-monovm branch from f78d359 to 0aa46e1 Compare September 16, 2024 15:08
@akien-mga akien-mga merged commit 0eea872 into godotengine:master Sep 17, 2024
20 checks passed
@akien-mga
Copy link
Member

Thanks! Amazing work 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C# Android SSL Crash using System.Net.Http.HttpClient
8 participants