Skip to content

I can't compile this code. #1151

Closed
Closed

Description

In Xamarin.android I am trying to compile

   public ArticleActivity(string path) : base()
        {
            _path = path;
        }

or

 public ArticleActivity(string path)
        {
            _path = path;
        }

None of both compiling. The error is GenerateJavaSubs. This thing can be improved in visual studio tooling. At the first place it's not showing detail until I click on detail in errors. It could be better if It's show error on the same code where it's have issue.

Activity

  1. JonDouglas commented on Jan 4, 2018

    @JonDouglas
    Contributor

    @anirugu Can you upload a full diagnostic build output?

  2. added
    Area: App+Library Build
    enhancement
    on Jan 4, 2018
  3. jonpryor commented on Jan 4, 2018

    @jonpryor
    Contributor

    The problem is that the error message isn't entirely useful. Build from the command-line, and get:

    error MSB4018: The "GenerateJavaStubs" task failed unexpectedly. […/Scratch.Gxa1151/Scratch.Gxa1151/Scratch.Gxa1151.csproj]
    error MSB4018: System.InvalidOperationException: The type 'Scratch.Gxa1151.MainActivity' needs to have a public default constructor. […/Scratch.Gxa1151/Scratch.Gxa1151/Scratch.Gxa1151.csproj]
    error MSB4018:   at Xamarin.Android.Tasks.ManifestDocument.Merge (System.Collections.Generic.List`1[T] subclasses, System.Collections.Generic.List`1[T] selectedWhitelistAssemblies, System.String applicationClass, System.Boolean embed, System.String bundledWearApplicationName, System.Collections.Generic.IEnumerable`1[T] mergedManifestDocuments) [0x004aa] in <acf4a3302c8d4a7eaf06af6dce1aafb2>:0  [… /Scratch.Gxa1151/Scratch.Gxa1151/Scratch.Gxa1151.csproj]
    error MSB4018:   at Xamarin.Android.Tasks.GenerateJavaStubs.Run (Java.Interop.Tools.Cecil.DirectoryAssemblyResolver res) [0x00491] in <acf4a3302c8d4a7eaf06af6dce1aafb2>:0  [… /Scratch.Gxa1151/Scratch.Gxa1151/Scratch.Gxa1151.csproj]
    error MSB4018:   at Xamarin.Android.Tasks.GenerateJavaStubs.Execute () [0x00225] in <acf4a3302c8d4a7eaf06af6dce1aafb2>:0  [… /Scratch.Gxa1151/Scratch.Gxa1151/Scratch.Gxa1151.csproj]
    error MSB4018:   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x00023] in <76dd0031e0df4aa99d9befb4c4585c69>:0  [… /Scratch.Gxa1151/Scratch.Gxa1151/Scratch.Gxa1151.csproj]
    error MSB4018:   at Microsoft.Build.BackEnd.TaskBuilder+<ExecuteInstantiatedTask>d__26.MoveNext () [0x0022d] in <76dd0031e0df4aa99d9befb4c4585c69>:0  [… /Scratch.Gxa1151/Scratch.Gxa1151/Scratch.Gxa1151.csproj]
    

    The bug is that Activity subclasses with an [Activity] custom attribute -- i.e. any type within AndroidManifest.xml that will be instantiated by Android -- must have a default constructor.

    So we're doing the quasi-right thing here: there is an error, and we're reporting it.

    The problem is the way we're reporting it: it's useless:

    Line Description File Project Path
    1794 The "GenerateJavaStubs" task failed unexpectedly. Xamarin.Android.Common.targets Scratch.Gxa1151 ../../../../../../Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets

    What we should report is, in the case of my example (I took the default template, and added a public MainActivity(string) constructor), report:

    Line Description File Project Path
    8 XAwhatever: The type MainActivity must contain a default constructor. MainActivity.cs Scratch.Gxa1151 MainActivity.cs
  4. jonpryor commented on Jan 4, 2018

    @jonpryor
    Contributor

    Sketching out the fix:

    Where we report the error needs to crib the LookupSource(TypeDefinition) method to obtain the filename and line number of the offending type, so that we can emit a better formed error message.

    ManifestDocument.cs should also use log.LogError() instead of throwing the exception.

  5. added this to the d15-7 milestone on Jan 4, 2018
  6. asterixorobelix commented on Jan 27, 2018

    @asterixorobelix

    I have been having this issue too. After adding a blank constructor to the Activity, the error went away..

  7. jonpryor commented on Jan 27, 2018

    @jonpryor
    Contributor

    @asterixorobelix: Any type mentioned in AndroidManifest.xml -- any type which is created by Android -- must have a default constructor.

  8. ghost locked as resolved and limited conversation to collaborators on Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: App+Library Buildenhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions