Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Invalid Resources generated for DesignTime #3007

Closed
NPadrutt opened this issue Apr 15, 2019 · 21 comments · Fixed by #3054
Closed

[Bug] Invalid Resources generated for DesignTime #3007

NPadrutt opened this issue Apr 15, 2019 · 21 comments · Fixed by #3054
Assignees
Labels
Area: App+Library Build Issues when building Library projects or Application projects. Area: Design-Time Build Issues with VS design-time build.
Milestone

Comments

@NPadrutt
Copy link

Description

With Version 3.5.x of Xamarin Forms I have a lot of errors all regarding the Resource.Designer in the obj-Debug-[Version]-designtime folder.
I experimented around with the target and minimum Android SDK Version but had it with both 9.0 and 8.1.

(since I was no able to get the errors in an output, I add a Screenshot here)

image

It seems that this is caused by entries like:

		// aapt resource value: 0x7F010174
		public const int bottomtab.navarea = 2130772340;
		
		// aapt resource value: 0x7F010175
		public const int bottomtab.tabbar = 2130772341;

Also these do not exist in the Resource.Designer used to build the app.
I tried deleting all the bin and obj folders, but the file is always regenerated with the same entries.

Steps to Reproduce

Open a solution in either VS 2019 or 2017 with Xamarin Forms >= 3.5 in the project.

Expected Behavior

No Errors.

Actual Behavior

Error messages appear.

Basic Information

  • Version with issue:
  • Last known good version: 3.4.0.1039999
  • IDE: Visual Studio 2017 / 2019
  • Platform Target Frameworks:
    • iOS:
    • Android: 8.1, 9
    • UWP:
  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices:

Screenshots

Reproduction Link

App1.zip

@samhouts samhouts transferred this issue from xamarin/Xamarin.Forms Apr 19, 2019
@pjcollins pjcollins added this to the Under Consideration milestone Apr 22, 2019
@pjcollins pjcollins added Area: App+Library Build Issues when building Library projects or Application projects. Area: Design-Time Build Issues with VS design-time build. labels Apr 22, 2019
@pjcollins
Copy link
Member

I was able to reproduce some resolution issues when loading this project "untouched" in VS2017/2019, but deleting the included bin and obj folders and allowing a fresh NuGet restore to complete did put me back in a working state. There seems to be a potential bin/obj and NuGet related issue that could be investigated further using the attached project. However, I didn't encounter any Resource.designer.cs related errors after changing target framework versions or playing around with this project in VS2017 and VS2019.

@NPadrutt
Copy link
Author

@pjcollins Thanks for your feedback. Have you restartet VS after you where able to build? What I observed is the Designer file recreated on a restart.

@jonathanpeppers
Copy link
Member

I think I can reproduce it command-line (I deleted bin/obj directories first):

# Build the netstandard project
> msbuild .\App1\App1\App1.csproj /restore
# Simulate a design-time build that would happen in the IDE
> msbuild .\App1\App1.Android\App1.Android.csproj /restore /t:Compile /p:DesignTimeBuild=True /p:BuildingInsideVisualStudio=True

This was VS 2019 GA.

Got a bunch of errors like:

  obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided [C:\Users\jopepper\Downloads\App1 (5)\App1\App1\App1.Android\App1.Android.csproj]
  obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ',' expected [C:\Users\jopepper\Downloads\App1 (5)\App1\App1\App1.Android\App1.Android.csproj]
  obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected [C:\Users\jopepper\Downloads\App1 (5)\App1\App1\App1.Android\App1.Android.csproj]

Will see if this happens with xamarin-android/master.

@jonathanpeppers
Copy link
Member

So it looks like master is working, but with lots of warnings:

# There were ~2,000 of these!
Xamarin.Android.Common.targets(1332,2): warning XA0106: Skipping App1.Droid.Resource.Styleable.ViewStubCompat_android_layout. Please check that your Nuget Package versions are compatible.

I need to figure out where the problem is here, it seems we have duplicate resources in some of the support library NuGets?

It looks like the problem with the C# compiler error was fixed in: 752a25f Duplicates are now warnings, why are there so many?

