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,301 questions
0 comments No comments
{count} vote

11 answers

Sort by: Most helpful
  1. Graham McKechnie 321 Reputation points
    2022-05-05T00:48:51.8+00:00

    The error indicates that it is most likely that it doesn't like your proguard.cfg file, either the contents or you haven't got it set up properly. Key words in that error message not a valid file

    Does it build if you exclude the proguard.cfg file from the solution? Right-click on it and "exclude from project" as a test. At the same time disable r8 on the build. You must be able to get back to the situation where you were successfully building it as a release build as you were previously.

    My guess is that you still have something wrong with your proguard.cfg.

    I don't understand how you say you got rid of the ClassNotFoundException Now when I do a Release build, the Didn't find class "androidx.appcompat.widget.FitWindowsFrameLayout" on path: DexPathList[[zip error file is no longer there. So progress made

    The above was a runtime error, not a build error. This log above is reporting a build error - completely different to the original runtime error.

    Please post the contents of your proguard.cfg, so we can check it for errors. Have you set the build action of the proguard.cfg? Is it in the root folder of the project?


  2. Graham McKechnie 321 Reputation points
    2022-05-05T18:05:13.19+00:00

    @Riffy
    You appear to have extra proguard files in your project that I've never seen in any of my projects. proguard_xamarin, proguard_project_primary and proguard_project_references. You should only have proguard.cfg in your projects folder.

    Did you place them there?

    You still have not shown me the contents of your proguard.cfg. Those three extra files are not required, their contents will be automatically processed by r8, which should result in the files, seeds, usage and mapping all appearing in your project folder.

    I'm not asking you to find your last successful build. I'm asking you to work backwards to before you attempted to use r8. Therefore if you turn off r8, then r8 won't run and you should get a successful build. You need to get back to the starting point of before you attempted to use r8.


  3. Graham McKechnie 321 Reputation points
    2022-05-05T19:34:33.947+00:00

    Thanks for that at least it is a starting point.

    Before you attempt to build a release build using r8 you must be able to build a successful build without it. It is a waste of time attempting to build with r8 unless you can successfully build without. That is why r8 is off (blank) by default.

    Important question - Can you confirm that you can build a successful release build with r8 off?

    If you can build successfully then you can proceed to strip unused code with r8.

    I've got no idea where you got all those rules and why you would think they should be there.

    Please replace the existing contents of your proguard.cfg with the following. I'd also suggest removing those 3 files you added previously.

    -printconfiguration config_navigation.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.
    
    # Added 20/06/2021 To upload a mapping.txt file
    # Open the Play Console.
    # Select an app.
    # On the left menu, select Release > App bundle explorer.
    # Select the Downloads tab, and scroll down to the 'Assets' section. Click the upload arrow for the 
    # mapping file or the debug symbols as applicable to upload the deobfuscation for the version of 
    # your app.
    
    # Added these 3 13/01/2021
    -printmapping mapping.txt
    -keepattributes SourceFile
    -keepattributes LineNumberTable
    
    # remember to change back the build Action in the Properties Window to ProguardConfiguration 
    # if you ever exclude the proguard.cfg from the project, 
    # as it will come back as none if you do not which means this file won't be read.
    
    -keep class androidx.appcompat.widget.FitWindowsLinearLayout
    

    There is only a single keep rule in this particular proguard.cfg. Most of the file is just comments to remind me of things I always seem to forget or overlook.

    That should solve your original ClassNotFoundException. Please note that there probably will be others. So, launch the app and see if it crashes. If it crashes look at the log output and search for the ClassNotFoundException. It will be very close to the top of the log and if you are using Android Studio's log - the text will be in red.

    For each crash add a new keep rule. It's a tedious exercise, but doable and the proguard.cfg created can then be used in other similar Android projects as a starting point.

    If you want to check what proguard does then look at the files, usage.txt and seeds.txt - use Show all files to see them and read up on the articles about proguard and r8.


  4. Graham McKechnie 321 Reputation points
    2022-05-05T23:34:47.277+00:00

    You still haven't answered the question. Can you build your project in Release mode with r8 off?

    Sorry I missed you saying that you can build in release mode. That is good then.

    I just tried to reproduce your reference to Xamarin.Android.D8.targets etc.. by keeping r8 on, but excluding the proguard.cfg file and I couldn't reproduce your error. Therefore I can't explain how those three file names somehow got into your project folder. The error you received earlier just pointed to the fact that your proguard.cfg wasn't valid, therefore I don't understand how those files got into your project. I think you could safely delete them.

    Obviously, my app crashed as soon as I launched it. java.lang.ClassNotFoundException: Didn't find class androidx.core.app.CoreComponentFactory as you would expect since nothing was processed. But no extra files were dumped in my project.

    Why do you keep referring to proguard.txt (specifically .txt)?

    I've never used Dotfuscator as I use a different obfuscator. Can you simply build a release build as a test with Dotfuscator turned off. Is it just a switch on/off or more complicated.


  5. Graham McKechnie 321 Reputation points
    2022-05-06T04:50:48.9+00:00

    @Riffy
    Your very first message indicates that you were doing a release build. The reason it crashed was because you didn't have a proguard.cfg with the correct keep rule.

    I understand the R8 tags in Xamarin.Android.D8.targets that is completely normal, if you double click on a build error. It will take you to the section of where MSBuild is failing. That is not your code, that is MSBuild code attempting to execute. What I don't understand is how those files end up in your project. Those three proguard files should end up in the folder obj\Release\120\proguard\ where the 120 indicates the Android version. MSBuild automatically will process that section of the build and include those files if r8 is enabled plus it will read your specific project's proguard.cfg if it exists and its BuildAction is set to ProguardConfiguration. If you had included a proguard.cfg it would have processed it. However, you didn't include it.

    With R8 off, they Build fine in Release mode.
    Of course, they do because that section of MSBuild isn't executed.

    Why can't you just clean the release build, and add the proguard.cfg I posted earlier and enable r8 and build and deploy and then report what happens?

    What version of VS 2022 are you using?

    Another tip - don't bury your projects in C:\Users\<whatever_name> etc. Why use longer path names than necessary? Create a Folder like C:\ProjectsVS2022 and then when you create a new project the templates indicate it should go in the folder C:\ProjectsVS2022 and a new folder will be created off ProjectsVS20222 as in ProjectVS2022\YourProjectname.

    You didn't answer my question about Dotfuscator