Demystify Android Resource.Designer.cs

I installed the Oxyplot nuget package into my Android project. Unfortunately it added some lines to the Resource.Designer.cs file that caused compile errors. Deleting the offending lines fixed the problem - until a subsequent invocation of the package manager after which point the offending lines are re-added. My solution was to "hide" the file from the Oxyplot package update by renaming the file and class to Rezource (with a 'z'), but this caused the app to fail at startup with a 'resource not found' error. My question is, how does this file work?...what's the magic behind it, and why would renaming the file and/or class cause a failure?

Tagged:

Best Answer

Answers

  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    This is an autogenerated file that defines the ID's for all resources in your app and any project that it references.

    When you add any resource like a layout file, a string, or an ID, an entry is created in this file automatically. When you reference a resource in code such as Resource.Layout.my_view it refers to a constant defined in this file.

    If you delete it then it will get regenerated when you re-compile, but if the whole app wasn't rebuilt it may not have been regenerated successfully after you renamed it, giving the resource not found error.

    I'm surprised that you got compiler errors after adding a package. What errors did you get? Sometimes adding support libraries can give these kind of errors and a clean/rebuild all solves the problem.

  • DanP.2059DanP.2059 USMember

    Thanks @JimBennett. In all there were 151 errors that were some variation of:

    'Resource.Attribute' does not contain a definition for 'mediaRouteSettingsDrawable' xxxxxxxxxxx.Droid   D:\Project\xxxxxxxxxxx.Droid\Resources\Resource.Designer.cs
    

    The error was for this line of code:

    global::OxyPlot.Xamarin.Forms.Platform.Android.Resource.Attribute.mediaRouteSettingsDrawable = global::xxxxxxxxxxx.Droid.Resource.Attribute.mediaRouteSettingsDrawable;
    

    I removed the package, and instead added references to the Oxyplot assemblies directly. The first compile after adding the references reintroduced all the offending lines of code into Resources.Designer.cs which means it has nothing to do with the package manager. Note that if I then manually delete the lines, they are not re-added upon subsequent compiles.

  • DanP.2059DanP.2059 USMember

    Update...doing a rebuild all puts the lines back in the resource file. This is why I always saw the error when building on TFS. Something is scanning the Oxyplot assemblies for resources and adding them at build time.

  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    Reason I ask is there is a bug registered against OxyPlot for what you are seeing but with a Forms app and the 'fix' seems to be build the assemblies from source as the NuGet package has not been updated with the fix:

    https://github.com/oxyplot/oxyplot/issues/918

  • DanP.2059DanP.2059 USMember

    Yeah I saw that issue, but I wasn't really sure how it was happening hence my question. I'm building a Xamarin Forms app and was simply experimenting with Oxyplot.

Sign In or Register to comment.