@dellis1972
Copy link
Contributor

This looks related to this change https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.

Xamarin.Forms is using android:id="@+id/bottomtab.tabbar". It seems the ManagedResourceParser is NOT fixing up android id values with contain a ..

It should be producing public const int toolbar_one = 2131230899; not public const int toolbar.one = 2131230899;

There might be a way to work around this but it will come at a design time build performance cost. You can set the AndroidUseManagedDesignTimeResourceGenerator to False in your csproj. This will mean the design time build will fall back on the old aapt based generator (which is slower), but it should work.

Place the following in the first PropertyGroup if the Android csproj.

<AndroidUseManagedDesignTimeResourceGenerator>False</AndroidUseManagedDesignTimeResourceGenerator>

@SebastianKruseWago
Copy link

@dellis1972 Seems to solve the issue but what did you mean with this?

it will come at a design time build performance cost

Do you mean the very aweful Xamarin.Forms Previewer? Or the Android native "designer" which is also average at best?

@dellis1972
Copy link
Contributor

The Design Time Build is what Visual Studio does in the background to generate code completion and intellisense. Normally this build should run very quickly, this is one of the reasons the ManagedResourceParser exists. Aapt however can be very slow, especially on projects with allot of resources. So disabling the ManagedResourceParser might mean the IDE becomes a bit sluggish when a Design Time Build is running, this is normally when you save a axml layout file you are working on.

I'm working on a fix for the ManagedResourceParser now.

dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 25, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit reworks the parser to use the `CodeDomProvider.CreateValidIdentifier`
method to create a valid c# identifier from the name passed in.
However.. it turns out that `CodeDomProvider.CreateValidIdentifier` thinks
that a `.` is a valid identifier character. This is despite
`CodeDomProvider.IsValidIdentifier` returning `false` for that value...

So lets ALSO do a string replace '.' for '_' as well.

I also updated the unit test to test for this senario.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 25, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit reworks the parser to use the `CodeDomProvider.CreateValidIdentifier`
method to create a valid c# identifier from the name passed in.
However.. it turns out that `CodeDomProvider.CreateValidIdentifier` thinks
that a `.` is a valid identifier character. This is despite
`CodeDomProvider.IsValidIdentifier` returning `false` for that value...

So lets ALSO do a string replace '.' for '_' as well.

I also updated the unit test to test for this senario.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 25, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit reworks the parser to use the `CodeDomProvider.CreateValidIdentifier`
method to create a valid c# identifier from the name passed in.
However.. it turns out that `CodeDomProvider.CreateValidIdentifier` thinks
that a `.` is a valid identifier character. This is despite
`CodeDomProvider.IsValidIdentifier` returning `false` for that value...

So lets ALSO do a string replace '.' for '_' as well.

I also updated the unit test to test for this senario.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 25, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit reworks the parser to use the `CodeDomProvider.CreateValidIdentifier`
method to create a valid c# identifier from the name passed in.
However.. it turns out that `CodeDomProvider.CreateValidIdentifier` thinks
that a `.` is a valid identifier character. This is despite
`CodeDomProvider.IsValidIdentifier` returning `false` for that value...

So lets ALSO do a string replace '.' for '_' as well.

I also updated the unit test to test for this senario.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 26, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which
is responsible for making sure the identifier we generate
is a valid C# identifier. This logic is based on the
C# language specifications [1]. It also does keyword
detection and prepends an `@` if required. Just in case
a user calls an item `base` or `class` or any other
C# keyword.

This commit also reworks the id generation of the
`ManagedResourceParser` to replicate exactly the
ids which `aapt2` produce. As a result if the managed
parser generates the code from the `R.txt` file or
directly from the resources, the resulting designer.cs
file will be IDENTICAL.

This should help us reduce some of the calls to `aapt`
in the future.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords

Rework the ManagedResourceParser to produce the same results as R.txt
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 26, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which
is responsible for making sure the identifier we generate
is a valid C# identifier. This logic is based on the
C# language specifications [1]. It also does keyword
detection and prepends an `@` if required. Just in case
a user calls an item `base` or `class` or any other
C# keyword.

