Skip to content

Commit

Permalink
[monodroid] AndroidEnablePreloadAssemblies causes runtime crash.
Browse files Browse the repository at this point in the history
Fixes dotnet#5838

Turning off Assembly preloading results in the following runtime error

```
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)
```

This only occurs on one unit test [`DebuggingTest.ClassLibraryMainLauncherRuns()`](https://github.com/xamarin/xamarin-android/blob/bf4f4f42af26cdddb46087deed59aae8424f7942/tests/MSBuildDeviceIntegration/Tests/DebuggingTest.cs#L74-L124)
This happens because the `MainActivity` in that test is in a library project.
As a result when we do not preload assemblies the main assembly is NEVER
loaded. This means `ResourceIdManager.UpdateIdValues()` never finds the
`Resource` class from the main assembly, and as such none of the ids are
updated.

To fix this , when running without assembly preloading we will ALWAYS
load the first assembly in the assembly list. This will always be the
main assembly and will contain the `Resource` class.
  • Loading branch information
dellis1972 committed Apr 28, 2021
1 parent d19963f commit a77aa03
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/monodroid/jni/monodroid-glue-internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ namespace xamarin::android::internal
void disable_external_signal_handlers ();
void lookup_bridge_info (MonoDomain *domain, MonoImage *image, const OSBridge::MonoJavaGCBridgeType *type, OSBridge::MonoJavaGCBridgeInfo *info);
void load_assembly (MonoDomain *domain, jstring_wrapper &assembly);
void load_assemblies (MonoDomain *domain, jstring_array_wrapper &assemblies);
void load_assemblies (MonoDomain *domain, bool preload, jstring_array_wrapper &assemblies);
void set_debug_options ();
void parse_gdb_options ();
void mono_runtime_init (dynamic_local_string<PROPERTY_VALUE_BUFFER_LEN>& runtime_args);
Expand Down
9 changes: 6 additions & 3 deletions src/monodroid/jni/monodroid-glue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,7 @@ MonodroidRuntime::load_assembly (MonoDomain *domain, jstring_wrapper &assembly)
}

inline void
MonodroidRuntime::load_assemblies (MonoDomain *domain, jstring_array_wrapper &assemblies)
MonodroidRuntime::load_assemblies (MonoDomain *domain, bool preload, jstring_array_wrapper &assemblies)
{
timing_period total_time;
if (XA_UNLIKELY (utils.should_log (LOG_TIMING)))
Expand All @@ -1604,6 +1604,9 @@ MonodroidRuntime::load_assemblies (MonoDomain *domain, jstring_array_wrapper &as
for (size_t i = 0; i < assemblies.get_length (); ++i) {
jstring_wrapper &assembly = assemblies [i];
load_assembly (domain, assembly);
// only load the first "main" assembly if we are not preloading.
if (!preload)
break;
}

if (XA_UNLIKELY (utils.should_log (LOG_TIMING))) {
Expand Down Expand Up @@ -1638,8 +1641,8 @@ MonodroidRuntime::create_and_initialize_domain (JNIEnv* env, jclass runtimeClass
if (assembliesBytes != nullptr)
designerAssemblies.add_or_update_from_java (domain, env, assemblies, assembliesBytes, assembliesPaths);
#endif
if (androidSystem.is_assembly_preload_enabled () || (is_running_on_desktop && force_preload_assemblies))
load_assemblies (domain, assemblies);
bool preload = (androidSystem.is_assembly_preload_enabled () || (is_running_on_desktop && force_preload_assemblies));
load_assemblies (domain, preload, assemblies);
init_android_runtime (domain, env, runtimeClass, loader);

osBridge.add_monodroid_domain (domain);
Expand Down

0 comments on commit a77aa03

Please sign in to comment.