Android App suddenly crashes when compiling in Release Mode and running on connected phone on Viisual Studio 2022

Riffy 276 Reputation points
2022-04-30T23:03:51.713+00:00

Hi

My App was working fine when build to release mode, and both when downloading from Google play store or from a Android device connected to computer.

I am using Xamarin VS Studio 2022 and C#.

But it has suddenly started failing.

I get the following error among others:

Time Device Name Type PID Tag Message
04-30 15:15:19.075 Samsung SM-J320FN Error 19103 AndroidRuntime Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.appcompat.widget.FitWindowsFrameLayout" on path: DexPathList[[zip file "/data/app/com.proj.proj-2/base.apk", zip file "/data/app/com.proj.proj-2/split_config.armeabi_v7a.apk", zip file "/data/app/com.asirkl.irkl-2/split_config.en.apk", zip file "/data/app/com.asirkl.irkl-2/split_config.xhdpi.apk"],nativeLibraryDirectories=[/data/app/com.proj.proj-2/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at android.view.LayoutInflater.createView(LayoutInflater.java:578)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
... 24 more
Suppressed: java.io.IOException: Zip archive '/data/app/com.proj.proj-2/split_config.armeabi_v7a.apk' doesn't contain classes.dex (error msg: Entry not found)
at dalvik.system.DexFile.openDexFileNative(Native Method)
at dalvik.system.DexFile.openDexFile(DexFile.java:295)
at dalvik.system.DexFile.<init>(DexFile.java:80)
at dalvik.system.DexFile.<init>(DexFile.java:59)
at dalvik.system.DexPathList.loadDexFile(DexPathList.java:262)
at dalvik.system.DexPathList.makeDexElements(DexPathList.java:231)
at dalvik.system.DexPathList.<init>(DexPathList.java:109)
at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:48)
at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:65)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:120)
at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:45)
at android.app.LoadedApk.getClassLoader(LoadedApk.java:419)
at android.app.Lo

AND:

Time Device Name Type PID Tag Message
04-30 15:15:19.075 Samsung SM-J320FN Error 19103 AndroidRuntime java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asirkl.irkl/crc64d8b4ff5105917ffe.MainActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class androidx.appcompat.widget.FitWindowsFrameLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3150)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Can someone help me resolve this error.

The App works fine in Debug mode.

Thanks

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,315 questions
0 comments No comments
{count} vote

11 answers

Sort by: Most helpful
  1. Graham McKechnie 321 Reputation points
    2022-05-01T23:11:01.883+00:00

    AndroidRuntime Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.appcompat.widget.FitWindowsFrameLayout"

    You need to add a proguard.cfg to your app. Make sure in Properties of the proguard.cfg file that you set Build Action to ProguardConfiguration.
    The exception is telling you that r8 is removing that class.
    So you need to add a keep rule like the following in a proguard.cfg.

    -keep class androidx.appcompat.widget.FitWindowsFrameLayout

    Fix that one and you may well find others. Keep adding the missing classes that result in ClassNotFoundExceptions until it doesn't crash.

    You can check each one by checking what is already included by checking the proguard folder in C:\Users\<yourusername>.nuget\packages\xamarin.androidx.appcompat\1.4.1.1\proguard

    Just make sure that you are checking the correct folder for the same version as what you are using in your app.

    The quick fix (not recommended) is to turn off r8.

    Add the following to the top of your proguard.cfg before any of your keep rules.

    -printconfiguration config_your_appname.txt
    -printseeds seeds.txt
    -printusage usage.txt
    
    #usage.txt - this file contains the code that was removed from the apk
    #seeds.txt - this file has a list of classes and class members which were not obfuscated
    #mappings.txt - this file provides the translation between the original and the obfuscated classes, methods, field names. Not applicable to xamarin, because we don't obfuscate. Required when uploading the aab to Google Play.
    

  2. Graham McKechnie 321 Reputation points
    2022-05-02T22:32:27.34+00:00

    @Riffy

    All the contents of that particular proguard.txt file tell us is that you do need to create your own proguard.cfg in your project. By default that file is keeping the class androidx.appcompat.widget.SearchView, but there is no mention of FitWindowsFrameLayout. Therefore you have to create your own specific keep rules for FitWindowsFrameLayout for your app.

    From your comments, I gather you haven't done that.

    Add what I suggested, in my post with the additional line below and then rebuild and see if the crash is eliminated. Don't be surprised if there are more ClassNotFoundExceptions for other classes. However, you should expect that the FitWindowsFrameLayout ClassNotFoundException should be gone if you added the line below to your proguard cfg file.

    -keep class androidx.appcompat.widget.FitWindowsFrameLayout  
    

    If you search google for xamarin.android proguard/r8 you should find specific articles about using r8.

    If you are still having problems, please post the contents of your proguard.cfg. I forgot to add in my post that the proguard.cfg should be added to the root folder of your project and don't forget to change the BuildAction to ProguardConfiguration.

    Once you have it working you should be able to read the contents of both the seeds and usage txt files.

    I'd make the following suggestion to help save your sanity. Make sure as you are developing a particular feature in debug mode that when you are finished, always then do a full release build with dx/r8 enabled and link with SDK and Use Assemblies and package format as aab. That way you know about problems like this immediately. Trying to sort out release r8 issues when you are fully done at the end of the project can be a real pain. I had one recently that took me about a week to get sorted. See https://github.com/xamarin/xamarin-android/issues/6928


  3. Graham McKechnie 321 Reputation points
    2022-05-03T00:18:13.027+00:00

    @Riffy
    The error is telling what is wrong.

    My post stated r8 not proguard, so just change it to r8.

    You are calling the file proguard.txt, however in your Xamarin.Android project it should be proguard.cfg and the build action should be ProguardConfiguration. Once you have the proguard.cfg file in your project. Select it and then Right Click it. Select Properties, then beside Build Action use the drop-down to select ProguardConfiguration.

    Remove the following lines

    -keep class androidx.appcompat.widget.SearchView { <init>(...); }  
    -keepclassmembernames,allowobfuscation,allowshrinking class androidx.appcompat.widget.AppCompatTextViewAutoSizeHelper$Impl* {  
       <methods>;  
     -keep class androidx.appcompat.widget.FitWindowsFrameLayout  
     }  
    

    Note the {} brackets, you have FitWindowsFrameLayout inside the previous class which is wrong.

    Note you don't need to repeat a keep rule that is already in a particular proguard.txt file in the .nuget folder as it is automatically read.

    Could you also indicate what version of Xamarin.AndroidX.AppCompat you are using?

    Finally, just place on a new line.

    -keep class androidx.appcompat.widget.FitWindowsFrameLayout  
    

    Note you also have a non ideal release setting for the linker. You should be using SDK and User Assemblies to get the smallest apk.


  4. Graham McKechnie 321 Reputation points
    2022-05-03T23:31:01.89+00:00

    @Riffy
    Understand, it is always a struggle when you are starting out. My advice would be to always build a release build when you finish a particular feature. Leaving it to when the debug build is finished and expecting it to be ok in a release build is not going to work, unless you are extremely lucky.

    Always make your final build of the day a release build with all the correct release settings, that will give you some idea of where it has gone wrong from the previous release build if it does crash.

    Since you are using an old version of AppCompat, I would recommend upgrading your all .nugets to the latest versions. Just take note of the existing version numbers, so you can revert if necessary. Same thing applies when new versions are released in the future, never upgrade without taking note of what combination of version(s) were working.

    Now the important part. Have you now built a successful Release build, with the suggestions from yesterday? Have you read up on the Xamarin.Android articles on dx/r8 I referred to earlier?


  5. Graham McKechnie 321 Reputation points
    2022-05-04T22:35:46.48+00:00

    @Riffy

    There is nothing very helpful in that error. If this is a runtime error we would need to see more info. Is this a build error or a runtime error?

    If a runtime error. Then you need to work backwards to confirm what it is. Turn off r8 in the project, i.e. set it to the blank value in the IDE. Rebuild and confirm that it can run. If that doesn't help reset the linker to SDK assemblies only. You are aiming to get back to a configuration that doesn't crash. I'm assuming one of the changes will allow it to run crash-free.

    Let us know your results.

    If none of that helps then it is time to report on Xamarin.Android. https://github.com/xamarin/xamarin-android

    If you want to share your project, you could always set it up on github.

    You can probably see more information about a runtime crash if you use the logger Tools/Android/Device Logger. I don't particularly like it but it does work. If you want a logger that has more functionality install Android Studio. If this is crashing at startup then the information to look for will be near the top of the output.

    When you google that error, it leads you to xamarin.android with a number of similar issues - worth looking at.