Home » Android » Why are Android layout file names so limited?

Why are Android layout file names so limited?

Posted by: admin June 15, 2020 Leave a comment

Questions:

It’s good to have consistency in file names.

MyActivity.java contains the public class MyActivity

I would like the xml file with its layout to be called res/layout/MyActivity.xml

But I get an error message saying “Invalid file name: must contain only [a-z0-9_.]”

So two questions:

  1. Why is the character set so limited (not even upper case? Come on!) – Ah – this restriction is probably in place so you will never be screwed by filesystems that don’t make a distinction between upper and lower case, like Apple’s HFS+ (although see Wikipedia for the gory story http://en.wikipedia.org/wiki/Comparison_of_file_systems#cite_note-note-35-77 )

  2. Which filenames are restricted – all of res? just res/layout? res/layout plus some other folders?

Can anyone confirm 1, and give details on 2?

Thanks,

Peter

How to&Answers:

Why is the character set so limited

Because the name has to be a valid Java identifier, so you can use constants like R.layout.foo to identify the layout at runtime.

Which filenames are restricted – all
of res? just res/layout? res/layout
plus some other folders?

Anything in res/

Answer:

Not sure of the reason for #1. Never seen an explanation in any readings about Resources. For #2 from my experience anything that will be used as a id in java e.g., R.drawable.marker, R.string.default_message has to follow those rules of [a-z0-9_].

Answer:

When using MacOS X as a development platform, almost any developer dealing with cross platform code will sooner or later run into the issue that source code from a Linux/UNIX project cannot be built after download/checkout/clone because the project has two identically named files in a single directory that only vary in case; and on OS X that means you end up with just one file as the second one will overwrite the first one.

Of course HFS+ can be case-sensitive, but this can only be configured when you format a partition and by default all Macs come with preinstalled OS X on a preformatted partition with a case-insensitive HFS+ (as that is the default mode for HFS+). So you would first have to reformat your newly bought Mac and reinstall OS X to achieve that goal. And then be prepared for trouble as many apps rely on case-insensitivity on the Mac; a very famous example is Steam (you cannot run Steam from a case-sensitive HFS+).

And it’s not just MacOS. FAT is case-insensitive and while NTFS can be case-sensitive (just like HFS+), it isn’t by default. Also I think SMB isn’t as per protocol spec (at least older versions). The filesystem used on CDs isn’t, and so on.

So I guess to avoid any troubles straight from the beginning, Google thought it’s a good idea to force files to be all lower case, in which case it doesn’t matter what filesystem you are using or how it is configured. Of course, that’s a bit stupid if you can still run into that issue with your source code files, however that these may be mixed case is a decision that predates Android and that has been made by Sun many years before Google even dreamed of that system.