Skip to content

[Bug] [Android] System.IO.FileNotFoundException when <BundleAssemblies>True</BundleAssemblies> #64

Closed

Description

@RadoslawKubas

Description

After upgrade from Xamarin.Forms 4.4.0.991640 to 4.5.282-pre4 I noticed that when

<BundleAssemblies>True</BundleAssemblies>

app crash on start with exception

System.IO.FileNotFoundException: Could not load file or assembly 'Xamarin.AndroidX.AppCompat.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

My working setup for release in 4.4 is

<AndroidLinkMode>Full</AndroidLinkMode>
<AotAssemblies>True</AotAssemblies>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<EnableLLVM>True</EnableLLVM>
<BundleAssemblies>True</BundleAssemblies>

in 4.5 everything works fine only if

<AndroidLinkMode>Full</AndroidLinkMode>
<AotAssemblies>True</AotAssemblies>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<EnableLLVM>True</EnableLLVM> 
<BundleAssemblies>False</BundleAssemblies>

Steps to Reproduce

  1. Create new solution
  2. Upgrade Xamarin to 4.5.0.282-pre4
  3. Set:
<AndroidLinkMode>Full</AndroidLinkMode>
<AotAssemblies>True</AotAssemblies>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<EnableLLVM>True</EnableLLVM>
<BundleAssemblies>True</BundleAssemblies>
  1. Run app on the real device

Expected Behavior

App starts.

Actual Behavior

App crash with System.IO.FileNotFoundException exception.

Basic Information

  • Version with issue: 4.5.0.282-pre4
  • Last known good version: 4.4.0.991640
  • IDE: Visual Studio 2019
  • Platform Target Frameworks:
    • Android: Xamarin.Android 10.1
  • Android Support Library Version: 28.0.0.03, AndroidX 1.1.0
  • Nuget Packages:
  • Affected Devices: all

