Bug 43553 - Using "Run Code Analysis" on Android Class Library hits ""GenerateJavaStubs" task failed unexpectedly ... Sequence contains no elements" unless the library includes a subclass of `Activity`
Summary: Using "Run Code Analysis" on Android Class Library hits ""GenerateJavaStubs" ...
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: General (show other bugs)
Version: 7.0 (C8)
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
: 43537 (view as bug list)
Depends on:
Blocks:
 
Reported: 2016-08-20 00:50 UTC by Brendan Zagaeski (Xamarin Support)
Modified: 2017-04-18 20:33 UTC (History)
6 users (show)

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


Attachments
Example diagnostic MSBuild output (243.36 KB, text/plain)
2016-08-20 00:50 UTC, Brendan Zagaeski (Xamarin Support)
Details
Detailed version information (1.67 KB, text/plain)
2016-08-20 00:55 UTC, Brendan Zagaeski (Xamarin Support)
Details

Description Brendan Zagaeski (Xamarin Support) 2016-08-20 00:50:36 UTC
Created attachment 17134 [details]
Example diagnostic MSBuild output

Using "Run Code Analysis" on Android Class Library hits ""GenerateJavaStubs" task failed unexpectedly ... Sequence contains no elements" unless the library includes a subclass of `Activity`


This bug is related to Bug 43548, but that bug discusses the error message in the context of the normal build and deploy process for Android _application projects_.  In this second bug, the problem instead occurs under the more "special" condition of using the "Run Code Analysis" feature on Android _class libraries_.

It looks like the problem in this scenario is that setting the `RunCodeAnalysis` MSBuild property to `true` causes some additional targets to run that are not part of the normal build steps for Android class libraries.  It seems that some of those additional targets are only compatible with Android _application projects_ and should not ever be run for _class libraries_, regardless of whether `RunCodeAnalysis` is set to `true` or not.




## Steps to replicate (on Mac in this example)


1. Create a new "Android > Library > Class Library" in Xamarin Studio.


2. Select "Project > Run Code Analysis on $(MSBuildProjectName)"

-   Or build the project on the command line with `RunCodeAnalysis` set to `true`:

    xbuild /p:Configuration=Debug /p:RunCodeAnalysis=true AndroidClassLibrary1/AndroidClassLibrary1.csproj




## One additional experiment

Another way to replicate the error (though it might be "somewhat invalid") is to try to run the `SignAndroidPackage` target on the class library:

xbuild /p:Configuration=Debug /t:SignAndroidPackage AndroidClassLibrary1/AndroidClassLibrary1.csproj




## "Hacky" workaround

One way to work around the problem is to add an empty Activity subclass to the class library so that the class library can "pretend" to be a full Android application project:

[Activity(MainLauncher = true)]
public class MainActivity : Activity
{
}




## BAD Results

The build fails with the same error as Bug 43548.  Presumably this is because the build process is being "tricked into thinking" that it's building an application project.  Of course Android class libraries should _not_ be required to contain an `Activity` subclass, so the build failure is not appropriate in this scenario.

> /Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: error : Error executing task GenerateJavaStubs: Sequence contains no elements
> 		Error executing task GenerateJavaStubs: System.InvalidOperationException: Sequence contains no elements
>   at System.Linq.Enumerable.Max (System.Collections.Generic.IEnumerable`1[T] source) [0x0006a] in <4a648327db854c86ab0ece073e38f4b3>:0 
>   at System.Linq.Enumerable.Max[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] selector) [0x00007] in <4a648327db854c86ab0ece073e38f4b3>:0 
>   at Java.Interop.Tools.JavaCallableWrappers.TypeNameMapGenerator.WriteBinaryMapping (System.IO.Stream o, System.Collections.Generic.Dictionary`2[TKey,TValue] mapping) [0x00015] in <3bec6a0f8b3945ab8cd1742c43134d59>:0 
>   at Java.Interop.Tools.JavaCallableWrappers.TypeNameMapGenerator.WriteJavaToManaged (System.IO.Stream output) [0x00070] in <3bec6a0f8b3945ab8cd1742c43134d59>:0 
>   at Xamarin.Android.Tasks.GenerateJavaStubs.UpdateWhenChanged (System.String path, System.Action`1[T] generator) [0x00014] in <7b8cf0a088cb4e499a73d931c1c995f9>:0 
>   at Xamarin.Android.Tasks.GenerateJavaStubs.WriteTypeMappings (System.Collections.Generic.List`1[T] types) [0x00078] in <7b8cf0a088cb4e499a73d931c1c995f9>:0 
>   at Xamarin.Android.Tasks.GenerateJavaStubs.Run () [0x0016e] in <7b8cf0a088cb4e499a73d931c1c995f9>:0 
>   at Xamarin.Android.Tasks.GenerateJavaStubs.Execute () [0x001d8] in <7b8cf0a088cb4e499a73d931c1c995f9>:0 
>   at Microsoft.Build.BuildEngine.TaskEngine.Execute () [0x00000] in <b1524340ac7e4c06b22fac1b2db62c08>:0 
>   at Microsoft.Build.BuildEngine.BuildTask.Execute () [0x0008f] in <b1524340ac7e4c06b22fac1b2db62c08>:0 



## Regression status: bug in a new feature



### BAD results in Cycle 6

Dating back to Cycle 6, Xamarin.Android includes 2 `.targets` files that mention `RunCodeAnalysis`:

/Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/xbuild/Xamarin/Android/Xamarin.Analysis.target
/Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/xbuild/Xamarin/Android/Xamarin.Analysis.Compatibility.targets


The _exact_ error message that occurs for this problem has admittedly changed a bit since the first Cycle 6 versions, but I think the core problem (running inappropriate targets when `RunCodeAnalysis` is `true`) has probably been the same all along.

BAD:   Xamarin.Android 6.2.0.36 (Cycle 8 Beta 1)
BAD:   Xamarin.Android 6.0.0.34 (Cycle 6)



### `RunCodeAnalysis` is probably ignored in "Cycle 5 – Service Release 5" and earlier

Xamarin.Android 5.1.9.0 (Cycle 5 – Service Release 5) includes _no_ `.targets` files that mention `RunCodeAnalysis`, so I suspect that the `RunCodeAnalysis` MSBuild property is simply ignored in that version (and most likely all earlier versions too).




## Additional version info (brief)

Xamarin Studio Version 6.1 (build 5345)
Mono 4.6.0 (mono-4.6.0-branch/d0fc1a6) (64-bit)
OS X 10.11.6
Comment 1 Brendan Zagaeski (Xamarin Support) 2016-08-20 00:55:09 UTC
Created attachment 17135 [details]
Detailed version information
Comment 2 Brendan Zagaeski (Xamarin Support) 2016-08-20 00:58:11 UTC
*** Bug 43537 has been marked as a duplicate of this bug. ***
Comment 3 me 2017-04-07 15:05:19 UTC
Is there any update on a fix for this issue? Still experiencing it it Visual Studio 2017 with Xamarin for Visual Studio 4.3.795 and Xamarin.Android 7.1.0.43
Comment 4 me 2017-04-18 20:33:04 UTC
Any update?

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