dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.92k stars 526 forks source link

LLVM libc++ Android C++ library support for Xamarin Android #4949

Closed raghav-alevoor closed 4 years ago

raghav-alevoor commented 4 years ago

The current build configs for Xamarin Android uses "libstdc++" runtime with the cmake configuration -DANDROID_STL="system" This runtime library may be removed in a future release as discussed in https://github.com/android/ndk/issues/744

Also, the LLVM libc++ is the standard C++ library since Android Lollipop. Full details https://developer.android.com/ndk/guides/cpp-support

The cmake configuration for Xamarin-Android build should also be updated to use libc++_shared.so with the configuration -DDANDROID_STL="c++_shared", so that the binaries are compatible with the current and future version of Android.

The Android Vendor Test Suite ,a requirement for Android compatibility testing, test case "VtsVndkDependencyTest" forbids any linkage against "libstdc++" for ROM installed libraries. Because of the linkage to "libstdc++" in "libmonodroid" any ROM installed Xamarin apk will fail the test.

Can the C++ runtime libraries be updated to LLVM libc++ implementation from the current libstdc++.

Thank you.

Regards, Raghav

grendello commented 4 years ago

Hey @raghav-alevoor we don't want to use the full libstdc++ in Xamarin.Android proper because of the size increase it would cause for all the APKs. Xamarin.Android's C++ code uses no libstdc++ types that require linking against the "full" library and we want it to stay this way for as long as it's possible. If, however, we were to use it, we would link it into the runtime statically. The reason is that the application developed with Xamarin.Android can ship its own version of libstdc++ and we'd end up with symbol conflicts and a situation of potentially disastrous side effects arising from two different versions of libstdc++ living in one process. On the other hand, we must link against something and currently this is the system STL implementation. Regarding the ROM requirement - I understand the issue, but I don't currently see a way to solve with the "general distribution" version of Xamarin.Android. However, I might consider adding a build option to use libc++ so that you, or anyone else who needs to include XA application in the ROMs, can build their own compliant version. I'll also experiment with static linking of the C++ standard library again to see if/how things have changed size-wise since last time I checked.

grendello commented 4 years ago

So, linking libc++ statically into our runtime makes it bigger by around 4-6 times, depending on the architecture. Also, a number of libc++ symbols are exported by the runtime, which is something we mustn't allow. I'll spend some time investigating this as we might not have a choice in the future but to link agains libc++, to see if I can work around the visible symbol issue.

grendello commented 4 years ago

I don't see a way to hide the libc++ symbols without recompiling the STL. Also, the library is compiled with exceptions and RTII and all that baggage is carried into Xamarin.Android runtime even though we explicitly specify we don't use it. About the only way to go away from system I see now is to use ANDROID_STL=none which will cause the runtime to be built with -nostdinc++ -nostdlibc++ which is a bit of a pain because we do use some standard C++ headers even though we don't link the standard library.

raghav-alevoor commented 4 years ago

Hi @grendello, thanks for your comments. I've created a pull request #4952 Tested the locally built XA in an APK, the VTS test is also happy.

Thank you.

brendanzagaeski commented 4 years ago

Release status update

A new Preview version of Xamarin.Android has now been published that includes the fix for this item. The fix is not yet included in a Release version. I will update this item again when a Release version is available that includes the fix.

Fix included in Xamarin.Android SDK version 11.0.99.34.

Fix included on Windows in Visual Studio 2019 version 16.8 Preview 2. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.

Fix included on macOS in Visual Studio 2019 for Mac version 8.8 Preview 2. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.

brendanzagaeski commented 3 years ago

Release status update

A new Release version of Xamarin.Android has now been published that includes the fix for this item.

Fix included in Xamarin.Android SDK version 11.1.0.17.

Fix included on Windows in Visual Studio 2019 version 16.8. To get the new version that includes the fix, check for the latest updates or install the most recent release from https://visualstudio.microsoft.com/downloads/.

Fix included on macOS in Visual Studio 2019 for Mac version 8.8. To get the new version that includes the fix, check for the latest updates on the Stable updater channel.