Bug 22877 - Unable to override Application.GetSystemService
Summary: Unable to override Application.GetSystemService
Status: RESOLVED DUPLICATE of bug 11182
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.16.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2014-09-11 12:51 UTC by Gabe Martin
Modified: 2014-09-11 14:00 UTC (History)
3 users (show)

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

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 DUPLICATE of bug 11182

Description Gabe Martin 2014-09-11 12:51:30 UTC
When attempting to run the below simple sample application it works as long as the SampleApplication.GetSystemService method is not overridden. Once this method is overridden even to simply call the base implementation results in the following Java runtime exception.

This failed on two different Mac OS X development systems running Xamarin Studio but seemed to work on a Windows System.

We need the ability to override Application.GetSystemService because the intent is to use this method as a way to inject Xamarin based services into an existing Android library project.


-----------------------------------
Sample App
-----------------------------------
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace SampleApp2
{
	[Activity (Label = "SampleApp2", MainLauncher = true, Icon = "@drawable/icon")]
	public class MainActivity : Activity
	{
		int count = 1;

		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById<Button> (Resource.Id.myButton);
			
			button.Click += delegate {
				button.Text = string.Format ("{0} clicks!", count++);
			};
		}
	}

	[Application]
	public class SampleApplication : Application
	{
		public SampleApplication ()
		{
		}

		public override Java.Lang.Object GetSystemService (string name)
		{
			return base.GetSystemService (name);
		}
	}
}


-----------------------------------
Log output
-----------------------------------
Forwarding debugger port 8841
Detecting existing process
[monodroid-debug] Trying to initialize the debugger with options: --debugger-agent=transport=dt_socket,loglevel=0,address=127.0.0.1:8841,server=y,embedding=1
[Mono] Image addref mscorlib[0xb8076aa0] -> mscorlib.dll[0xb8088670]: 1
[Mono] AOT module 'mscorlib.dll.so' not found: dlopen failed: library "/data/data/SampleApp2.SampleApp2/lib/mscorlib.dll.so" not found
[Mono] Assembly mscorlib[0xb8076aa0] added to domain RootDomain, ref_count=1
[Mono] Assembly Loader probing location: '/storage/emulated/0/Android/data/SampleApp2.SampleApp2/files/.__override__/SampleApp2.dll'.
[Mono] Image addref SampleApp2[0xb80be1c8] -> /storage/emulated/0/Android/data/SampleApp2.SampleApp2/files/.__override__/SampleApp2.dll[0xb80bd468]: 2
[Mono] Assembly SampleApp2[0xb80be1c8] added to domain RootDomain, ref_count=1
[Mono] AOT module '/storage/emulated/0/Android/data/SampleApp2.SampleApp2/files/.__override__/SampleApp2.dll.so' not found: dlopen failed: library "/data/data/SampleApp2.SampleApp2/lib//storage/emulated/0/Android/data/SampleApp2.SampleApp2/files/.__override__/SampleApp2.dll.so" not found
[Mono] Assembly Loader loaded assembly from location: '/storage/emulated/0/Android/data/SampleApp2.SampleApp2/files/.__override__/SampleApp2.dll'.
[Mono] Config attempting to parse: '/storage/emulated/0/Android/data/SampleApp2.SampleApp2/files/.__override__/SampleApp2.dll.config'.
[Mono] Config attempting to parse: '/Users/builder/data/lanes/1131/62e09eb0/source/monodroid/builds/install/mono-x86/etc/mono/assemblies/SampleApp2/SampleApp2.config'.
[monodroid-gc] GREF GC Threshold: 46800
[Mono] Image addref Mono.Android[0xb80bef78] -> Mono.Android.dll[0xb80be5a0]: 1
[Mono] Assembly Mono.Android[0xb80bef78] added to domain RootDomain, ref_count=1
[Mono] AOT module 'Mono.Android.dll.so' not found: dlopen failed: library "/data/data/SampleApp2.SampleApp2/lib/Mono.Android.dll.so" not found
[Mono] Assembly Ref addref Mono.Android[0xb80bef78] -> mscorlib[0xb8076aa0]: 2
Loaded assembly: /storage/emulated/0/Android/data/SampleApp2.SampleApp2/files/.__override__/SampleApp2.dll
Loaded assembly: Mono.Android.dll [External]
[AndroidRuntime] Shutting down VM
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: SampleApp2.SampleApp2, PID: 1340
[AndroidRuntime] java.lang.UnsatisfiedLinkError: Native method not found: sampleapp2.SampleApplication.n_getSystemService:(Ljava/lang/String;)Ljava/lang/Object;
[AndroidRuntime] 	at sampleapp2.SampleApplication.n_getSystemService(Native Method)
[AndroidRuntime] 	at sampleapp2.SampleApplication.getSystemService(SampleApplication.java:25)
[AndroidRuntime] 	at android.content.ContentProvider.attachInfo(ContentProvider.java:1581)
[AndroidRuntime] 	at android.content.ContentProvider.attachInfo(ContentProvider.java:1562)
[AndroidRuntime] 	at mono.MonoRuntimeProvider.attachInfo(MonoRuntimeProvider.java:50)
[AndroidRuntime] 	at android.app.ActivityThread.installProvider(ActivityThread.java:4790)
[AndroidRuntime] 	at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
[AndroidRuntime] 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
[AndroidRuntime] 	at android.app.ActivityThread.access$1500(ActivityThread.java:135)
[AndroidRuntime] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
[AndroidRuntime] 	at android.os.Handler.dispatchMessage(Handler.java:102)
[AndroidRuntime] 	at android.os.Looper.loop(Looper.java:136)
[AndroidRuntime] 	at android.app.ActivityThread.main(ActivityThread.java:5017)
[AndroidRuntime] 	at java.lang.reflect.Method.invokeNative(Native Method)
[AndroidRuntime] 	at java.lang.reflect.Method.invoke(Method.java:515)
[AndroidRuntime] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
[AndroidRuntime] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
[AndroidRuntime] 	at dalvik.system.NativeStart.main(Native Method)
Comment 1 Parmendra Kumar 2014-09-11 13:39:47 UTC
I have checked this issue as per given step on bug description, And I am able to reproduce it.
I observed that Application.GetSystemService is not override. 

