Skip to content

Commit

Permalink
[One .NET] fix $(AndroidEnablePreloadAssemblies) default
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonathanpeppers committed Apr 2, 2021
1 parent b595e11 commit 597916c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,11 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<AndroidGenerateJniMarshalMethods Condition=" '$(AndroidGenerateJniMarshalMethods)' == '' ">False</AndroidGenerateJniMarshalMethods>
<AndroidMakeBundleKeepTemporaryFiles Condition=" '$(AndroidMakeBundleKeepTemporaryFiles)' == '' ">False</AndroidMakeBundleKeepTemporaryFiles>

<!-- This is the default the runtime expects -->
<_AndroidEnablePreloadAssembliesDefault>true</_AndroidEnablePreloadAssembliesDefault>
<!-- If true it will cause all the assemblies in the apk to be preloaded on startup time -->
<_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' == 'true' ">False</_AndroidEnablePreloadAssembliesDefault>
<_AndroidEnablePreloadAssembliesDefault Condition=" '$(UsingAndroidNETSdk)' != 'true' ">True</_AndroidEnablePreloadAssembliesDefault>
<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' ">$(_AndroidEnablePreloadAssembliesDefault)</AndroidEnablePreloadAssemblies>
<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' and '$(UsingAndroidNETSdk)' != 'true' ">true</AndroidEnablePreloadAssemblies>
<AndroidEnablePreloadAssemblies Condition=" '$(AndroidEnablePreloadAssemblies)' == '' and '$(UsingAndroidNETSdk)' == 'true' ">false</AndroidEnablePreloadAssemblies>
<_NativeAssemblySourceDir>$(IntermediateOutputPath)android\</_NativeAssemblySourceDir>
<_AndroidUseNewTypemaps>True</_AndroidUseNewTypemaps>
<_SkipJniAddNativeMethodRegistrationAttributeScan Condition=" '$(_SkipJniAddNativeMethodRegistrationAttributeScan)' == '' ">False</_SkipJniAddNativeMethodRegistrationAttributeScan>
Expand Down
9 changes: 8 additions & 1 deletion tests/MSBuildDeviceIntegration/Tests/DebuggingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void ClassLibraryMainLauncherRuns ()
app.SetAndroidSupportedAbis ("armeabi-v7a", "x86");
}
app.SetDefaultTargetDevice ();
app.SetProperty ("AndroidEnablePreloadAssemblies", "true");

var lib = new XamarinAndroidLibraryProject {
ProjectName = "MyLibrary"
Expand All @@ -102,7 +103,13 @@ public void ClassLibraryMainLauncherRuns ()
// Remove the default MainActivity.cs & AndroidResources
app.AndroidResources.Clear ();
app.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\layout\\foo.xml") {
TextContent = () => "<?xml version=\"1.0\" encoding=\"utf-8\" ?><LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" />"
TextContent = () =>
@"<?xml version=""1.0"" encoding=""utf-8""?>
<LinearLayout
xmlns:android=""http://schemas.android.com/apk/res/android""
android:layout_width=""fill_parent""
android:layout_height=""wrap_content""
/>"
});
app.Sources.Remove (app.GetItem ("MainActivity.cs"));

Expand Down

0 comments on commit 597916c

Please sign in to comment.