Bug 37537 - Linker is stripping away a type in a referenced PCL when using the "Link SDK assemblies option"
Summary: Linker is stripping away a type in a referenced PCL when using the "Link SDK ...
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 9.4 (iOS 9.2)
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-01-08 22:44 UTC by Jon Goldberger [MSFT]
Modified: 2016-01-22 10:25 UTC (History)
3 users (show)

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


Attachments
Test Project (625.69 KB, application/zip)
2016-01-08 22:44 UTC, Jon Goldberger [MSFT]
Details
Version info from reporting customer (3.60 KB, application/zip)
2016-01-08 22:47 UTC, Jon Goldberger [MSFT]
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:
RESOLVED ANSWERED

Description Jon Goldberger [MSFT] 2016-01-08 22:44:52 UTC
Created attachment 14510 [details]
Test Project

## Description

When using Linker option "Link SDK Assemblies only" types in a referenced PCL project are stripped. Note this is a forms project and a setup I have not seen before for custom renderers, but it seems the linker should not be touching the PCL assembly when "Link SDK Assemblies only" option is selected. 

Basically set up of solution is:

LinkerRepro project: Xamarin Forms PCL core project
LinkerRepro.iOS: iOS app project
LinkerRepro.UI: Has custom subclass of a forms Entry, CustomEntry (for custom renderer purposes)
LinkerRepro.iOS.UI: Has iOS custom renderer for CustomEntry, CustomEntryRenderer

LinkerRepro references LinkerRepro.UI
LinkerRepro.iOS references LinkerRepro and LinkerRepro.iOS.UI
LinkerRepro.iOS.UI references LinkerRepro.UI

What is getting stripped is the CustomEntryRenderer type in LinkerRepro.iOS.UI. However adding --linkskip=LinkerRepro.iOS.UI does not resolve the issue. But instantiating the CustomEntryRenderer in the AppDelegate does preserve the type and resolve the issue. And of course using "Don't Link" option resolves the issue as well but of course this option can not be used for distribution builds. 

Oddly I also noted that the CustomEntryRenderer type is stripped away in Debug configuration even with the "Don't Link" option selected unless I preserve the type by instantiating it in AppDelegate as above (or calling their Initializer.Init() method, i.e. something that is in the LinkerRepro.iOS.UI assembly.) 

I got the same results in both Xamarin Studio and Visual Studio. 



## Steps to reproduce (Test solution attached)

Note: You might have to remove and replace the references to the LinkerRepro.UI and LinkerRepro.iOS.UI projects. Just open the References folder for all projects and remove and replace those with a yellow warning triangle. I saved the solution after making these changes so it should be fine as is... but just in case. 

1. Use a hardware device, not the simulator.

2. You will obviously need a certificate and provisioning profile. The app id is “com.eschergroup.LinkerRepro”.

3. First, build the solution with the following settings:

a. Adhoc | iPhone.

b. Don’t link (assemblies).

c. Build, then right-click on LinkerRepro.iOS and select Deploy (Or use "Start without debugging")

d. Run the deployed app. 

Expected result: The background colour of the textbox is yellow, meaning the custom renderer has linked correctly.

Actual result: as expected.

4. Second, build the solution with the following settings:

a. Adhoc | iPhone.

b. Link SDK assemblies only.

c. Build, then right-click on LinkerRepro.iOS and select Deploy (Or use "Start without debugging").

d. Run the deployed app. The background colour of the textbox is grey, meaning the custom renderer has been dropped by the linker.

Expected result: The background colour of the textbox is yellow, meaning the custom renderer has linked correctly.

Actual result: The background colour of the textbox is grey, meaning the custom renderer has been dropped by the linker.


5. Finally, build the solution with the following settings:

a. Adhoc | iPhone.

b. Link SDK assemblies only.

c. Set the “Additional mtouch arguments” to “--linkskip=LinkerRepro --linkskip=LinkerRepro.iOS.UI --linkskip=LinkerRepro.UI --linkskip=Xamarin.Forms.Core --linkskip=Xamarin.Forms.Core.Platform --linkskip=Xamarin.Forms.Platform.iOS --linkskip=Xamarin.Forms.Xaml”.

d. Build, then right-click on LinkerRepro.iOS and select Deploy (Or use "Start without debugging").

e. Run the deployed app. The background colour of the textbox is still grey, meaning the --linkskip arguments have not helped.

Expected result: The background colour of the textbox is yellow, meaning the custom renderer has linked correctly.

Actual result: The background colour of the textbox is grey, meaning the --linkskip arguments have not helped.

In step 5, the size of the IPA file will have increased, but the custom renderer functionality is still not included.


## Version info

Microsoft Visual Studio Professional 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00079

Installed Version: Professional

Visual Basic 2015   00322-40000-00000-AA850
Microsoft Visual Basic 2015

Visual C# 2015   00322-40000-00000-AA850
Microsoft Visual C# 2015

Visual C++ 2015   00322-40000-00000-AA850
Microsoft Visual C++ 2015

Visual F# 2015 RC   00322-40000-00000-AA850
Microsoft Visual F# 2015 RC

Windows Phone SDK 8.0 - ENU   00322-40000-00000-AA850
Windows Phone SDK 8.0 - ENU

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools   14.0.20626.0
ASP.NET and Web Tools

ASP.NET Web Frameworks and Tools 2013   5.2.30624.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.5
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

GenerateUnitTest   1.0
Generates unit test code for methods in classes under test.

GitHub.VisualStudio   1.0
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet Package Manager   3.2.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   14.0.50616.0
Microsoft SQL Server Data Tools

Visual Studio Tools for Universal Windows Apps   14.0.23309.00 d14oob
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin   4.0.0.1717 (1390b70)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   6.0.0.35 (d300845)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS   9.3.99.33 (ea30b32)
Visual Studio extension to enable development for Xamarin.iOS.


=== 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.0.109 (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 1 Jon Goldberger [MSFT] 2016-01-08 22:47:42 UTC
Created attachment 14511 [details]
Version info from reporting customer
Comment 2 Sebastien Pouliot 2016-01-14 18:52:48 UTC
@Jon using master I get a grey/silver text box when using adhoc/don't link

Could you duplicate this ? If so please attach both .ipa (working / non-working) to the bug report and I'll have a look at it.
Comment 3 Sebastien Pouliot 2016-01-14 19:26:44 UTC
1. AdHoc vs Debug is only a configuration name in the IDE. mtouch use the options set inside the configuration, the name itself means nothing to it. Always double check the options (same options should provide identical builds) in such cases.


2. No type is removed from user code assemblies when "Link SDK" is used. Also all --linkskip to non-SDK assemblies are not needed (the linker already skips them).

However note that only referenced assemblies will be included in the builds. This is why the workarounds in AppDelegate works, they create a real [1] reference to the required assemblies.

[1] the C# compilers removes the assembly references if nothing is used from an assembly. IOW it's not enough to set them in the IDE, they _must_ be used (or it won't be in the assembly binary and mtouch won't have the information).

The best way to create a good reference is to use:

> [assembly: Preserve (typeof (LinkerRepro.iOS.UI.CustomEntryRenderer), AllMembers = true)]

in AppDelegate.cs as it both create the assembly reference and also tell the linker the type will be (all) needed at runtime.

Using the above I get Debug/Link SDK working (on device and sim) and also AdHoc/Don't link and AdHoc/Link SDK working.
Comment 4 Peter Murphy 2016-01-22 10:25:01 UTC
Hi Sebastien,

I'm the customer who logged this.  The Preserve attribute plus a call to an *empty* Init() method is working well for us.  Thanks for the suggestion.

Regards,

Peter.