launchdarkly / dotnet-client-sdk

LaunchDarkly Client-side SDK for .NET
Other
7 stars 10 forks source link

Support for net8-android #64

Closed rbakhshi closed 8 months ago

rbakhshi commented 10 months ago

Is this a support request? no

Describe the bug Support for .NET 8 seems to be broken (at least in case of Android)

To reproduce

  1. Create a sample MAUI project using .NET8-rc2
  2. Run the sample to make sure it works on Android simulator
  3. Add a reference to LaunchDarkly.ClientSdk v4 nuget
  4. Try running the sample project again
  5. It will fail with AndroidManifest error image

Error details:

AndroidManifest.xml(28,18): java.exe error AMM0000: C:\Users\home\workspace\net\net8\maui1\MauiApp1\MauiApp1\obj\Debug\net8.0-android\lp\120\jl\AndroidManifest.xml Warning:
AndroidManifest.xml(28,18): java.exe error AMM0000:     Namespace 'androidx.versionedparcelable' used in: AndroidManifest.xml, AndroidManifest.xml.
AndroidManifest.xml(28,18): java.exe error AMM0000: 
AndroidManifest.xml(28,18): java.exe error AMM0000:     Attribute application@appComponentFactory value=(androidx.core.app.CoreComponentFactory) from AndroidManifest.xml:28:18-86
AndroidManifest.xml(28,18): java.exe error AMM0000:     is also present at AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory).
AndroidManifest.xml(28,18): java.exe error AMM0000:     Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:12:3-30:17 to override.
AndroidManifest.xml(28,18): java.exe error AMM0000: Directory 'obj\Debug\net8.0-android\lp\120' is from 'androidx.versionedparcelable.versionedparcelable.aar'.

I suspect the issue is due to references to Xamarin dependencies which are no longer valid for MAUI apps. Below is list of dependencies installed by LaunchDarkly.ClientSdk

MauiApp1

Updates:

LaunchDarkly.ClientSdk.3.1.0 -> LaunchDarkly.ClientSdk.4.0.0
LaunchDarkly.CommonSdk.6.1.0 -> LaunchDarkly.CommonSdk.7.0.0
LaunchDarkly.InternalSdk.3.2.0 -> LaunchDarkly.InternalSdk.3.3.1

Installing:

Xamarin.Android.Arch.Core.Common.1.1.1.1
Xamarin.Android.Arch.Core.Runtime.1.1.1.1
Xamarin.Android.Arch.Lifecycle.Common.1.1.1.1
Xamarin.Android.Arch.Lifecycle.LiveData.1.1.1.1
Xamarin.Android.Arch.Lifecycle.LiveData.Core.1.1.1.1
Xamarin.Android.Arch.Lifecycle.Runtime.1.1.1.1
Xamarin.Android.Arch.Lifecycle.ViewModel.1.1.1.1
Xamarin.Android.Support.Annotations.28.0.0.1
Xamarin.Android.Support.AsyncLayoutInflater.28.0.0.1
Xamarin.Android.Support.Collections.28.0.0.1
Xamarin.Android.Support.Compat.28.0.0.1
Xamarin.Android.Support.CoordinaterLayout.28.0.0.1
Xamarin.Android.Support.Core.UI.28.0.0.1
Xamarin.Android.Support.Core.Utils.28.0.0.1
Xamarin.Android.Support.CursorAdapter.28.0.0.1
Xamarin.Android.Support.CustomView.28.0.0.1
Xamarin.Android.Support.DocumentFile.28.0.0.1
Xamarin.Android.Support.DrawerLayout.28.0.0.1
Xamarin.Android.Support.Fragment.28.0.0.1
Xamarin.Android.Support.Interpolator.28.0.0.1
Xamarin.Android.Support.Loader.28.0.0.1
Xamarin.Android.Support.LocalBroadcastManager.28.0.0.1
Xamarin.Android.Support.Media.Compat.28.0.0.1
Xamarin.Android.Support.Print.28.0.0.1
Xamarin.Android.Support.SlidingPaneLayout.28.0.0.1
Xamarin.Android.Support.SwipeRefreshLayout.28.0.0.1
Xamarin.Android.Support.v4.28.0.0.1
Xamarin.Android.Support.VersionedParcelable.28.0.0.1
Xamarin.Android.Support.ViewPager.28.0.0.1

Also, you can try using v3.1.0. The application starts but LdClient.Init fails with following exception:

System.InvalidOperationException: Unknown underlying handler.  Only AndroidMessageHandler is supported for AndroidClientHandler System.InvalidOperationException: Unknown underlying handler.  Only AndroidMessageHandler is supported for AndroidClientHandler
   at Xamarin.Android.Net.AndroidClientHandler..ctor()
   at LaunchDarkly.Sdk.Client.PlatformSpecific.Http.<>c__DisplayClass2_0.<PlatformGetHttpMessageHandlerFactory>b__0(HttpProperties p)
   at LaunchDarkly.Sdk.Internal.Http.HttpProperties.NewHttpClient()
   at LaunchDarkly.Sdk.Internal.Events.DefaultEventSender..ctor(HttpProperties httpProperties, EventsConfiguration config, Logger logger)
   at LaunchDarkly.Sdk.Client.Integrations.EventProcessorBuilder.Build(LdClientContext context)
   at LaunchDarkly.Sdk.Client.LdClient..ctor(Configuration configuration, Context initialContext, TimeSpan startWaitTime)
   at LaunchDarkly.Sdk.Client.LdClient.CreateInstance(Configuration configuration, Context initialContext, TimeSpan maxWaitTime)
   at LaunchDarkly.Sdk.Client.LdClient.Init(Configuration config, Context initialContext, TimeSpan maxWaitTime)
   at Product.Services.FeatureConfiguration.SetupLdContext()
   at Product.Services.FeatureConfiguration.ContextChanged()
   at Product.Services.ProvisionService.<>c__DisplayClass11_0.<<ProvisionAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Product.Common.ExceptionHelpers.CatchAllAsync(Func`1 action, Action`1 exceptionHandler)

Expected behavior The application should build without any issues.

Logs I think I added the stack trace

SDK version version 4.0.0 Also tried version 3.1.0 and it allows building the project but fails at runtime (Exception details above)

Language version, developer tools .NET 8.0.100-rc.2.23502.2

OS/platform Windows 10

Additional context Add any other context about the problem here.

tanderson-ld commented 10 months ago

Hi @rbakhshi , thank you for reporting this. We have started investigating and will let you know what we find.

tanderson-ld commented 10 months ago

This issue has been logged internally as 221860 and is still being investigated.

tanderson-ld commented 10 months ago

@rbakhshi , we have been able to reproduce the issue and are currently evaluating options to handle the underlying AndroidX vs. Android.Support.v4 compatibility in the ecosystem.

To provide us confirmation, if you add the Xamarin.AndroidX.Migration package to your project, does your build error get resolved? You may need to clean your project.

rbakhshi commented 10 months ago

Thanks @tanderson-ld, currently out of office. Will check when I get back.

rbakhshi commented 10 months ago

ok @tanderson-ld I can confirm that at least the build error is now gone. For some "technical" reasons I cannot test the actual runtime behaviour right now.

I needed to add these to the project file:

  <ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">
    <PackageReference Include="Xamarin.AndroidX.Migration" Version="1.0.10" />
    <PackageReference Include="Xamarin.AndroidX.Legacy.Support.V4" Version="1.0.0.13" />
  </ItemGroup>
tanderson-ld commented 10 months ago

Thank you for looking into that and confirming it builds! We are still discussing this internally. Are those dependencies satisfactory for you as a work around for the moment?

Would you mind also letting us know if with those migration dependencies in place, do you still see the Only AndroidMessageHandler is supported for AndroidClientHandler error at runtime?

rbakhshi commented 10 months ago

OK tested the new build and unfortunately still fails with similar error. Here is the error when running v4.0.0 sdk with above Xamarin.AndroidX.Migration package:

FATAL EXCEPTION: main
Process: xxxx, PID: 12953
android.runtime.JavaProxyThrowable: System.InvalidOperationException: Unknown underlying handler.  Only AndroidMessageHandler is supported for AndroidClientHandler
   at Xamarin.Android.Net.AndroidClientHandler..ctor()
   at LaunchDarkly.Sdk.Client.PlatformSpecific.Http.<>c__DisplayClass2_0.<PlatformGetHttpMessageHandlerFactory>b__0(HttpProperties p)
   at LaunchDarkly.Sdk.Internal.Http.HttpProperties.NewHttpClient()
   at LaunchDarkly.Sdk.Internal.Events.DefaultEventSender..ctor(HttpProperties httpProperties, EventsConfiguration config, Logger logger)
   at LaunchDarkly.Sdk.Client.Integrations.EventProcessorBuilder.Build(LdClientContext context)
   at LaunchDarkly.Sdk.Client.LdClient..ctor(Configuration configuration, Context initialContext, TimeSpan startWaitTime)
   at LaunchDarkly.Sdk.Client.LdClient.CreateInstance(Configuration configuration, Context initialContext, TimeSpan maxWaitTime)
   at LaunchDarkly.Sdk.Client.LdClient.Init(Configuration config, Context initialContext, TimeSpan maxWaitTime)