Output Log: https://gist.github.com/saurabh360/77fe444e2eae043fa250
Screencast: http://screencast.com/t/0N7QgZ5GROXo

Environment Info: 
=== Xamarin Studio ===

Version 5.3 (build 441)
Installation UUID: 1a096c6f-0678-402e-89b2-a2c10f7e80e4
Runtime:
	Mono 3.8.0 ((no/45d0ba1)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 308000009

=== Apple Developer Tools ===

Xcode 5.1 (5084)
Build 5B130a

=== Xamarin.Mac ===

Version: 1.10.0.10 (Business Edition)

=== Xamarin.Android ===

Version: 4.16.0 (Business Edition)
Android SDK: /Users/360_macmini/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)
		3.2    (API level 13)
		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)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.iOS ===

Version: 7.4.0.108 (Business Edition)
Hash: 77efa3f
Branch: 
Build date: 2014-08-26 07:59:55-0400

=== Build Information ===

Release ID: 503000441
Git revision: befb6aa1176d37a5f678f4274f340a0159091b7a
Build date: 2014-09-08 17:57:02-04
Xamarin addins: 6dc7c388e31fdfc8014689839d37de0d4622435c

=== Operating System ===

Mac OS X 10.9.4
Darwin ShrutiMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 2 Jonathan Pryor 2014-09-11 14:00:33 UTC
The problem is that Application.getSystemService() is being invoked before Xamarin has had a chance to initialize:

> [AndroidRuntime] java.lang.UnsatisfiedLinkError: Native method not found: sampleapp2.SampleApplication.n_getSystemService:(Ljava/lang/String;)Ljava/lang/Object;
> [AndroidRuntime]     at sampleapp2.SampleApplication.n_getSystemService(Native Method)
> [AndroidRuntime]     at sampleapp2.SampleApplication.getSystemService(SampleApplication.java:25)
> [AndroidRuntime]     at android.content.ContentProvider.attachInfo(ContentProvider.java:1581)
> [AndroidRuntime]     at android.content.ContentProvider.attachInfo(ContentProvider.java:1562)
> ...

We need to rethink Application initialization.

*** This bug has been marked as a duplicate of bug 11182 ***