Activity

  1. jsuarezruiz commented on Feb 25, 2020

    @jsuarezruiz

    @RadoslawKubas Could you attach an example or review the attached one?
    Issue9689.zip
    I can't reproduce it. I am using Xamarin.Forms 4.5-pre4 and:

    <AndroidLinkMode>Full</AndroidLinkMode>
    <AotAssemblies>True</AotAssemblies>
    <AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
    <EnableLLVM>True</EnableLLVM>
    <BundleAssemblies>True</BundleAssemblies>
    
  2. VladislavAntonyuk commented on Feb 25, 2020

    @VladislavAntonyuk

    I can reproduce it in release mode only

  3. danardelean commented on Feb 25, 2020

    @danardelean

    I had/have the same problem and it is only in Release mode.
    Tried disabling Xamarin.AndroidX.AppCompat for the linker but I still got the problem.

    Another problem with AndroidX (the stable versions) and XF 4.5 pre-release is that you cannot enable r8 (code shrinker) in release.

    In the end I rolled back to the old support libraries and everything works.

  4. Redth commented on Feb 26, 2020

    @Redth
    Member

    @danardelean can you be more specific about the error you are seeing? Please open a new issue with more details.

  5. RadoslawKubas commented on Feb 28, 2020

    @RadoslawKubas
    Author

    I checked with latest stable 4.5.0.356 and same problem repeats. I tried to prepare an example, but the problem is that even in my main solution, I can't reproduce it locally. When building same commit with VS2019 Community 16.4.5 it seems to be fine, but with build on Microsoft App Center and publish to Google Play, all prepunch tests fails.

  6. mduchev commented on Mar 2, 2020

    @mduchev

    I am having the exact same issue. On DEBUG everything works fine. If I change the config to Release - the build passes, I can archive the app but as soon as the app starts, the app crashes with the same error.

  7. mduchev commented on Mar 2, 2020

    @mduchev

    Okay, so I managed to produce a sample project and I managed to narrow down the problem, but this time I got a different exception (but at the same time it is kind of similar). It turned out that it isn't due to the BundleAssemblies config. This happens as soon as we choose to use r8 for code shrinking, the app crashes.

    Here is the demo project:
    DemoAndroidXCrash.zip

    Steps to reproduce:

    1. Open the solution
    2. Build the app in Release config
    3. Archive & Distribute it with a whatever signing identity you wish
    4. Install the signed apk into a phone
    5. Run the app

    If we don't have any code shrinking, everything works fine and the app launches successfully. However, if we choose to use code shrinking (and r8 specifically), then the app crashes with the following exception:

    Time Device Name Type PID Tag Message
    03-02 15:53:09.701 HMD Global Nokia 6.2 Error 26021 AndroidRuntime Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.appcompat.widget.FitWindowsFrameLayout" on path: DexPathList[[zip file "/data/app/com.companyname.demoandroidxcrash-Pdr5ZJ6GGM0LzJrOyKjpMA==/base.apk"],nativeLibraryDirectories=[/data/app/com.companyname.demoandroidxcrash-Pdr5ZJ6GGM0LzJrOyKjpMA==/lib/arm64, /data/app/com.companyname.demoandroidxcrash-Pdr5ZJ6GGM0LzJrOyKjpMA==/base.apk!/lib/arm64-v8a, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(Unknown Source:223)
    at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(Unknown Source:4)
    at androidx.appcompat.app.AppCompatDelegateImpl.initWindowDecorActionBar(Unknown Source:0)
    at androidx.appcompat.app.AppCompatDelegateImpl.getSupportActionBar(Unknown Source:0)
    at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar(Unknown Source:7)
    at androidx.appcompat.app.AppCompatActivity.setSupportActionBar(Unknown Source:4)
    at crc6422feeebb19be5b6f.MainActivity.n_onCreate(Native Method)
    at crc6422feeebb19be5b6f.MainActivity.onCreate(Unknown Source:0)
    at android.app.Activity.performCreate(Activity.java:7323)
    at android.app.Activity.performCreate(Activity.java:7314)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2930)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3085)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6745)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    Force finishing activity com.companyname.demoandroidxcrash/crc6422feeebb19be5b6f.MainActivity

  8. tranb3r commented on Mar 3, 2020

    @tranb3r

    Here is another repro project:
    TestAndroidX.zip

    In debug mode, an image will be displayed on the main page.
    In release mode, the image is not displayed, because of the exception.

    The exception is only happening with <BundleAssemblies>True</BundleAssemblies>.
    It makes no difference whether R8 or Linker are enabled or not.
    I'm using XF 4.5.0.
    This exception can be easily reproduced on the emulator.

    Stack trace of the exception (no crash, it's silent):
    ImageElementManager: Error loading image: System.IO.FileNotFoundException: Could not load file or assembly 'Xamarin.AndroidX.AppCompat.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
    File name: 'Xamarin.AndroidX.AppCompat.Resources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
    at Xamarin.Forms.Platform.Android.FileImageSourceHandler.LoadImageAsync (Xamarin.Forms.ImageSource imagesource, Android.Widget.ImageView imageView, System.Threading.CancellationToken cancellationToken)
    at Xamarin.Forms.Platform.Android.ImageViewExtensions.UpdateBitmap (Android.Widget.ImageView imageView, Xamarin.Forms.IImageElement newView, Xamarin.Forms.IImageElement previousView, Xamarin.Forms.ImageSource newImageSource, Xamarin.Forms.ImageSource previousImageSource)
    at Xamarin.Forms.Platform.Android.FastRenderers.ImageElementManager.TryUpdateBitmap (Xamarin.Forms.Platform.Android.IImageRendererController rendererController, Android.Widget.ImageView Control, Xamarin.Forms.IImageElement newImage, Xamarin.Forms.IImageElement previous)

  9. nhdanh commented on Mar 12, 2020

    @nhdanh

    I have same problem

  10. tranb3r commented on Mar 12, 2020

    @tranb3r

    Similar to #45 (which has been closed).
    @moljac Could you please take a look ? This is quite serious...

  11. ognamala commented on Mar 17, 2020

    @ognamala

    We have also observed this problem, which happens only in release mode and when Bundle Assemblies is set to true

  12. nhdanh commented on Mar 18, 2020

    @nhdanh

    Will fix on new version ?

  13. 66 remaining items

  14. tranb3r commented on Jun 6, 2020

    @tranb3r

    When using Xamarin.Forms 4.6.514 instead of 4.6.0.800, here is the error I get on building my android app:
    7>C:\Users\bertr.nuget\packages\xamarin.androidx.migration\1.0.6\buildTransitive\monoandroid90\Xamarin.AndroidX.Migration.targets(131,9): error : Could not find 1 Android X assemblies, make sure to install the following NuGet packages:
    7>C:\Users\bertr.nuget\packages\xamarin.androidx.migration\1.0.6\buildTransitive\monoandroid90\Xamarin.AndroidX.Migration.targets(131,9): error : - Xamarin.AndroidX.AppCompat.Resources
    7>C:\Users\bertr.nuget\packages\xamarin.androidx.migration\1.0.6\buildTransitive\monoandroid90\Xamarin.AndroidX.Migration.targets(131,9): error : You can also copy-and-paste the following snippet into your .csproj file:
    7>C:\Users\bertr.nuget\packages\xamarin.androidx.migration\1.0.6\buildTransitive\monoandroid90\Xamarin.AndroidX.Migration.targets(131,9): error :

    I'm probably doing something wrong...

    And by the way, I don't understand why the new version of Xamarin.AndroidX.Migration that is being published is 1.0.0.1, when I'm already using 1.0.6.

  15. moljac commented on Jun 7, 2020

    @moljac
    Contributor

    @tranb3r

    I'm probably doing something wrong...

    I don't think so. With more complex samples there might be such issues with transitive dependencies. In some cases I had to manually fix some dependencies by adding explicit dependencies.

    Can you post your project file (*.csproj) for Android app only. (PackageReference items), please.

    For now:

    1. rename Xamarin.AndroidX.AppCompat.Resources to Xamarin.AndroidX.AppCompat.AppCompatResources version is 1.0.0.1

    Or let nuget handle transitive dependencies:

    Forms will pull everything in:

        <PackageReference Include="Xamarin.Forms" Version="4.6.514" />
        <PackageReference Include="Xamarin.Essentials" Version="1.5.3.2" />
    

    Was your app migrated to AndroidX before? If yes, then you don't need to migrate again. You'll need it if some library references (transitively) Android.Support then types are mapped on CIL level (Mono.Cecil).

    I will need more time to explain Migration.

    Xamarin.AndroidX.Migration 1.0.0.1 had to be published, because of the mappings in THAT moment. (There were different artifacts bound at that version) and that nuget carries mapping files.

    Just simple explanation:

    Check 1st config.json published on 20200214 it has 89 artifacts bound. Latest one with 1.0.6 was published on 20200506 it has 96 artifacts. If I would publish updates with version 1.0.6.1 or 1.0.7 there would be mappings for only 89 artifacts, so 7 missing and that would cause problems. When I bump the last published tag on 1.0.6.1 it will contain all artifacts and everything for the mappings. After next release and fixes it will be 1.0.7 or I will publish 1.0.6.1 as 1.0.7.

  16. tranb3r commented on Jun 8, 2020

    @tranb3r

    @moljac
    Here is my androidapp.csproj packagereferences (I've removed some custom nugets), if it can help.

      <ItemGroup>
        <PackageReference Include="Xamarin.Forms" Version="4.6.514" />
        <PackageReference Include="Xamarin.AndroidX.Work.Runtime" Version="2.3.4.1" />
        <PackageReference Include="Xamarin.AndroidX.Migration" Version="1.0.6" />
        <PackageReference Include="Xamarin.AndroidX.Paging.Common" Version="2.1.2" />
      </ItemGroup>
    

    For the rest of your explanation, sorry but I'm totally lost...

  17. moljac commented on Jun 8, 2020

    @moljac
    Contributor

    @tranb3r

    For the rest of your explanation, sorry but I'm totally lost...

    It is OK. I feel that way too. Ping me later if you want deeper info.

    I would try several options:

    1. removing Xamarin.AndroidX.Migration completely
    2. change version of Xamarin.AndroidX.Migration to 1.0.0.1
    3. add explicitly Xamarin.AndroidX.AppCompat.AppCompatResources v 1.0.0.1

    Please provide feedback.

    Today after I publish 1.0.6.1 or 1.0.7 transitive dependency resolving will be easier.

  18. melimion commented on Jun 8, 2020

    @melimion

    @tranb3r try this:

    • clean local nuget caches
    • disable all other nuget sources besides local with AndroidX packages
    • run "update-package -reinstall"
  19. moljac commented on Jun 8, 2020

    @moljac
    Contributor

    @melimion
    Did updates from friday work for you?

  20. melimion commented on Jun 8, 2020

    @melimion

    @moljac haven't tried yet, will try today

  21. moljac commented on Jun 8, 2020

    @moljac
    Contributor

    Nuget setup issue and related sample:

    #111

  22. melimion commented on Jun 8, 2020

    @melimion

    @moljac rebuilt app with new nugets- no issues

  23. moljac commented on Jun 8, 2020

    @moljac
    Contributor

    @melimion Thanks a lot for the feedback. Appreciated.

  24. tranb3r commented on Jun 8, 2020

    @tranb3r

    @moljac
    With new nugets, no more issues in my app.

      <ItemGroup>
        <PackageReference Include="Xamarin.Forms" Version="4.6.514" />
        <PackageReference Include="Xamarin.AndroidX.Work.Runtime" Version="2.3.4.3" />
        <PackageReference Include="Xamarin.AndroidX.Migration" Version="1.0.6.1" />
        <PackageReference Include="Xamarin.AndroidX.Paging.Common" Version="2.1.2.1" />
      </ItemGroup>
    

    Thanks !

  25. moljac commented on Jun 9, 2020

    @moljac
    Contributor

    @tranb3r NP (you re welcome).
    Thank you for the feedback.

  26. EmilAlipiev commented on Jun 9, 2020

    @EmilAlipiev

    Sorry, late to the party but actually Xamarin team seems to be right. Bundle into native assemblies reduces size for apk but increases for aab. Here is what i found,
    see in this SO post. https://stackoverflow.com/questions/62293367/xamarin-android-app-bundle-with-bundle-into-native-assemblies-increases-size-b

    Let me know if you have similar experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions