From 9c0437866c7308794283b76125483adecdea9067 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Fri, 30 Apr 2021 18:41:27 +0100 Subject: [PATCH] [monodroid] `AndroidEnablePreloadAssemblies` causes runtime crash. (#5883) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Context: https://github.com/xamarin/xamarin-android/issues/5838 Setting `$(AndroidEnablePreloadAssemblies)`=False, which disables assembly preloading (see af02a65a), could result in a crash when: 1. The launched `Activity` isn't located within the "main App assembly", the assembly built as part of the `App.apk` build, and 2. The launched `Activity` uses Android Resource IDs, and 3. Those Android Resource IDs differ between the Library project build and the Application project build (usually the case). This setup is exemplified by the `DebuggingTest.ClassLibraryMainLauncherRuns()` unit test. Our current Resource architecture assumes/ensures that there is Only Oneā„¢ `Resource` type mentioned by an assembly-level `ResourceDesignerAttribute` custom attribute, with `ResourceDesignerAttribute.IsApplication`=true: [assembly: global::Android.Runtime.ResourceDesignerAttribute("My.Resource", IsApplication=true)] The `ResourceIdManager.UpdateIdValues()` method looks for this attribute in all loaded assemblies, and when (if) found, will invoke the `UpdateIdValues()` static method on the specified type. The problem is that only the *App assembly* `Resource.UpdateIdValues()` method does anything, and if the App assembly isn't loaded -- which may be the case when the `Activity` loaded isn't from the App assembly and assembly preload is disabled -- then: 1. `ResourceIdManager.UpdateIdValues()` never finds the `ResourceDesignerAttribute`, and 2. `Resource.UpdateIdValues()` is never invoked, and 3. Android Resource IDs in non-App assemblies are never updated to contain their correct values. Thus, attempting to use an Android resource may result in unexpected failures: var button = FindViewById