Bug 25994 - Unable to set version with NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString(); fails with "System.InvalidProgramException"
Summary: Unable to set version with NSBundle.MainBundle.ObjectForInfoDictionary("CFBun...
Status: RESOLVED ANSWERED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Other ()
Version: 1.10.0
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Chris Hamons
URL:
Depends on:
Blocks:
 
Reported: 2015-01-13 20:26 UTC by Kent Green [MSFT]
Modified: 2015-01-14 13:18 UTC (History)
3 users (show)

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


Attachments
Xamarin Studio logs from customer (510.19 KB, application/zip)
2015-01-13 20:26 UTC, Kent Green [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 Kent Green [MSFT] 2015-01-13 20:26:58 UTC
Created attachment 9345 [details]
Xamarin Studio logs from customer

---Overview---
From this desk case:
https://xamarin.desk.com/agent/case/116844

Customer reports that they're unable to transition their project to the Unified API. In their app, the following snippet throws the exception:

---Snippet from Main.cs---
class MainClass
>    {
>       static void Main(string[] args)
>        {
>			NSApplication.Init();
                         
>                      //Exception is thrown by version assignment
>			var version = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString();
>			var documentsUrl = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0];
>			var documents = documentsUrl.Path;
>			var cacheDirectoryOverride = Path.Combine(documents, "..", "Library", "Caches");

			// This enables Xamarin Insights for runtime analytics and logging
>			Xamarin.Insights.Initialize(VtcApplication.InsightsAppId, version, "VTConnect", cacheDirectoryOverride);

>            NSApplication.Main(args);
>        }
>   }

---Exception Thrown---
System.InvalidProgramException: Invalid IL code in Foundation.NSBundle:get_MainBundle (): IL_0000: ret       
   at Vtc.MainClass.Main (System.String[] args) [0x00006] in /Users/kentgreen/Downloads/VTC-copy/Vtc.Clients/Vtc.Mac/Main.cs:14


---Steps to Reproduce---
Based on my tests, the customer's sample (to be added as a private attachment shortly); reproduces the issue simply by building it. 

---Expected Results---
I noticed that if I copied the same snippet of code into a fresh template project that was either a Classic API Mac App, or a Unified API Mac App; and had no other changes, the snippet hit (based on the breakpoint) and the project built as expected. 

Based on this test it seems like the segment throwing the exception should be valid, but I'm not sure if I might have missed some other factor.

---Actual Results---
The version assignment throws the "System.InvalidProgramException."

---Build Information---
=== Xamarin Studio ===

Version 5.7 (build 661)
Installation UUID: 94acfa66-ee40-4c4a-9a8a-9abffd5435d8
Runtime:
Mono 3.12.0 ((detached/a813491)
GTK+ 2.24.23 (Raleigh theme)

Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.6.0.51 (Business Edition)
Hash: dfb682f
Branch:
Build date: 2015-01-08 13:39:32-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/denniswelu/Library/Developer/Xamarin/android-sdk-mac_x86
Supported Android versions:
2.1 (API level 7)
2.2 (API level 8)
2.3 (API level 10)
3.1 (API level 12)
4.0 (API level 14)
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)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Xamarin.Mac ===

Version: 1.10.0.18 (Business Edition)

=== Build Information ===

Release ID: 507000661
Git revision: b70bab61da996da29045ea8ee8aed1a6faedbe78
Build date: 2015-01-05 16:31:31-05
Xamarin addins: 82f6c71490562d6cd125a09287f441902fdac3d7

=== Operating System ===

Mac OS X 10.10.1
Darwin DennisMBP-2.local 14.0.0 Darwin Kernel Version 14.0.0
Fri Sep 19 00:26:44 PDT 2014
root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64

---Additional Notes---
In my tests to reproduce the bug, I tested both with and without the "Xamarin.Insights.Initialize" line of code; this didn't seem to change the final results.
Comment 2 Kent Green [MSFT] 2015-01-13 20:52:49 UTC
Another observation I forgot to mention is that in the sample that reproduces
the issue, if I substituted a random string like "jkdslfjioew" for
"CFBundleVersion" it seemed to hit the same exception, which makes me think
somehow it's not finding the CFBundleVersion at all as it should do / does in a
template sample.
Comment 3 Shruti 2015-01-14 03:36:54 UTC
I have checked this issue with builds Xamarin.Mac Version: 1.10.0.18 and 1.11.3.0  and  able to reproduce it with build version 1.10.0.18. Here is my observation :

When I launch app with 1.10.0.18, I experienced same exception "System.InvalidProgramException." 
Exception Log: https://gist.github.com/Shruti360/7bd64515539f784d5868
Application Log: https://gist.github.com/Shruti360/21b1d867f8e847ac68e5
IDE Log: https://gist.github.com/Shruti360/b3aac36122fb02a87e4e

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

When I launch app with xam mac build 1.11.3.0 , I faced another issue. In this case, I got exception of "System.Exception: Unable to load required framework: 'lib/Cocoa.Opus.dylib'" 

Exception Log: https://gist.github.com/Shruti360/711088032372114c20c5
Application Log:  https://gist.github.com/Shruti360/50e7da4c4540b98d6bc5
IDE Log: https://gist.github.com/Shruti360/787722ba4136864fabd2

Environment Info:
=== Xamarin Studio ===

Version 5.7.1 (build 2)
Installation UUID: 67eaf3b4-f8a7-4ab4-a9df-3197350ca5dc
Runtime:
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.2 (6736)
Build 6C86e

=== Xamarin.iOS ===

Version: 8.6.0.52 (Enterprise Edition)
Hash: 7c4c2c5
Branch: 
Build date: 2015-01-08 22:29:16-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Enterprise Edition)
Android SDK: /Users/ixamarin78/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		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)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

=== Xamarin.Mac ===

Version: 1.11.3.0 (Enterprise Edition) and 1.10.0.18 

=== Build Information ===

Release ID: 507010002
Git revision: 5a4e27c5dbd44e36bb1a0e2b63acf1e448fa2dbd
Build date: 2015-01-13 19:33:45-05
Xamarin addins: ee38284410f7625ad84602be0a5d0ade0ac779aa

=== Operating System ===

Mac OS X 10.10.0
Darwin XamnewiMac.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 4 Dennis Welu 2015-01-14 09:46:58 UTC
Owning the original project I went the route of recreating the project file manually from the Xam.Mac Unified Template. After doing this the "Invalid IL..." exception was gone but ran into the same problem that the latest comment has noted "System.Exception: Unable to load required framework: 'lib/Cocoa.Opus.dylib’”. So then I went to the 3rd part vendor for that library and tried their example project out. Turns out it is exhibiting that same unable to load error. The "Invalid IL" exception was throwing me off originally but now I get the feeling there is a problem in the 3rd party components. I will contact them and update this issue accordingly.
Comment 5 Chris Hamons 2015-01-14 11:44:37 UTC
"I will contact them and update this issue accordingly." Let me know what you find out from that 3rd party.
Comment 6 Dennis Welu 2015-01-14 13:00:31 UTC
The 3rd party has acknowledged an issue with the dylib's in the project. This is what caused the "Unable to load required framework" exception. However after correcting that the "Invalid IL" issue still remains in the original project. Recreating the project file from an empty template seems to deal with that so this issue is no longer blocking, but, something unknown was causing that Invalid IL problem in the test project. The test project originated from a Classic template that was migrated to Unified about a month ago.
Comment 7 Dennis Welu 2015-01-14 13:16:19 UTC
Not sure if its the cause, but as I review the diff for the Debug configuration between the test project and the rebuilt project, the main difference I see is the addition of a <XamMacArch>x86_64</XamMacArch> element. That sounds important...
Comment 8 Chris Hamons 2015-01-14 13:18:17 UTC
You should check all of the projects in question to see if they are all referencing the same version of Xamarin.Mac and the same .NET framework. 

From what I can tell, your library in question is using .NET 2.0, your application is using the unified mobile profile. 

You may need to either move to the unified full profile or rebuild your libraries as a PCL or against the mobile profile. I believe this is what the invalid IL is coming from.