Skip to content

Commit 1b3f797

Browse files
[One .NET] fix $(AndroidEnablePreloadAssemblies) default
The `ClassLibraryMainLauncherRuns` test was crashing on app start with: Java.Lang.RuntimeException: Unable to start activity ComponentInfo{com.xamarin.classlibrarymainlauncherruns/com.xamarin.classlibrarymainlauncherruns.MainActivity}: android.view.InflateException: Binary XML file line #1 in com.xamarin.classlibrarymainlauncherruns:layout/foo: Binary XML file line #1: You must supply a layout_width attribute. ---> Android.Views.InflateException: Binary XML file line #1 in com.xamarin.classlibrarymainlauncherruns:layout/foo: Binary XML file line #1: You must supply a layout_width attribute. ---> Java.Lang.UnsupportedOperationException: Binary XML file line #1: You must supply a layout_width attribute. --- End of managed Java.Lang.UnsupportedOperationException stack trace --- java.lang.UnsupportedOperationException: Binary XML file line #1: You must supply a layout_width attribute. It seems like this would have always been broken, so I'm not exactly sure how it is passing in some cases? I fixed `foo.xml` so it was valid, and then I got a `NullReferenceException` at startup instead! Process: com.xamarin.classlibrarymainlauncherruns, PID: 2993 android.runtime.JavaProxyThrowable: System.NullReferenceException: Object reference not set to an instance of an object at MyLibrary.MainActivity.OnCreate(Bundle bundle) at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_(IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) at com.xamarin.classlibrarymainlauncherruns.MainActivity.n_onCreate(Native Method) at com.xamarin.classlibrarymainlauncherruns.MainActivity.onCreate(MainActivity.java:29) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.java:7984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) When I looked closer, it seemed that `foo.xml` shouldn't be used at runtime at all. Thinking about this test: 1. `MyApp.dll` has no activity. 2. `MyLibrary.dll` has the main activity & layouts. It made me think the change to `$(AndroidEnablePreloadAssemblies)` in d13d0f9 might be at fault here. To solve the issue: 1. Leave `$(_AndroidEnablePreloadAssembliesDefault)` set to `true`. I think this is the default the runtime expects, and not the default value of `$(AndroidEnablePreloadAssemblies)`. 2. Conditionally set a default for `$(AndroidEnablePreloadAssemblies)` that is different for .NET 6 vs legacy. 3. Set `AndroidEnablePreloadAssemblies=true` in the test. This is a case where I don't know how the runtime would know how to load `MyLibrary.dll` otherwise. I fixed `foo.xml` anyway, because it caused confusion.
1 parent 28f4a06 commit 1b3f797

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,11 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
289289
<AndroidGenerateJniMarshalMethods Condition=" '$(AndroidGenerateJniMarshalMethods)' == '' ">False</AndroidGenerateJniMarshalMethods>
290290
<AndroidMakeBundleKeepTemporaryFiles Condition=" '$(AndroidMakeBundleKeepTemporaryFiles)' == '' ">False</AndroidMakeBundleKeepTemporaryFiles>
291291

292+
<!-- This is the default the runtime expects -->
293+
<_AndroidEnablePreloadAssembliesDefault>true</_AndroidEnablePreloadAssembliesDefault>
292294
<!-- If true it will cause all the assemblies in the apk to be preloaded on startup time -->
293-
<_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' == 'true' ">False</_AndroidEnablePreloadAssembliesDefault>
294-
<_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' != 'true' ">True</_AndroidEnablePreloadAssembliesDefault>
295-
<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' ">$(_AndroidEnablePreloadAssembliesDefault)</AndroidEnablePreloadAssemblies>
295+
<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' and '$(UsingAndroidNETSdk)' != 'true' ">true</AndroidEnablePreloadAssemblies>
296+
<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' and '$(UsingAndroidNETSdk)' == 'true' ">false</AndroidEnablePreloadAssemblies>
296297
<_NativeAssemblySourceDir>$(IntermediateOutputPath)android\</_NativeAssemblySourceDir>
297298
<_AndroidUseNewTypemaps>True</_AndroidUseNewTypemaps>
298299
<_SkipJniAddNativeMethodRegistrationAttributeScan Condition=" '$(_SkipJniAddNativeMethodRegistrationAttributeScan)' == '' ">False</_SkipJniAddNativeMethodRegistrationAttributeScan>

tests/MSBuildDeviceIntegration/Tests/DebuggingTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void ClassLibraryMainLauncherRuns ()
8585
app.SetAndroidSupportedAbis ("armeabi-v7a", "x86");
8686
}
8787
app.SetDefaultTargetDevice ();
88+
app.SetProperty ("AndroidEnablePreloadAssemblies", "true");
8889

8990
var lib = new XamarinAndroidLibraryProject {
9091
ProjectName = "MyLibrary"
@@ -102,7 +103,13 @@ public void ClassLibraryMainLauncherRuns ()
102103
// Remove the default MainActivity.cs & AndroidResources
103104
app.AndroidResources.Clear ();
104105
app.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\layout\\foo.xml") {
105-
TextContent = () => "<?xml version=\"1.0\" encoding=\"utf-8\" ?><LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" />"
106+
TextContent = () =>
107+
@"<?xml version=""1.0"" encoding=""utf-8""?>
108+
<LinearLayout
109+
xmlns:android=""http://schemas.android.com/apk/res/android""
110+
android:layout_width=""fill_parent""
111+
android:layout_height=""wrap_content""
112+
/>"
106113
});
107114
app.Sources.Remove (app.GetItem ("MainActivity.cs"));
108115

0 commit comments

Comments
 (0)