Bug 57279 - Sample for Microsoft Intune is failing - System.NullReferenceException at IntuneMAMSampleAndroid.EntryActivity.OnCreate
Summary: Sample for Microsoft Intune is failing - System.NullReferenceException at Int...
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: 7.3 (15.2)
Hardware: PC Mac OS
: High major
Target Milestone: 15.4
Assignee: dean.ellis
URL:
Depends on:
Blocks:
 
Reported: 2017-06-08 17:32 UTC by Gonzalo Martin
Modified: 2017-08-22 09:33 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Emulator Details (99.19 KB, image/png)
2017-06-08 17:32 UTC, Gonzalo Martin
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Gonzalo Martin 2017-06-08 17:32:27 UTC
Created attachment 22780 [details]
Emulator Details

##### Steps to reproduce #####
1. Create a new Xamarin.Android project with Xamarin Studio (mac)
2. Right click on "Components" -> "Get More Components". Search for "Intune"
3. Add Microsoft Intune MAM to the current app
4. While the component is being installed, select "Samples" tab in the new opened window and open sample for Android platform.
5. Build the last sample added project and run it.

*** Expected result ***
The app should open without errors

*** Current result ***
The app could not be opened and I'm seeing the following error in application output:

> System.NullReferenceException: Object reference not set to an instance of an object.
>  at IntuneMAMSampleAndroid.EntryActivity.OnCreate (Android.OS.Bundle bundle) [0x00021] in <5c130bb15c044aed8b3f49b92768b1b5>:0
>  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <d855bac285f44dda8a0d8510b679b1e2>:0
>  at at (wrapper dynamic-method) System.Object:ad17188e-2e54-45de-a9dc-b44d6a7f3e9d (intptr,intptr,intptr)

##### Suplemental Info #####
Applicaton Output: https://gist.github.com/gonzalonm/1c597a5c7063108653acebf00eee9cdf

##### Environment Info #####
Mono 5.0.1.1
XA 7.3.1.2
Full environment details: https://gist.github.com/gonzalonm/9b96866da9e9dc88b98f7ae714939854
Emulator details: (see the attached screenshot)
Comment 1 Gonzalo Martin 2017-06-08 17:56:29 UTC
One more thing about this issue. Peter found a workaround to have this working. 
Running this sample on 64bit device, you should enable arm64-v8a ABI.
For that way, the app won't crash and works fine.
Comment 2 Jonathan Pryor 2017-06-08 20:15:35 UTC
The immediate cause of the NullReferenceException is that `FindViewById<Button>(Resource.Id.EntryActivityStartButton);` is returning `null`, so the subsequent `button.Click += ...` throws.

Additionally (?!), if I build from the command-line, it works:

> xbuild /t:Install IntuneMAMSampleAndroid.csproj
> xbuild /t:_Run IntuneMAMSampleAndroid.csproj

No NullReferenceException is thrown.

It works because all the id values are consistent:

> $ find . -iname R.java | xargs grep EntryActivityStartButton
> ./obj/AnyCPU/Debug/android/com/microsoft/intune/mam/R.java:        public static final int EntryActivityStartButton=0x7f0c0050;
> ./obj/AnyCPU/Debug/android/microsoftintunemamsampleandroid/microsoftintunemamsampleandroid/R.java:        public static final int EntryActivityStartButton=0x7f0c0050;
> ./obj/AnyCPU/Debug/android/src/android/support/v4/R.java:        public static int EntryActivityStartButton=0x7f0c0050;
> ./obj/AnyCPU/Debug/android/src/android/support/v7/appcompat/R.java:        public static int EntryActivityStartButton=0x7f0c0050;
> ./obj/AnyCPU/Debug/android/src/com/microsoft/intune/mam/R.java:        public static final int EntryActivityStartButton=0x7f0c0050;
> ./obj/AnyCPU/Debug/android/src/microsoftintunemamsampleandroid/microsoftintunemamsampleandroid/R.java:        public static final int EntryActivityStartButton=0x7f0c0050;
> 
> $ grep -1 EntryActivityStartButton Resources/Resource.Designer.cs 
> 			// aapt resource value: 0x7f0c0050
> 			public const int EntryActivityStartButton = 2131492944;
			
Note that all EntryActivityStartButton values are 0x7f0c0050.

