maui icon indicating copy to clipboard operation
maui copied to clipboard

Microsoft.Maui.Handlers.ButtonHandler' threw an exception. On Release Build - App Launch

Open LeoJHarris opened this issue 1 year ago • 4 comments

Description

Appears to be same as https://github.com/dotnet/maui/issues/23954 which was closed for no apparent reason. Was this fixed? Our app runs fine on Debug mode but crashes immediately in Release, see relevant log output when attempting to launch the app on real device.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.91 SR9.1

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 14

Did you find any workaround?

No

Relevant log output

Time	Device Name	Type	PID	Tag	Message
10-09 12:59:54.144		Error	14733	AndroidRuntime	android.runtime.JavaProxyThrowable: [System.TypeInitializationException]: The type initializer for 'Microsoft.Maui.Handlers.ButtonHandler' threw an exception.
	at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.RemapForControls(Unknown Source:0)
	at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.SetupDefaults(Unknown Source:0)
	at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.UseMauiPrimaryApp(Unknown Source:0)
	at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.UseMauiApp(Unknown Source:0)
	at SampleApp.MauiProgram.CreateMauiApp(Unknown Source:0)
	at SampleApp.MainApplication.CreateMauiApp(Unknown Source:0)
	at Microsoft.Maui.MauiApplication.OnCreate(Unknown Source:0)
	at Android.App.Application.n_OnCreate(Unknown Source:0)
	at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(Unknown Source:0)
	at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
	at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
	at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7711)
	at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478)
	at android.os.Handler.dispatchMessage(Handler.java:106)
	at android.os.Looper.loopOnce(Looper.java:230)
	at android.os.Looper.loop(Looper.java:319)
	at android.app.ActivityThread.main(ActivityThread.java:8919)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

LeoJHarris avatar Oct 09 '24 00:10 LeoJHarris

Could you provide us with a sample project so we can investigate it further? Looking forward to your reply!

ninachen03 avatar Oct 09 '24 10:10 ninachen03

This happened to me today out of nowhere. The project was working fine. I decided to add some new functionality that wasn't wired up, but required adding Microsoft.Maui.Graphics.Skia nuget package. I added the 9.0x rc. The first attempt to run it on a device resulted in:

: 'The type initializer for 'Microsoft.Maui.Handlers.ButtonHandler' threw an exception.'

I removed Microsoft.Maui.Graphics.Skia and added back the stable 8.0.9.1 version and that worked.

My guess is that this is a .net 9.0 compat issue.

nixkuroi avatar Oct 12 '24 09:10 nixkuroi

@nixkuroi we dont use Skia anywhere and still have the issue

LeoJHarris avatar Oct 13 '24 19:10 LeoJHarris

Any update or am I the only one having this issue?

LeoJHarris avatar Oct 23 '24 02:10 LeoJHarris

Hi ! I'm having the exact same issue in release mode, hopefuly i tested the production release before releasing it to the public !

Skarzag avatar Oct 29 '24 15:10 Skarzag

@Skarzag its completely blocking us from an Android release atm, fortunately the final Xamarin.Android build we released is still sitting stable on the play store but should we need to get an urgent fix for whatever reason we are completely stuck and that includes iOS with the latest apple authentication issue.

We use the following NuGets packages

<!-- begins shared packages -->
<ItemGroup>
	<PackageReference Include="Microsoft.AppCenter" Version="5.0.6" />
	<PackageReference Include="Microsoft.AppCenter.Analytics" Version="5.0.6" />
	<PackageReference Include="Microsoft.AppCenter.Crashes" Version="5.0.6" />
	<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.10.0" />
	<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
	<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
	<PackageReference Update="Microsoft.Maui.Controls" Version="8.0.92" />
	<PackageReference Update="Microsoft.Maui.Controls.Compatibility" Version="8.0.92" />
	<PackageReference Include="CommunityToolkit.Maui" Version="9.1.0" />
	<PackageReference Include="CommunityToolkit.Maui.MediaElement" Version="4.1.2" />
	<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
	<PackageReference Include="EzPasswordValidator" Version="2.1.0" />
	<PackageReference Include="GeoTimeZone" Version="5.3.0" />
	<PackageReference Include="LocalizationResourceManager.Maui" Version="1.3.0-alpha.1" />
	<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.10" />
	<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
	<PackageReference Include="Onion.Maui.GoogleMaps" Version="6.0.1" />
	<PackageReference Include="Oscore.Maui.Biometric" Version="1.0.1" />
	<PackageReference Include="Plugin.LocalNotification" Version="11.1.4" />
	<PackageReference Include="Plugin.StoreReview" Version="6.2.0" />
	<PackageReference Include="Prism.DryIoc.Maui" Version="9.0.537" />
	<PackageReference Include="Prism.Maui.Rx" Version="9.0.537" />
	<PackageReference Include="Sharpnado.Tabs.Maui" Version="3.2.1" />
	<PackageReference Include="YoutubeExplode" Version="6.4.3" />
	<PackageReference Include="ZXing.Net.Maui.Controls" Version="0.4.0" />