This commit also reworks the id generation of the
`ManagedResourceParser` to replicate exactly the
ids which `aapt2` produce. As a result if the managed
parser generates the code from the `R.txt` file or
directly from the resources, the resulting designer.cs
file will be IDENTICAL.

This should help us reduce some of the calls to `aapt`
in the future.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 26, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which
is responsible for making sure the identifier we generate
is a valid C# identifier. This logic is based on the
C# language specifications [1]. It also does keyword
detection and prepends an `@` if required. Just in case
a user calls an item `base` or `class` or any other
C# keyword.

This commit also reworks the id generation of the
`ManagedResourceParser` to replicate exactly the
ids which `aapt2` produce. As a result if the managed
parser generates the code from the `R.txt` file or
directly from the resources, the resulting designer.cs
file will be IDENTICAL.

This should help us reduce some of the calls to `aapt`
in the future.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 29, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which
is responsible for making sure the identifier we generate
is a valid C# identifier. This logic is based on the
C# language specifications [1]. It also does keyword
detection and prepends an `@` if required. Just in case
a user calls an item `base` or `class` or any other
C# keyword.

This commit also reworks the id generation of the
`ManagedResourceParser` to replicate exactly the
ids which `aapt2` produce. As a result if the managed
parser generates the code from the `R.txt` file or
directly from the resources, the resulting designer.cs
file will be IDENTICAL.