tanderson-ld commented 10 months ago

@rbakhshi , I still need to investigate that InvalidOperationException. I've been treating them as two separate errors for the moment. Will look into that one once we have the first error corrected.

tanderson-ld commented 10 months ago

@rbakhshi , we've decided we're going to update our implementation to stop using monoandroid and begin using .netX.X-android. Still investigating exactly which .netX.X-android version we will use to make sure we maintain compatibility for customers. Are you currently shipping a MAUI app that was using LaunchDarkly Dotnet Client v3.1.0? Or perhaps this is a first attempt at using Launch Darkly in your application?

rbakhshi commented 10 months ago

@tanderson-ld we have a xamarin app using LD client which we are porting to. Net 8 and Maui. (Considering the xamarin end of life approaching)

tanderson-ld commented 10 months ago

@rbakhshi , we are planning to target net7.0 for our library which I believe will be compatible with your project. Would you be interested in alpha testing a distribution when we have our first alpha ready? Hopefully within the next week.

rbakhshi commented 10 months ago

Yes, I would be interested! Thanks

tanderson-ld commented 10 months ago

@rbakhshi , just wanted to keep you updated. The last thing I need to do to make the alpha is to update our build and CI infrastructure. That should be the last step. I'll reach back out when I have the alpha.

rbakhshi commented 10 months ago

Awesome news! Thanks @tanderson-ld

rbakhshi commented 9 months ago

Just an update here: I've been referencing v4 in the app but commented out the initialization part. (Just to run the rest of app without launchdarkly). This was all fine.

Running Release though, will still fail on Android Linker step. Removing reference to launchdarkly nuget stops the linker failure.

this issue (https://github.com/xamarin/xamarin-android/issues/8208) made me suspect it could be related to launchdarkly's current nuget package

tanderson-ld commented 9 months ago

@rbakhshi , I haven't forgotten about you! Juggling a few different priorities at the moment.

indyjason79 commented 9 months ago

We were using ClientSdk v3.1 and upgraded our solution to .Net 8 and this package to v4.0. After also running into the compiler error mentioned above, I reverted back to v3.1 and the app compiles now but crashes on startup. Not sure if it's related to this lib or not.

Has anyone else been able to get LaunchDarkly.ClientSdk v3.1 to work with .Net 8 Android?

tanderson-ld commented 9 months ago

A significant slowdown was caused by refactoring of our CI/CD test frameworks to use Maui, but we just got unblocked on that technical issue, so hopefully we get to an alpha soon.

tanderson-ld commented 8 months ago

Hi @rbakhshi , we just put the alpha up on Nuget. The version is 5.0.0-alpha.1. Sorry for the inaccurate estimate originally. Please let us know if you run into any issues! Thank you again for your patience.

rbakhshi commented 8 months ago

Hi @tanderson-ld thanks for the update.

Unfortunately I am getting the same error even with 5.0.0-alpha.1 release:

System.InvalidOperationException: Unknown underlying handler.  Only AndroidMessageHandler is supported for AndroidClientHandler
   at Xamarin.Android.Net.AndroidClientHandler..ctor() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Xamarin.Android.Net/AndroidClientHandler.cs:line 70
   at LaunchDarkly.Sdk.Client.PlatformSpecific.Http.<>c__DisplayClass2_0.<PlatformGetHttpMessageHandlerFactory>b__0(HttpProperties p)
   at LaunchDarkly.Sdk.Internal.Http.HttpProperties.NewHttpClient()
   at LaunchDarkly.Sdk.Internal.Events.DefaultEventSender..ctor(HttpProperties httpProperties, EventsConfiguration config, Logger logger)
   at LaunchDarkly.Sdk.Client.Integrations.EventProcessorBuilder.Build(LdClientContext context)
   at LaunchDarkly.Sdk.Client.LdClient..ctor(Configuration configuration, Context initialContext, TimeSpan startWaitTime)
   at LaunchDarkly.Sdk.Client.LdClient.CreateInstance(Configuration configuration, Context initialContext, TimeSpan maxWaitTime)
   at LaunchDarkly.Sdk.Client.LdClient.Init(Configuration config, Context initialContext, TimeSpan maxWaitTime)

image

tanderson-ld commented 8 months ago

I do see a warning in our build related to the deprecation of AndroidClientHandler, but I haven't been able to reproduce the runtime exception you are encountering. I can update from AndroidClientHandler to AndroidMessageHandler and put out a new alpha to see if that addresses the problem for you.

What Android API level are you targeting with your app?

tanderson-ld commented 8 months ago

@rbakhshi , could you provide a snippet with your Configuration setup? I'd like to test with as similar a setup.

tanderson-ld commented 8 months ago

We released version 5.0.0-alpha.2 which uses the AndroidMessageHandler instead. I haven't been able to reproduce the issue unfortunately.

rbakhshi commented 8 months ago

Hi @tanderson-ld , thanks for the update. I tested a clean new project and even alpha1 seems to work. That is great. Now I need to check why I still get that above error.

I will try alpha2 in a bit and update you

rbakhshi commented 8 months ago

ok I can confirm that alpha.2 works fine for us. thanks @tanderson-ld

tanderson-ld commented 8 months ago

Great! Thank you for your patience while we resolved it. Please let us know if you see any other issues!

rbakhshi commented 7 months ago

@tanderson-ld when targeting API 34 for android, the app fails when initializing launch darkly:

Seems like related to this: https://github.com/dotnet/maui/issues/17861

System.TypeInitializationException: The type initializer for 'LaunchDarkly.Sdk.Client.PlatformSpecific.PlatformConnectivity' threw an exception.
    ---> Java.Lang.SecurityException: com.my.app: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
    ---> Android.OS.RemoteException: Remote stack trace:
    at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:13927)
    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2570)
    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2720)
    at android.os.Binder.execTransactInternal(Binder.java:1339)
    at android.os.Binder.execTransact(Binder.java:1275)

    --- End of managed Android.OS.RemoteException stack trace ---