</ItemGroup>
<!--ends shared packages-->

<!--begins android packages-->
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android34.0'">
	<PackageReference Include="Xamarin.AndroidX.Collection">
		<Version>1.4.4</Version>
	</PackageReference>
	<PackageReference Include="Xamarin.AndroidX.Collection.Ktx">
		<Version>1.4.4</Version>
	</PackageReference>
	<PackageReference Include="Xamarin.AndroidX.Fragment.Ktx">
		<Version>1.8.4</Version>
	</PackageReference>
	<PackageReference Include="Xamarin.AndroidX.Lifecycle.Common">
		<Version>2.8.6</Version>
	</PackageReference>
	<PackageReference Include="Xamarin.AndroidX.Lifecycle.Runtime">
		<Version>2.8.6</Version>
	</PackageReference>
	<PackageReference Include="Xamarin.Firebase.Core">
		<Version>121.1.1.11</Version>
	</PackageReference>
	<PackageReference Include="Xamarin.Firebase.Crashlytics">
		<Version>119.2.1</Version>
	</PackageReference>
	<PackageReference Include="Xamarin.Firebase.Messaging">
		<Version>124.0.3</Version>
	</PackageReference>
	<PackageReference Include="Xamarin.GooglePlayServices.Auth.Api.Phone">
		<Version>118.1.0.2</Version>
	</PackageReference>
</ItemGroup>
<!--ends android packages-->

LeoJHarris avatar Oct 29 '24 20:10 LeoJHarris

Just found what was our problem : This doesn't happen on iOS so i focused on Android and... I've deactivated R8 tool in the app project and everything is back to normal.

<!--DISABLED BECAUSE IT CRASH AT STARTUP ON ANDROID RELEASE <AndroidLinkTool>r8</AndroidLinkTool>-->

Skarzag avatar Oct 30 '24 08:10 Skarzag

@Skarzag suggestions also resolved it for me, I've just disabled both D8 & r8 but I did suspect something like this earlier and I tried playing around with the proguard.cfg file without any luck to prevent necessary classes and methods are not removed or obfuscated by ProGuard.

@PureWeen @samhouts @jonathanpeppers any ideas or do we just disable this for the time being?

LeoJHarris avatar Oct 30 '24 19:10 LeoJHarris

If you can get the adb logcat output, it will show the Java class that r8 removed in the error. You should then be able to add a ProGuard rule to preserve the Java class.

AndroidLinkMode=r8 is a more advanced feature that is not on by default, because most apps require a few rules in order to work.

jonathanpeppers avatar Oct 30 '24 19:10 jonathanpeppers

@jonathanpeppers I am just searching through the the logcat now from the time the app started to launch until it crashed. Is there a particular tag to look for or message when searching information on the Java class that r8 removed?

LeoJHarris avatar Oct 30 '24 19:10 LeoJHarris

Usually, it's a crash and your app closes, and there is a Java stacktrace of the missing type.

jonathanpeppers avatar Oct 30 '24 20:10 jonathanpeppers

@jonathanpeppers The java stacktrace from logcat:

10-31 09:09:01.982 31702 31702 I MonoDroid: Android.Runtime.JavaProxyThrowable: Exception of type 'Android.Runtime.JavaProxyThrowable' was thrown.
10-31 09:09:01.982 31702 31702 I MonoDroid:
10-31 09:09:01.982 31702 31702 I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
10-31 09:09:01.982 31702 31702 I MonoDroid: android.runtime.JavaProxyThrowable: [System.TypeInitializationException]: The type initializer for 'Microsoft.Maui.Handlers.ButtonHandler' threw an exception.
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.RemapForControls(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.SetupDefaults(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.UseMauiPrimaryApp(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.UseMauiApp(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at SampleApp.MauiProgram.CreateMauiApp(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at SampleApp.MainApplication.CreateMauiApp(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.MauiApplication.OnCreate(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Android.App.Application.n_OnCreate(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7711)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.os.Handler.dispatchMessage(Handler.java:106)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.os.Looper.loopOnce(Looper.java:230)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.os.Looper.loop(Looper.java:319)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.ActivityThread.main(ActivityThread.java:8919)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at java.lang.reflect.Method.invoke(Native Method)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
10-31 09:09:01.982 31702 31702 I MonoDroid:
10-31 09:09:01.982 31702 31702 I MonoDroid:   --- End of managed Android.Runtime.JavaProxyThrowable stack trace ---
10-31 09:09:01.982 31702 31702 I MonoDroid: android.runtime.JavaProxyThrowable: [System.TypeInitializationException]: The type initializer for 'Microsoft.Maui.Handlers.ButtonHandler' threw an exception.
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.RemapForControls(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.SetupDefaults(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.UseMauiPrimaryApp(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.Controls.Hosting.AppHostBuilderExtensions.UseMauiApp(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at SampleApp.MauiProgram.CreateMauiApp(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at SampleApp.MainApplication.CreateMauiApp(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Microsoft.Maui.MauiApplication.OnCreate(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Android.App.Application.n_OnCreate(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7711)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.os.Handler.dispatchMessage(Handler.java:106)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.os.Looper.loopOnce(Looper.java:230)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.os.Looper.loop(Looper.java:319)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at android.app.ActivityThread.main(ActivityThread.java:8919)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at java.lang.reflect.Method.invoke(Native Method)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
10-31 09:09:01.982 31702 31702 I MonoDroid:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
10-31 09:09:01.982 31702 31702 I MonoDroid:

I trying the following rules

# Keep the ButtonHandler class and its methods
-keep class Microsoft.Maui.Handlers.ButtonHandler { *; }

# Keep all classes that extend ButtonHandler
-keep class * extends Microsoft.Maui.Handlers.ButtonHandler { *; }

# Keep all methods in the Microsoft.Maui.Handlers package
-keep class Microsoft.Maui.Handlers.** { *; }
}

LeoJHarris avatar Oct 30 '24 20:10 LeoJHarris

@jonathanpeppers just checking as well, I have added my proguard.cfg to our MAUI app but we dont have the old ProGuardConfiguration build action anymore, I assume we can leave this as 'None' as long as the file is in the root Android project folder?

LeoJHarris avatar Oct 30 '24 20:10 LeoJHarris

It looks to me like the inner exception is lost: The type initializer for 'Microsoft.Maui.Handlers.ButtonHandler' threw an exception. It doesn't say what the exception was. Hard to know if this is an r8 issue at all, without seeing the underlying exception. Does a new ButtonHandler() call somewhere in the app, show the full error?

ProguardConfiguration is the item group you use:

  • https://github.com/dotnet/android/blob/ea6734fa342687deaf5616449cf5c074978d4205/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets#L28

This should still be in @(AvailableItemName) and show in Visual Studio, but I don't know what it would do in multi-targeted MAUI projects.

jonathanpeppers avatar Oct 30 '24 20:10 jonathanpeppers

@jonathanpeppers we don't have any references or calls to new ButtonHandler() and neither do we even have any ButtonHandlers setup.

You would like me to add new ButtonHandler() somewhere in the app and rerun the app with logcat? If so where should I put the line of code?

LeoJHarris avatar Oct 30 '24 20:10 LeoJHarris

ProguardConfiguration is the item group you use:

  • https://github.com/dotnet/android/blob/ea6734fa342687deaf5616449cf5c074978d4205/src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.AvailableItems.targets#L28

This should still be in @(AvailableItemName) and show in Visual Studio, but I don't know what it would do in multi-targeted MAUI projects.

I have setup as follows in .csproj so don't think this is an issue with actually including the proguard:

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android34.0'">
	<ProguardConfiguration Include="Platforms\Android\proguard.cfg" />
</ItemGroup>

LeoJHarris avatar Oct 30 '24 20:10 LeoJHarris

You would like me to add new ButtonHandler() somewhere in the app and rerun the app with logcat? If so where should I put the line of code?

My suggestion was to put it somewhere before the app crashes, to see if you get the inner exception message. Sometimes, if System.Reflection is the caller, and you get TypeLoadException, you lose the inner exception.

jonathanpeppers avatar Oct 30 '24 20:10 jonathanpeppers

@jonathanpeppers correct me if wrong but I have tried in the MainActivity OnCreated method but the crash already was occuring so I tried in the MAUIApplication.cs file from the Android project

protected override MauiApp CreateMauiApp()
{
    Log.Debug("ButtonHandler", "Before ButtonHandler called");
    new ButtonHandler();
    Log.Debug("ButtonHandler", "After ButtonHandler called");

    return MauiProgram.CreateMauiApp();
}

The logs seem to be the same:

10-31 10:26:21.390 16362 16362 D ButtonHandler: Before ButtonHandler called
10-31 10:26:21.391 16362 16413 E lservices.SampleApp: No package ID 77 found for resource ID 0x770b000f.
10-31 10:26:21.391 16362 16413 E lservices.SampleApp: No package ID 77 found for resource ID 0x770b000f.
10-31 10:26:21.393 16362 16413 I FA      : App measurement initialized, version: 108007
10-31 10:26:21.393 16362 16413 I FA      : To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
10-31 10:26:21.393 16362 16413 I FA      : To enable faster debug mode event logging run:
10-31 10:26:21.393 16362 16413 I FA      :   adb shell setprop debug.firebase.analytics.app ########
10-31 10:26:21.429  4679  4679 D BoundBrokerSvc: onBind: Intent { act=com.google.android.gms.measurement.START pkg=com.google.android.gms }
10-31 10:26:21.429  4679  4679 D BoundBrokerSvc: Loading bound service for intent: Intent { act=com.google.android.gms.measurement.START pkg=com.google.android.gms }
10-31 10:26:21.435 16362 16362 D AndroidRuntime: Shutting down VM
10-31 10:26:21.435 16362 16362 E AndroidRuntime: FATAL EXCEPTION: main
10-31 10:26:21.435 16362 16362 E AndroidRuntime: Process: ########, PID: 16362
10-31 10:26:21.435 16362 16362 E AndroidRuntime: android.runtime.JavaProxyThrowable: [System.TypeInitializationException]: The type initializer for 'Microsoft.Maui.Handlers.ButtonHandler' threw an exception.
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at SampleApp.MainApplication.CreateMauiApp(Unknown Source:0)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at Microsoft.Maui.MauiApplication.OnCreate(Unknown Source:0)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at Android.App.Application.n_OnCreate(Unknown Source:0)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(Unknown Source:0)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7711)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:106)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at android.os.Looper.loopOnce(Looper.java:230)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:319)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:8919)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
10-31 10:26:21.435 16362 16362 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

LeoJHarris avatar Oct 30 '24 21:10 LeoJHarris

With a catch block, is there an InnerException? I would log both exc.ToString() and exc.InnerException to see if there is any more information.

jonathanpeppers avatar Oct 30 '24 21:10 jonathanpeppers

@jonathanpeppers getting more information now:

10-31 10:51:04.231  6296  6296 E ButtonHandler: System.TypeInitializationException: The type initializer for 'Microsoft.Maui.Handlers.ButtonHandler' threw an exception.
10-31 10:51:04.231  6296  6296 E ButtonHandler:  ---> Java.Lang.ClassNotFoundException: com.microsoft.maui.PlatformInterop
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniEnvironment.Types.TryFindClass(String classname, Boolean throwOnError)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniEnvironment.Types.FindClass(String classname)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniType..ctor(String classname)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniType.GetCachedJniType(JniType& cachedType, String classname)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniPeerMembers.get_JniPeerType()
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniPeerMembers.JniStaticMethods.GetMethodInfo(String method, String signature)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniPeerMembers.JniStaticMethods.GetMethodInfo(String encodedMember)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniPeerMembers.JniStaticMethods.InvokeObjectMethod(String encodedMember, JniArgumentValue* parameters)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Microsoft.Maui.PlatformInterop.GetDefaultColorStateList(Int32 color)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Microsoft.Maui.Platform.ColorStateListExtensions.CreateDefault(Int32 color)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Microsoft.Maui.Platform.ColorExtensions.ToDefaultColorStateList(Color color)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Microsoft.Maui.Handlers.ButtonHandler..cctor()
10-31 10:51:04.231  6296  6296 E ButtonHandler:   --- End of managed Java.Lang.ClassNotFoundException stack trace ---
10-31 10:51:04.231  6296  6296 E ButtonHandler: java.lang.ClassNotFoundException: com.microsoft.maui.PlatformInterop
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7711)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Handler.dispatchMessage(Handler.java:106)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Looper.loopOnce(Looper.java:230)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Looper.loop(Looper.java:319)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.main(ActivityThread.java:8919)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at java.lang.reflect.Method.invoke(Native Method)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
10-31 10:51:04.231  6296  6296 E ButtonHandler:
10-31 10:51:04.231  6296  6296 E ButtonHandler:   --- End of managed Java.Lang.ClassNotFoundException stack trace ---
10-31 10:51:04.231  6296  6296 E ButtonHandler: java.lang.ClassNotFoundException: com.microsoft.maui.PlatformInterop
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7711)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Handler.dispatchMessage(Handler.java:106)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Looper.loopOnce(Looper.java:230)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Looper.loop(Looper.java:319)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.main(ActivityThread.java:8919)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at java.lang.reflect.Method.invoke(Native Method)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
10-31 10:51:04.231  6296  6296 E ButtonHandler:
10-31 10:51:04.231  6296  6296 E ButtonHandler:    --- End of inner exception stack trace ---
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at SampleApp.MainApplication.CreateMauiApp()
10-31 10:51:04.231  6296  6296 E ButtonHandler: Java.Lang.ClassNotFoundException: com.microsoft.maui.PlatformInterop
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniEnvironment.Types.TryFindClass(String classname, Boolean throwOnError)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniEnvironment.Types.FindClass(String classname)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniType..ctor(String classname)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniType.GetCachedJniType(JniType& cachedType, String classname)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniPeerMembers.get_JniPeerType()
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniPeerMembers.JniStaticMethods.GetMethodInfo(String method, String signature)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniPeerMembers.JniStaticMethods.GetMethodInfo(String encodedMember)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Java.Interop.JniPeerMembers.JniStaticMethods.InvokeObjectMethod(String encodedMember, JniArgumentValue* parameters)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Microsoft.Maui.PlatformInterop.GetDefaultColorStateList(Int32 color)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Microsoft.Maui.Platform.ColorStateListExtensions.CreateDefault(Int32 color)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Microsoft.Maui.Platform.ColorExtensions.ToDefaultColorStateList(Color color)
10-31 10:51:04.231  6296  6296 E ButtonHandler:    at Microsoft.Maui.Handlers.ButtonHandler..cctor()
10-31 10:51:04.231  6296  6296 E ButtonHandler:   --- End of managed Java.Lang.ClassNotFoundException stack trace ---
10-31 10:51:04.231  6296  6296 E ButtonHandler: java.lang.ClassNotFoundException: com.microsoft.maui.PlatformInterop
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7711)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Handler.dispatchMessage(Handler.java:106)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Looper.loopOnce(Looper.java:230)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Looper.loop(Looper.java:319)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.main(ActivityThread.java:8919)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at java.lang.reflect.Method.invoke(Native Method)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
10-31 10:51:04.231  6296  6296 E ButtonHandler:
10-31 10:51:04.231  6296  6296 E ButtonHandler:   --- End of managed Java.Lang.ClassNotFoundException stack trace ---
10-31 10:51:04.231  6296  6296 E ButtonHandler: java.lang.ClassNotFoundException: com.microsoft.maui.PlatformInterop
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at crc6488302ad6e9e4df1a.MauiApplication.n_onCreate(Native Method)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at crc6488302ad6e9e4df1a.MauiApplication.onCreate(MauiApplication.java:28)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1316)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7711)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Handler.dispatchMessage(Handler.java:106)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Looper.loopOnce(Looper.java:230)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.os.Looper.loop(Looper.java:319)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at android.app.ActivityThread.main(ActivityThread.java:8919)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at java.lang.reflect.Method.invoke(Native Method)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
10-31 10:51:04.231  6296  6296 E ButtonHandler:         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)

LeoJHarris avatar Oct 30 '24 21:10 LeoJHarris

After adding the following proguard rule I got past the above issue.

-keep class com.microsoft.maui.PlatformInterop { *; }

Resolving the above then raised a few more missing rules though they appeared specific to my project such as the following:

-keep class com.microsoft.appcenter.AppCenter { *; }
-keep class com.microsoft.appcenter.analytics.Analytics { *; }
-keep class com.microsoft.appcenter.crashes.Crashes { *; }

As a side note from the build output, does the following have any cause for concern?

obj\Release\net8.0-android34.0\android-arm64\proguard\proguard_project_references.cfg : warning XA4304: ProGuard configuration file 'obj\Release\net8.0-android34.0\android-arm64\proguard\proguard_project_references.cfg' was not found.

LeoJHarris avatar Oct 31 '24 01:10 LeoJHarris

-keep class com.microsoft.maui.PlatformInterop { *; }

For this rule, we should automatically add it to .NET MAUI. I can look into that.

obj\Release\net8.0-android34.0\android-arm64\proguard\proguard_project_references.cfg : warning XA4304: ProGuard configuration file 'obj\Release\net8.0-android34.0\android-arm64\proguard\proguard_project_references.cfg' was not found.

This warning should be fixed in .NET 9:

  • https://github.com/dotnet/android/pull/8641

jonathanpeppers avatar Oct 31 '24 14:10 jonathanpeppers

-keep class com.microsoft.maui.PlatformInterop { *; }

For this rule, we should automatically add it to .NET MAUI. I can look into that.

I added this to my ProGuard rules file and I still get this same error. It's like this is ignored, or something else is removing it (?)

brunck avatar Nov 22 '24 19:11 brunck

They don't have default wildcards for proguard files:

https://github.com/dotnet/maui/blob/f6424476e57c1c25cc587f164b90bceb1681cd37/src/Controls/src/Build.Tasks/nuget/buildTransitive/netstandard2.0/Microsoft.Maui.Controls.Globs.props#L43-L55

So, you probably need <ItemGroup><ProguardConfiguration Include="....

The Android set is here, but MAUI overwrites them with EnableDefaultAndroidItems=false:

  • https://github.com/dotnet/android/blob/6394773fad5108b0d7b4e6f087dc3e6ea997401a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/Sdk/AutoImport.props#L60-L62

jonathanpeppers avatar Nov 22 '24 19:11 jonathanpeppers

Is there a way to do -printusage usage.txt? It doesn't seem to output anything when I try it.

brunck avatar Nov 22 '24 22:11 brunck

Moving this to enhancement as we probably should have rules in places to prevent r8 from removing core maui components.

@jonathanpeppers if the interop is being used, why is it being removed?

mattleibow avatar Dec 05 '24 13:12 mattleibow

Generally r8 has no idea if C# calls a Java method, so it will remove them otherwise. It doesn't think anyone called it from Java.

jonathanpeppers avatar Dec 05 '24 14:12 jonathanpeppers

I'm also getting this issue with Java.Lang.ClassNotFoundException: Didn't find class "com.microsoft.maui.PlatformInterop" on path: DexPathList

Turning off R8 linker fixes it.

Jakar510 avatar Jan 06 '25 23:01 Jakar510

No idea why but R8 is suddenly crashing my app in Release on 9.0.40. It crashes during startup when it is just displaying the splash so I am so far not getting anything useful to work with.

Similar to the other comments here I note


  <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net9.0-android|AnyCPU'">
    <AndroidPackageFormat>apk</AndroidPackageFormat>
	<AndroidLinkTool>r8</AndroidLinkTool>
	  <R8Options>-printusage usage.txt</R8Options>

Does not work. It should create usage.txt file that shows all the things that were stripped out so you can try to find obvious problems but no usage.txt is made anywhere.

jonmdev avatar Feb 21 '25 09:02 jonmdev

@jonathanpeppers @mattleibow I've looped back to this issue, was determined to get r8 & d8 enabled on our app which it is now after adding a few more rules but looks like another core MAUI component missing with the required proguard rule to fix it:

-keep class com.microsoft.maui.PlatformFontSpan { *; }

LeoJHarris avatar Apr 08 '25 23:04 LeoJHarris