When building from Visual Studio for Mac, that's *not* the case; I don't know why.

Immediately after opening the project, things are consistent:

> $ find . -iname R.java | xargs grep EntryActivityStartButton
> ./obj/AnyCPU/Debug/android/com/microsoft/intune/mam/R.java:        public static final int EntryActivityStartButton=0x7f070000;
> ./obj/AnyCPU/Debug/android/microsoftintunemamsampleandroid/microsoftintunemamsampleandroid/R.java:        public static final int EntryActivityStartButton=0x7f070000;
> $ grep -1 EntryActivityStartButton Resources/Resource.Designer.cs 
> 			// aapt resource value: 0x7f070000
> 			public const int EntryActivityStartButton = 2131165184;
			

After building and deploying (and observing the NullReferenceException), they are *not* consistent:

> $ find . -iname R.java | xargs grep EntryActivityStartButton
> ./obj/AnyCPU/Debug/android/com/microsoft/intune/mam/R.java:        public static final int EntryActivityStartButton=0x7f070000;
> ./obj/AnyCPU/Debug/android/microsoftintunemamsampleandroid/microsoftintunemamsampleandroid/R.java:        public static final int EntryActivityStartButton=0x7f070000;
> ./obj/AnyCPU/Debug/android/src/android/support/v4/R.java:        public static int EntryActivityStartButton=0x7f0c0050;
> ./obj/AnyCPU/Debug/android/src/android/support/v7/appcompat/R.java:        public static int EntryActivityStartButton=0x7f0c0050;
> ./obj/AnyCPU/Debug/android/src/com/microsoft/intune/mam/R.java:        public static final int EntryActivityStartButton=0x7f0c0050;
> ./obj/AnyCPU/Debug/android/src/microsoftintunemamsampleandroid/microsoftintunemamsampleandroid/R.java:        public static final int EntryActivityStartButton=0x7f0c0050;
> $ grep -1 EntryActivityStartButton Resources/Resource.Designer.cs 
> 			// aapt resource value: 0x7f070000
> 			public const int EntryActivityStartButton = 2131165184;

Note that some values are 0x7f070000, while others are 0x7f0c0050. That's not *necessarily* a problem; "Library" values are expected to be read+write `static int`, while "App" values are expected to be readonly `static final int`, and the Library values will be *replaced* with the App values during startup.

That's not what we see here, though: the `static final int` values are *themselves* inconsistent, and readonly values *cannot be updated during startup*:

> ./obj/AnyCPU/Debug/android/com/microsoft/intune/mam/R.java:        public static final int EntryActivityStartButton=0x7f070000
> ./obj/AnyCPU/Debug/android/src/com/microsoft/intune/mam/R.java:        public static final int EntryActivityStartButton=0x7f0c0050;

Additionally, *those are the same type*, `com.microsoft.intune.mam.R`! What is this intermediate "src" directory doing, and which one is valid?

Aside: Resource ids are important; it's how FindViewById() finds controls. Provide the wrong value, the control won't be found, *which is exactly what we observe*. It is thus paramount that the correct values be present in important locations, which includes `Resource.Designer.cs`.
Comment 3 Jonathan Pryor 2017-06-08 20:17:04 UTC
@Gonzalo: BTW, the workaround isn't building for arm64. The workaround is nuking the `bin` and `obj` directories and rebuilding, which presumably changing the ABI indirectly performs.
Comment 4 Jonathan Pryor 2017-06-08 20:18:34 UTC
@Dean: See Comment #2 for details. Maybe this is IDE integration; maybe not.

Regardless, this question sounds like a good starting point:

> Additionally, *those are the same type*, `com.microsoft.intune.mam.R`!
> What is this intermediate "src" directory doing, and which one is valid?
Comment 5 dean.ellis 2017-06-14 09:57:47 UTC
Weird. the 'com' directory in obj/Debug/android should NOT be there at all!!
Comment 6 dean.ellis 2017-06-14 10:02:18 UTC
ok it should be here.. or at least it seems to be in other projects
Comment 7 dean.ellis 2017-08-07 11:31:27 UTC
Looks like "UpdateAndroidResgen" is not being run because it thinks the outputs are up to date.
Comment 9 dean.ellis 2017-08-10 08:33:01 UTC
Fixed in tamarin-android/master/52d32c8e