This should help us reduce some of the calls to `aapt`
in the future.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue Apr 30, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which
is responsible for making sure the identifier we generate
is a valid C# identifier. This logic is based on the
C# language specifications [1]. It also does keyword
detection and prepends an `@` if required. Just in case
a user calls an item `base` or `class` or any other
C# keyword.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue May 1, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which
is responsible for making sure the identifier we generate
is a valid C# identifier. This logic is based on the
C# language specifications [1]. It also does keyword
detection and prepends an `@` if required. Just in case
a user calls an item `base` or `class` or any other
C# keyword.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
jonpryor pushed a commit that referenced this issue May 3, 2019
…ime (#3054)

Fixes: #3007

Xamarin.Forms introduced some new `android:id` values in the
[`Xamarin.Forms.Platform.Android` assembly][1].  These values include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers are
"fixed up" to be valid Java identifiers, replacing `.` with `_`.  The
same is NOT true for the `ManagedResourceParser`.  As a result the
very first design time build will fail with the following error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which is
responsible for making sure the identifier we generate is a valid C#
identifier.  This logic is based on the [C# language specification][1].
It also does keyword detection and prepends an `@` if required, just
in case a user calls an item `base` or `class` or any other C# keyword.

[1]: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13
[2]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
dellis1972 added a commit that referenced this issue May 3, 2019
…ime (#3054)

Fixes: #3007

Xamarin.Forms introduced some new `android:id` values in the
[`Xamarin.Forms.Platform.Android` assembly][1].  These values include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers are
"fixed up" to be valid Java identifiers, replacing `.` with `_`.  The
same is NOT true for the `ManagedResourceParser`.  As a result the
very first design time build will fail with the following error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which is
responsible for making sure the identifier we generate is a valid C#
identifier.  This logic is based on the [C# language specification][1].
It also does keyword detection and prepends an `@` if required, just
in case a user calls an item `base` or `class` or any other C# keyword.

[1]: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13
[2]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue May 3, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which
is responsible for making sure the identifier we generate
is a valid C# identifier. This logic is based on the
C# language specifications [1]. It also does keyword
detection and prepends an `@` if required. Just in case
a user calls an item `base` or `class` or any other
C# keyword.

This commit also reworks the id generation of the
`ManagedResourceParser` to replicate exactly the
ids which `aapt2` produce. As a result if the managed
parser generates the code from the `R.txt` file or
directly from the resources, the resulting designer.cs
file will be IDENTICAL.

This should help us reduce some of the calls to `aapt`
in the future.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
dellis1972 added a commit to dellis1972/xamarin-android that referenced this issue May 3, 2019
Fixes xamarin#3007

Xamarin.Forms introduced some new `android:id` values in the
`Xamarin.Forms.Platform.Android` assembly [1]. These include
a period `.` in the identifier, such as

	android:id="@+id/bottomtab.tabbar"
	android:id="@+id/toolbar.one"

When processing these with `aapt` or `aapt2` these identifiers
are "fixed up" to be valid java identifiers. The same is
NOT true for the `ManagedResourceParser`. As a result
the very first design time build will fail with the following
error.

	obj\Debug\81\designtime\Resource.designer.cs(5048,21,5048,30): error CS0145: A const field requires a value to be provided
	obj\Debug\81\designtime\Resource.designer.cs(5048,30,5048,31): error CS1003: Syntax error, ','
	obj\Debug\81\designtime\Resource.designer.cs(5048,31,5048,38): error CS1002: ; expected

This is because it is generating the following code in the
`Resource.designer.cs`

	public const int toolbar.one = 2131230899;

which is NOT a valid C# identifier.

This commit adds a new `ResourceIdentifier` class which
is responsible for making sure the identifier we generate
is a valid C# identifier. This logic is based on the
C# language specifications [1]. It also does keyword
detection and prepends an `@` if required. Just in case
a user calls an item `base` or `class` or any other
C# keyword.

This commit also reworks the id generation of the
`ManagedResourceParser` to replicate exactly the
ids which `aapt2` produce. As a result if the managed
parser generates the code from the `R.txt` file or
directly from the resources, the resulting designer.cs
file will be IDENTICAL.

This should help us reduce some of the calls to `aapt`
in the future.

[1] https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Platform.Android/Resources/Layout/BottomTabLayout.axml#L13.
[2] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#identifiers
[3] https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#keywords
@aherrick
Copy link

aherrick commented May 3, 2019

Just a heads up I'm still seeing this issue on the latest version of VS2019 4.7.03190

Deleting the Resource Designer fixes temporarily, then rebuilding brings it right back.

@dellis1972
Copy link
Contributor

@aherrick you will still see it in the stable release since we just merged this into master, and cherry-picked it to d16-2 (which is the next preview).

@aherrick
Copy link

aherrick commented May 4, 2019

@dellis1972 thanks for the update... will keep an eye out.

@aherrick
Copy link

FYI this still isn't fixed. VS 2019 16.1.0

@dellis1972
Copy link
Contributor

@aherrick the fix was cherry-picked to the 16.2 branch , it should be in Preview 2. I don't think we can back port this to 16.1 . Note the work around for 16.1 is to add

<AndroidUseManagedDesignTimeResourceGenerator>False</AndroidUseManagedDesignTimeResourceGenerator>

to your csproj PropertyGroup

@aherrick
Copy link

aherrick commented Jun 4, 2019

Thanks! The work around solved this for me.

@jgold6
Copy link

jgold6 commented Jun 28, 2019

Can this fix be released for S 2017 as well?

@candidodmv
Copy link

candidodmv commented Jul 5, 2019

as said above by @jgold6 , it's very important support vs2017!

@dellis1972
Copy link
Contributor

@jgold6 @candidodmv does the work around not work for VS2017?

@jgold6
Copy link

jgold6 commented Jul 8, 2019

The work around does work for VS 2017. As such I have already been informed by @bholmes that the fix for this will not be backported to VS 2017.

@devopsone
Copy link

I have 3 computers with the same setup and this only happens on one machine. Anybody knows why? I have to fix it with the workaround but wonder whats different to the other installations. They are totally same, even packages, updates.

@dev-thinks
Copy link

I am facing this issue still exists in latest VS 2019 version. and works with work around changes in project file.

Is this issue addressed already as it is in closed state now?

Tx

@dellis1972
Copy link
Contributor

@dev-thinks The fix was shipped, you might have hit a different corner case. Can you open a new issue and attach a repo project and/or diagnostic build logs so we can investigate.

@xamarin xamarin locked as resolved and limited conversation to collaborators Jun 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: App+Library Build Issues when building Library projects or Application projects. Area: Design-Time Build Issues with VS design-time build.
Projects
None yet
Development

Successfully merging a pull request may close this issue.