question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filename casing and pattern issues with Android

See original GitHub issue

I had image filenames like layout-StackLayout.png which generated Android resources without error, however nothing displayed in my app for those. If I remove the dash and rename it all lowercase, it works.

My initial reaction is to throw errors and warn the dev of appropriate conventions, however that feels against he value prop of a xplat solution which should handle such annoyances elegantly. We ought to do the right thing for the platform, but I wonder how that’ll bite us later. 😃

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Redthcommented, Feb 16, 2021

@davidortinau so this is a tough one to handle elegantly given the restrictions android places on this. We ultimately generate resource drawables, and that pretty much limits us to lowercase alphanumeric characters, no number for the first character, and underscores allowed.

We could try and rename files as we generate them to follow the naming rules, but there are some problems with this:

  1. We’d need to also check and change the filenames referenced at runtime (in the image Source for MAUI, for example, if I put Source="The-Cat.png" we need to map that to the_cat.png instead). Keep in mind, this is just for MAUI, and things like Comet, etc will have to do their own handling as well. Plus any other custom code that somehow works around the touch points we use, would need to account for this.
  2. Debugging these ‘automagic’ transformations can be confusing. Things like inspecting an APK or looking in your obj/ folders you’ll see the transformed filename which in some cases might be frustrating.
  3. We can run into collisions more easily. Let’s say I have the-cat.png and the_cat.png, when we fix the first filename to use _ instead of -, we now have two files named the same. This is a simple case and we could add more logic to look for collisions and keep changing it until we’re good, but it complicates the code, and validation of it both in fixing the filename at build/generation time, and trying to find the right match at runtime.
  4. On Android we do something like this today and it (still afaik) results in the build tasks doing multiple passes over resources since you can reference the invalid name in your other resources - this has been a pain point for awhile and an area that adds a bit of build time - enough that it’s been reviewed/debated a couple times when looking for perf improvements.

I’d like to provide a frustration free experience, but it feels like the better path here might be to go and fail fast letting developers know right away that their filename is invalid. It’s a pretty quick fix if we provide a detailed error.

0reactions
valdeterocommented, Jul 26, 2021

I agree with @srqdev that it should automatically change the name to lowercase since that doesn’t change the filename itself. However, I think that if it can’t create the files because of a restriction, then it should fail. Resizetizer has one job and if it can’t do it successfully then it should stop and inform the developer. I would rather a build error telling me that “the-cat” is invalid than it silently failing and the image just not showing up. I may have made other code changes at the same time and not even consider that Resizetizer is having an issue.

Handling all of the annoyances gracefully is wonderful but all of the complications that Redth mentioned makes it not worth the trouble if you can just tell the consumer “file xyz’s naming convention does not meet android restrictions. Go here to see what they are:”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

File's method renameTo() returns true but does not actually ...
File's method renameTo() returns true but does not actually rename the file if I change filename's lower/upper cases ; "SubFolder1"; String ...
Read more >
Android storage use cases and best practices
To help you migrate your app to support scoped storage, follow the best practices for common storage use cases that are outlined in...
Read more >
Android File Search does not work or could not be ...
The Android App file search does not work. ... That is why it is disabled and the default search pattern “uses” start with...
Read more >
Validate String as Filename in Java
In this tutorial, we'll discuss different ways to validate if a given String has a valid filename for the OS, using Java.
Read more >
java.io.FileWriter not case sensitive for file names?
Hey, I'm on Mac OS X 10.7.2 with Java SE 6 and I noticed that the FileWriter class doesn't seem to be case...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found