Search code examples
android.netproguardmauiandroid-r8

Missing class android.window.OnBackInvokedCallback


I am new with dotnet MAUI, still learning while working. I am trying to build my Android Application, I use my physical Device (Android 6.0 API 23).

If I Debug or make Release the app is compiling and building without any errors and crashes and works on my other physical devices. But if I switch R8 (which I need for publishing in Google Play) it crashes. It couldn`t even load MainPage. Staying in Splash Screen and in Visual Studio 2022 Community edition I can see "Application in break mode".

I can`t fix this issue: Warrning: Missing class android.window.OnBackInvokedCallback (referenced from: android.window.OnBackInvokedCallback androidx.activity.OnBackPressedDispatcher.mOnBackInvokedCallback and 4 other contexts)

I installed latest Nuget packages and

Xamarin.AndroidX.Activity 1.6.1

Xamarin.AndroidX.Activity.Compose"1.6.1

Xamarin.AndroidX.Activity.Ktx 1.6.1

Xamarin.AndroidX.AppCompat 1.5.1.1

Xamarin.AndroidX.Core" 1.9.0.1

Xamarin.AndroidX.Legacy.Support.Core.UI" 1.0.0.16

Xamarin.AndroidX.Lifecycle.Runtime" 2.5.1.1

Xamarin.AndroidX.Navigation.Common 2.5.3

Xamarin.AndroidX.Navigation.Runtime 2.5.3

Xamarin.AndroidX.Window 1.0.0.10

Xamarin.AndroidX.Window.WindowExtensions 1.0.0.10-alpha01

But I still can`t solve this problem. I searched a lot, but had no luck to find the right solution.

If someone can help, I will be really thankfull!


Solution

  • Create r8.cfg in platform/android/ folder(just create a class and rename to it).

    Add those in r8.cfg:

    -keep class android.window.** { *; }
    -dontwarn android.window.**
    

    Or

    -keep class android.window.OnBackInvokedCallback
    -dontwarn android.window.OnBackInvokedCallback
    

    Click r8.cfg set build action to proguardConfiguration in propertie window.

    This problem happens because t8Shrinker shrik those class some how, and you can follow those action solve any class not found problem becuase toggled r8 on,just change class name to not found class name(some class are abstract or interface,you should keep their subclass too).