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#: Support exporting for Android #73257

Merged
merged 3 commits into from
Jul 31, 2023

Conversation

RedworkDE
Copy link
Member

@RedworkDE RedworkDE commented Feb 13, 2023

Limitations: While https://github.com/dotnet/core/blob/main/release-notes/6.0/supported-os.md and https://github.com/dotnet/core/blob/main/release-notes/7.0/supported-os.md claim support for android on x64, arm32 and arm64 for both net6 and net7, actually trying to publish a project for android with the target framework net6 always fails with an error about the native runtime package being missing, same for arm32 on net7. So only x64 and arm64 with target framework net7 can actually be exported.

Because android is particular about what you can put into shared objects, put the published files with the regular assets instead and then copy they on startup into some temporary location so that they can be loaded normally.

Screenshots of a test app running on emulator and an actual tablet:
Screenshot_20230302_163047
Screenshot_20230302-160748_Net7

TODOs:

  • What is the best place to put the published outputs for execution? (Also keeping other platforms in mind, even tho this PR doesn't allow other platforms to use this option yet)
  • Don't copy everything on every start, just on the first and when things have changed.
  • The assets can sometimes (always?) be accessed by some path directly, so don't copy anything if this is possible.
    This doesn't really make sense / work. I still left one try with globalize_path as the cost is pretty negligible, but could save a lot of time for the first startup

@ThomasUijlen
Copy link

ThomasUijlen commented Feb 19, 2023

Does this already work? I'm looking to test C# on Android ASAP :O

If so, are there any guides on how to compile everything?

@RedworkDE
Copy link
Member Author

Does this already work? I'm looking to test C# on Android ASAP :O

Yes, this does already work in its current state.

If so, are there any guides on how to compile everything?

Compiling the editor with mono (note that all the bits that talk about mono itself are outdated and no longer relevant) and compiling the android template, but add the module_mono_enabled=yes argument to the scons commands.

@MadFlyFish
Copy link

Great job. Thank you.
I really hope it will release with 4.0.

@Calinou
Copy link
Member

Calinou commented Feb 21, 2023

Great job. Thank you. I really hope it will release with 4.0.

This pull request is targeting a future 4.x release, as 4.0 is currently in release candidate stage.

@RedworkDE RedworkDE force-pushed the net-android-support branch from 7609e62 to 4347771 Compare March 2, 2023 15:13
@RedworkDE RedworkDE marked this pull request as ready for review March 2, 2023 17:32
@RedworkDE RedworkDE requested a review from a team as a code owner March 2, 2023 17:32
@neikeq
Copy link
Contributor

neikeq commented Mar 4, 2023

From what I understand, this looks somewhat similar to how it works in 3.x (although much simpler ofc), where we don't target the android specific target framework, right? What .NET runtime is it using, is it Mono?

I don't think this is the right way to go. My plan for Godot 4 was to not do anything too custom that's not well-supported by .NET. I'm not convinced the supported-os page is referring to this when they claim Android support. I don't really know for sure, but I think net6-android is the only recommended target.
If possible, I would also like NuGet to work well, and many NuGet packages only work well when targeting the Android variant of the framework.

I'm currently waiting to see what's coming in .NET 8, as I think they are working on support for exporting a net8-android project as a self-contained library. But I'm not certain, so we'll have to wait and see.

@neikeq
Copy link
Contributor

neikeq commented Mar 4, 2023

actually trying to publish a project for android with the target framework net6 always fails with an error about the native runtime package being missing, same for arm32 on net7. So only x64 and arm64 with target framework net7 can actually be exported.

Does it use the CoreCLR runtime? If so, does that still happen when you pass -p:UseMonoRuntime=true to dotnet.

@RedworkDE
Copy link
Member Author

This currently works by simply specifying the android-arm64 rid for publishing, and then the output then includes a libhostfxr.so that takes care of everything like on the other platforms.

As far as I can tell, these exports use coreclr as the runtime and mono as the jit.

I'll have to test the rest for a bit.

@RedworkDE
Copy link
Member Author

Before I forget to comment again:

  • UseMonoRuntime=true this makes it possible to publish for android with all 4 architectures supported by godot, but those exports use the mono runtime (duh) and thus the CoreCLR loading code no longer works. I did not investigate if it is possible to load this without bringing all the old mono support code back.
  • Using the net6.0-android or net7.0-android tfm requires that you install / configure a whole bunch of android development dependencies, but the publish doesn't actually work and just emits the main managed assembly and no dependencies. Not sure what exactly is happening here.

@vonwell
Copy link

vonwell commented May 11, 2023

From what I understand, this looks somewhat similar to how it works in 3.x (although much simpler ofc), where we don't target the android specific target framework, right? What .NET runtime is it using, is it Mono?

I don't think this is the right way to go. My plan for Godot 4 was to not do anything too custom that's not well-supported by .NET. I'm not convinced the supported-os page is referring to this when they claim Android support. I don't really know for sure, but I think net6-android is the only recommended target. If possible, I would also like NuGet to work well, and many NuGet packages only work well when targeting the Android variant of the framework.

I'm currently waiting to see what's coming in .NET 8, as I think they are working on support for exporting a net8-android project as a self-contained library. But I'm not certain, so we'll have to wait and see.

In other words, does the multi-platform export template of C# have to wait until after net8?

@YuriSizov YuriSizov modified the milestones: 4.1, 4.2 Jun 14, 2023
@neikeq
Copy link
Contributor

neikeq commented Jun 28, 2023

While the solution we're going to go with is a different one, currently it still relies on some workarounds this PR implements (copying and extracting the assemblies). So if you can fix the conflicts, I think it's fine to merge this first, then I add the other bits.

@RedworkDE RedworkDE force-pushed the net-android-support branch from 4347771 to f759cc0 Compare June 28, 2023 20:35
@RedworkDE
Copy link
Member Author

I rebased #76305 (which contains the interesting bits with the assemblies handling) and then rebased this PR onto that, tho there isn't really much left. Didn't test this as probably everything you want to merge is in the other PR.

@XorZy
Copy link

XorZy commented Jul 7, 2023

Does this already work? I'm looking to test C# on Android ASAP :O

Yes, this does already work in its current state.

If so, are there any guides on how to compile everything?

Compiling the editor with mono (note that all the bits that talk about mono itself are outdated and no longer relevant) and compiling the android template, but add the module_mono_enabled=yes argument to the scons commands.

Does this still hold true?
I have tried building the android export template with module_mono_enabled=yes as indicated but I am met with the error
RuntimeError: This module does not currently support building for this platform:
I suppose there have been changes since your initial comment but is it still possible to get it to work?
I am currently working on a C# game and being able to export to Android would be extremely beneficial.
I would be willing to provide any help required if this PR can give us Android builds without having to wait for .NET8.

@AThousandShips
Copy link
Member

Please read the above link

@XorZy
Copy link

XorZy commented Jul 7, 2023

Please read the above link

I have, hence my question.
This PR (supposedly ?) does it it in a different way and was reported to be working a few months ago, so I'm curious if it can still be used, even out of tree, to circumvent the aforementioned problems with .NET6/7.

@MitchMakesThings
Copy link

@XorZy I compiled it in the last week or two and things seem to be working fine.
I'm almost certain you've missed a step from the Compiling for Android page. Make sure you've got the ANDROID_SDK_ROOT environment variable set.

I also updated my csproj to .NET 7 instead of 6. Not sure if that was required or not.

@XorZy
Copy link

XorZy commented Jul 7, 2023

@XorZy I compiled it in the last week or two and things seem to be working fine. I'm almost certain you've missed a step from the Compiling for Android page. Make sure you've got the ANDROID_SDK_ROOT environment variable set.

I also updated my csproj to .NET 7 instead of 6. Not sure if that was required or not.

Thanks for letting me know!
Yeah it's entirely possible I made a mistake, I am not familiar with the Android build process.
I think I have set the SDK path properly since I can compile it without module_mono_enabled=yes.
I compiled it using RedworkDE:net-android-support (f759cc0), maybe that was my mistake?

EDIT

So I tried again, cloning the repo from scratch and checking out the branch "net-android-support".
This time I did not get the RuntimeError: This module does not currently support building for this platform. but instead modules/mono/godotsharp_dirs.cpp:37:10: fatal error: 'mono_gd/support/android_support.h' file not found and sure enough this file was apparently deleted in commit 7c456d2.
Am I skipping a step or something? Should I check out another branch?

@emmauss
Copy link

emmauss commented Jul 29, 2023

Net8 will have nativeAOT support for Linux bionic both x64 and arm64 variants. Full support for shared libraries has only been available in the dotnet runtime repo for about 2 months, so I'm surprised how far you've gotten on net7. Are you publishing as a self contained app, or shared library?
Early attempts to create a simple dotnet(not mono) app for Android on net7, showed that you can create a self contained executable for the bionic target, but not a shared library. The latter requiring nativeAOT.

@neikeq neikeq merged commit 54ba3cf into godotengine:master Jul 31, 2023
@neikeq
Copy link
Contributor

neikeq commented Jul 31, 2023

Since we're not yet in feature freeze for 4.2, I'm merging this now so more people can start testing, and since I need it for my PR as well.

@RedworkDE RedworkDE deleted the net-android-support branch July 31, 2023 21:17
@RedworkDE RedworkDE restored the net-android-support branch July 31, 2023 21:17
@RedworkDE RedworkDE deleted the net-android-support branch July 31, 2023 21:17
@RedworkDE RedworkDE restored the net-android-support branch July 31, 2023 21:17
@RedworkDE RedworkDE deleted the net-android-support branch July 31, 2023 21:17
@Zamir7
Copy link

Zamir7 commented Aug 11, 2023

I downloaded godot 4.2 dev3 and I'm trying to export to android, but it says that in version 4 the export is not yet available, although the release says that you can already test the export (C#: Add initial support for exporting to Android (GH-73257)

er

@XorZy
Copy link

XorZy commented Aug 11, 2023

I downloaded godot 4.2 dev3 and I'm trying to export to android, but it says that in version 4 the export is not yet available, although the release says that you can already test the export (C#: Add initial support for exporting to Android (GH-73257)

er

There's already a PR (#80521) fixing that issue, hopefully it will be merged soon.

@Zamir7
Copy link

Zamir7 commented Sep 2, 2023

after export, it gives an error on the phone (Godot 4.2 dev4)
Screenshot_20230902-155708

@akien-mga
Copy link
Member

@Zamir7 See the "Limitations" paragraph in the first post. .NET 7.0 doesn't properly support arm32 currently.

@Zamir7
Copy link

Zamir7 commented Sep 2, 2023

@Zamir7 See the "Limitations" paragraph in the first post. .NET 7.0 doesn't properly support arm32 currently.

how about it
1693667410429

@Zamir7
Copy link

Zamir7 commented Sep 11, 2023

On .Net 7, the export is successful, but the application does not function, although it starts, practically nothing works, no matter what you click on the screen, nothing changes, as if the ui buttons do not work, and not only ui.

@ozanyasindogan
Copy link

One of the biggest challenge for me using C# with Godot now for Android deployments is that you can't make use any .Net socket function which requires SSL/TLS. The app crashes immediately. It's working fine on Windows though. It means you can't use HttpClient for https, SSLStream or Grpc over https. There is an open issue about this here and some people are recommending adding libssl.so manually to the APK. I think this is a major issue which should be fixed before the release as most multiplayer games using C# would be needed a TLS connection. Thank you for all your efforts ahead.

@van800
Copy link
Contributor

van800 commented Nov 27, 2023

I looked into debugging C# in the android export once relatively long time ago. godotengine/godot-proposals#3689
Is it worth looking again? Any tips on where to look please?

@raulsntos
Copy link
Member

@van800 Sorry, it's easy to miss comments on closed issues/PRs. I'd love to know more about what issues you have with debugging Android C# applications.

I'm assuming you are asking to support debugging in JetBrains Rider, Godot uses the linux-bionic Mono runtime so whatever Rider uses with those kinds of applications should likely work here too.

Feel free to reach out in the development chat if you have follow-up questions.

@ozanyasindogan
Copy link

@van800 Sorry, it's easy to miss comments on closed issues/PRs. I'd love to know more about what issues you have with debugging Android C# applications.

I'm assuming you are asking to support debugging in JetBrains Rider, Godot uses the linux-bionic Mono runtime so whatever Rider uses with those kinds of applications should likely work here too.

Feel free to reach out in the development chat if you have follow-up questions.

Hi Raul. APK's does not contain libssl dependencies which are required by .Net and any SSL/TLS connection attempt with .Net libraries makes the game crash. The issue explained here #84559, some MS staff even suggested solutions for it and I believe it's a very important bug for .Net developers.

@raulsntos
Copy link
Member

@ozanyasindogan If there's already an open issue, there's no need to bring it up in other issues/PRs. Let's keep the conversation in that issue #84559 so it can be followed by all the interested parties.

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.