Bug 56819 - Implementing java interfaces without inheriting Java.Lang.Object should fail early
Summary: Implementing java interfaces without inheriting Java.Lang.Object should fail ...
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: unspecified
Hardware: PC Windows
: Normal normal
Target Milestone: 15.5
Assignee: dean.ellis
Depends on:
Reported: 2017-05-24 22:43 UTC by Sven-Michael Stübe
Modified: 2017-09-06 23:55 UTC (History)
4 users (show)

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:

Description Sven-Michael Stübe 2017-05-24 22:43:37 UTC
Originally posted at the xamarin forums: https://forums.xamarin.com/discussion/72639/implementing-java-interfaces-without-inheriting-java-lang-object-should-fail-early#latest

Jonathan Dick asked me for posting it here again (https://twitter.com/redth/status/867509147062988800)

So here we go:

On stack overflow I answered some questions (e.g. http://stackoverflow.com/questions/38706366/interface-implement-differences-in-c-sharp-vs-java/38707255#38707255 ) where not inheriting from Java.Lang.Object was the cause of the problem. The main problem is, that nobody sees the message on the build output.

> Type 'MyListener ' implements Android.Runtime.IJavaObject but does not inherit from Java.Lang.Object. It is not supported.

Usually the interface is implemented with Handle and Dispose like:

public class SomeImplementation : ISomeJavaInterface
    public IntPtr Handle
            throw new NotImplementedException();
    public void Dispose()
        throw new NotImplementedException();

    // ...
I've added a Stackoverflow documentation for it: http://stackoverflow.com/documentation/xamarin.android/771/bindings/18058/implementing-java-interfaces

I'm wondering if there is any reason for not failing early with a compiler error!? If not, this should be changed :)
Comment 1 Marek Habersack 2017-06-07 21:10:53 UTC
This warning most certainly should become a build error. Since it cannot be really made into a compilation error I'm reassigning the bug to the build system which can capture the message and convert it to an error.
Comment 2 Jonathan Pryor 2017-06-07 21:25:48 UTC
> I'm wondering if there is any reason for not failing early with a compiler error

Yes: compatibility. We "discovered" and raised this as a warning several *years* after initial release, and without knowing how much code this would impact, had -- and still have -- no idea how many projects would suddenly stop building if this warning were made an error. (The warning was added to source control in August 2013; Mono for Android 1.0 was released in 2011.)

"Pick your poison," as they say...

Additionally, this isn't limited to just source code in a project. *ANY* e.g. NuGet package or other library your App Project references could also run afoul of this.

Yes, you shouldn't do that, but wholesale changing everything is...scary, frankly.

@Dean: While this should be an error, we should also add a new MSBuild property to *disable* emitting the error, in case some project which is currently working is working *in spite of this warning.* That way if a project "suddenly breaks," there's something they can do about it.
Comment 3 Jon Douglas [MSFT] 2017-06-23 19:48:37 UTC
I am confirming this behavior based on comments:


This is a common issue that Xamarin.Android developers run into because things like View.IOnClickListener(https://developer.xamarin.com/api/type/Android.Views.View+IOnClickListener/) implement `IJavaObject` but do not inherit from `Java.Lang.Object`. Thus the developer who implements an interface like View.IOnClickListener needs to ensure they inherit `Java.Lang.Object`.

An optional MSBuild property can definitely help identify more cases and ultimately help fix issues in projects, libraries, etc.
Comment 4 dean.ellis 2017-07-19 08:51:21 UTC
This is a more involved fix than at first thought. Moving to the 15.5 milestone.
Comment 5 Jonathan Pryor 2017-08-30 16:39:58 UTC
PR: https://github.com/xamarin/xamarin-android/pull/797
Comment 6 Jonathan Pryor 2017-09-06 23:55:49 UTC
Fixed in: