Bug 37491 - App crashes in Release config on device with System.Runtime.Serialization.InvalidDataContractException
Summary: App crashes in Release config on device with System.Runtime.Serialization.Inv...
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Linker (show other bugs)
Version: 6.0.0
Hardware: PC Mac OS
: --- normal
Target Milestone: 7.1 (C9)
Assignee: Radek Doulik
URL:
: 39988 (view as bug list)
Depends on:
Blocks:
 
Reported: 2016-01-07 20:55 UTC by Jon Goldberger [MSFT]
Modified: 2016-12-07 06:22 UTC (History)
11 users (show)

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


Attachments
Test Project (39.27 KB, application/zip)
2016-01-07 20:55 UTC, Jon Goldberger [MSFT]
Details

Description Jon Goldberger [MSFT] 2016-01-07 20:55:18 UTC
Created attachment 14486 [details]
Test Project

## Description

In a simple test app that serializes a DateTimeOffset object on a button click, the app will crash with below exception and stack trace if DataContractJsonSerializer.ReadObject method call is in the app, even though the method call is never run. IOW that method call is in a static method added to MainActivity, but the static method is never called. This issue only occurs in a Release configuration when deployed to a device. 

## Steps to reproduce

1. Load the attached test project (Used Xamarin Studio for testing)

2. Run the app to an Android device in release configuration

3. Click the button

Expected result: App will not crash.

Actual result: App crashes with Exception and stacktrace linked below. (Give it a couple of seconds)


## Stack trace

https://gist.github.com/King-of-Spades/8e17b531588a633c1c1a

## Notes

Based on the exception and trace this seem to be an issue with a method having been stripped away by the linker. Ignoring the System.Runtime.Serialization assembly in the Project Options->Android Build->Linker page resolves the issue (as does the "Don't link" option of course). I also noted that simply disabling the "Enable Optimizations" option on the Project Options-?Compiler page also resolves the issue. 

Also if you leave the project options as is in the test project and just comment out line 52 of MainActivity.cs

`resultEx = (DateTimeOffset)serializer.ReadObject(stream);`

this also resolves the issue without disabling the compiler optimizations or ignoring the System.Runtime.Serialization assembly. 


## Version info

=== Xamarin Studio ===

Version 5.10.1 (build 6)
Installation UUID: 964c531b-d928-456b-a9ae-e1f82266b360
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Version: 0.22.0.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

=== Xamarin Android Player ===

Not Installed

=== Apple Developer Tools ===

Xcode 7.2 (9548)
Build 7C68

=== Xamarin.iOS ===

Version: 9.4.0.0 (Business Edition)
Hash: 7322991
Branch: master
Build date: 2015-12-08 16:20:29-0500

=== Xamarin.Mac ===

Version: 2.4.1.5 (Business Edition)

=== Xamarin Inspector ===

Version: 0.3.2.3
Hash: 1b526e6
Branch: master
Build date: Tue Nov 17 20:54:30 UTC 2015

=== Build Information ===

Release ID: 510010006
Git revision: 0b60eecdb531933734519c13257d16a780274aab
Build date: 2015-12-04 20:28:20-05
Xamarin addins: 9876fd7c9837977178411ec7375b4352c0a0d6af
Build lane: monodevelop-lion-cycle6-baseline

=== Operating System ===

Mac OS X 10.11.2
Darwin Jons-iMac.local 15.2.0 Darwin Kernel Version 15.2.0
    Fri Nov 13 19:56:56 PST 2015
    root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64
Comment 3 Sadik Ali 2016-01-13 08:33:16 UTC
I have checked this issue with latest Stable XS XamarinStudio-5.10.1.6_0b60eecdb531933734519c13257d16a780274aab.dmg and able to reproduce the issue by following the steps mentioned in above Description. 


Screencast: http://www.screencast.com/t/GXjIDZjl

IDE Logs: https://gist.github.com/Arpit360/9960b75de708a22fd6f7

Environment info:
=== Xamarin Studio ===

Version 5.10.1 (build 6)
Installation UUID: 5aee9e7c-04d2-4896-b80a-a918340ae634
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Not Installed

=== Xamarin.Android ===

Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/360_macmini/Desktop/setup/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.4   (API level 19)

SDK Tools Version: 24.0.2
SDK Platform Tools Version: 20
SDK Build Tools Version: 19.1

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.1
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 7.3 (10130.10)
Build 7D111g

=== Xamarin.iOS ===

Version: 9.4.1.15 (Business Edition)
Hash: 28b1990
Branch: master
Build date: 2016-01-04 17:34:31-0500

=== Xamarin.Mac ===

Version: 2.5.0.3130 (Business Edition)

=== Build Information ===

Release ID: 510010006
Git revision: 0b60eecdb531933734519c13257d16a780274aab
Build date: 2015-12-04 20:28:20-05
Xamarin addins: 9876fd7c9837977178411ec7375b4352c0a0d6af
Build lane: monodevelop-lion-cycle6-baseline

=== Operating System ===

Mac OS X 10.11.2
Darwin 360-MACMINIs-Mac-mini-2.local 15.2.0 Darwin Kernel Version 15.2.0
    Fri Nov 13 19:56:56 PST 2015
    root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64
Comment 4 veni.lillkall 2016-06-10 06:57:32 UTC
We're facing the same issue and it still occurs in Xamarin cycle 7 release. For now I've applied the workaround below - a proper fix would be nice!

<AndroidLinkSkip>System.Runtime.Serialization</AndroidLinkSkip>

I think https://bugzilla.xamarin.com/show_bug.cgi?id=39988 is a duplicate of this.
Comment 5 veni.lillkall 2016-06-10 12:43:15 UTC
A more precise fix for linking:

<linker>
  <assembly fullname="System.Runtime.Serialization">
    <type fullname="System.Runtime.Serialization.DateTimeOffsetAdapter">
      <method name="set_OffsetMinutes" />
      <method name="set_UtcDateTime" />
    </type>
  </assembly>
</linker>
Comment 7 Brendan Zagaeski (Xamarin Support) 2016-06-13 14:38:44 UTC
*** Bug 39988 has been marked as a duplicate of this bug. ***
Comment 8 Shruti 2016-09-14 08:51:01 UTC
Reproduce status:

I am successfully able to reproduce this issue using the latest cycle 8 builds, as after the application deployed successfully on the device, on clicking the Button, the application crashes throwing the exception mentioned in he bug.

Verification status:

Using the latest master builds, the issue is fixed, as on clicking the button, the application did not crash.
Below is the screencast for the same:
http://www.screencast.com/t/p0nCFFeVu

Application output: https://gist.github.com/Shruti360/2e971aba170f1881479790162485c3c9

IDE about and logs: https://gist.github.com/shrutis360/32928272c17e70ecfdea26302e3aff47

Please merge the fix on the Cycle 8 Service Release 1 branch as well so that we may proceed and close this issue.

As of now not changing the status to resolved fixed on master and setting the Target Milestone to C8SR1, please let us know if anything else is required from our end.
Comment 10 Shruti 2016-12-07 06:22:17 UTC
I have checked this issue with latest Cycle 9 build and observed that the issue is fixed. As after the application deployed successfully on the device, on clicking the Button, the application doesn't crashes.

Supplement Info:
Application Output: https://gist.github.com/Shruti360/8c7db888eb953bfc4045d4c433ffc9ed
Environment Info and IDE Logs:
https://gist.github.com/shrutis360/77c1745da457d58c65ce7d5786d23c44

Thanks!

Note You need to log in before you can comment on or make changes to this bug.