android.os.RemoteException: Remote stack trace:
    at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:13927)
    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2570)
    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2720)
    at android.os.Binder.execTransactInternal(Binder.java:1339)
    at android.os.Binder.execTransact(Binder.java:1275)

    --- End of managed Android.OS.RemoteException stack trace ---
android.os.RemoteException: Remote stack trace:
    at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:13927)
    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2570)
    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2720)
    at android.os.Binder.execTransactInternal(Binder.java:1339)
    at android.os.Binder.execTransact(Binder.java:1275)

    --- End of inner exception stack trace ---
    at Java.Interop.JniEnvironment.InstanceMethods.CallNonvirtualObjectMethod(JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args)
    at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualObjectMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters)
    at Android.Content.ContextWrapper.RegisterReceiver(BroadcastReceiver receiver, IntentFilter filter)
    at Microsoft.Maui.Networking.ConnectivityImplementation.StartListeners()
    at Microsoft.Maui.Networking.ConnectivityImplementation.add_ConnectivityChanged(EventHandler`1 value)
    at LaunchDarkly.Sdk.Client.PlatformSpecific.PlatformConnectivity..cctor()
    --- End of managed Java.Lang.SecurityException stack trace ---
java.lang.SecurityException: com.my.app: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
    at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
    at android.os.Parcel.createException(Parcel.java:3041)
    at android.os.Parcel.readException(Parcel.java:3024)
    at android.os.Parcel.readException(Parcel.java:2966)
    at android.app.IActivityManager$Stub$Proxy.registerReceiverWithFeature(IActivityManager.java:5684)
    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1852)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1792)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1780)
    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:755)
Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.am.ActivityManagerService.registerReceiverWithFeature(ActivityManagerService.java:13927)
    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2570)
    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2720)
    at android.os.Binder.execTransactInternal(Binder.java:1339)
    at android.os.Binder.execTransact(Binder.java:1275)
tanderson-ld commented 7 months ago

Hi again @rbakhshi. Just to confirm, is it the case that you do NOT encounter this issue on LD Dotnet SDK 5.0.0 and Android target API 33? Just want to make sure this truly only happens when switching to target API 34. I'm catching up on that other issue you sent, thank you for doing some digging.

rbakhshi commented 7 months ago

Correct. Using v5.0.0 and API 33 is fine

rbakhshi commented 6 months ago

I raised a separate bug for this here https://github.com/launchdarkly/dotnet-client-sdk/issues/89