Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Xamarin allow certain file names on Android?

I noticed that if you have an image file (png or svg and possibly non-image files too) and wanted to rename it to default.png, public.svg, etc., then Android will throw a compiler error with Invalid symbol: [file name]. I've read somewhere that this is because these are C# reserved keywords. What I don't understand is why these keywords have anything to do with file names and why they only seem to be causing an issue on Android but not other platforms. Is this a bug or by design?

like image 889
user246392 Avatar asked Dec 17 '25 08:12

user246392


1 Answers

It is "by design"...

In the build process, first a tool called Android Asset Packaging Tool (aapt and aapt2) is run that produces a Java file called R.java that maps resource names to int values. (Note: aapt|2 is a Google Android tool, not a MSFT/Xamarin one)

So a drawable named public.png would generate something like the following in the R.java file:

public static int public=0x7f020023;

And these values are also mapped into the C# world by Xamarin via the auto-generated Resource.designer.cs file, and thus

public const int public = 2130837539;

And the use of these language keywords as variable names in the Java/C# files will cause compilation errors.

like image 83
SushiHangover Avatar answered Dec 19 '25 23:12

SushiHangover



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!