Android: How to organize project resources? - android

I'm wondering what some good ways of organizing Android projects are. I'm building a little recipe application and have already made dozens of layouts, drawable resources, menus, etc., not to mention Java source code.
Android does not allow sub-folders for resources, so is there a way to organize them? Right now I'm trying to organize them through naming conventions (e.g. new_list_ingredient_edit) but I'm wondering if there is a better way, as the names will probably get pretty long and cumbersome.
Same thing with resources such as string values. As I understand it, I can create multiple resource files (e.g. strings_new_ingredient.xml, drawables_new_instructions.xml, etc.) but all my resources across files have still have to have unique names, which again is cumbersome.

Although android doesn't allow sub-folders for resources, it does for assets, but this is much more cumbersome to use.
A possible option would be to use a sql database with all of your strings, but this approach is probably too much work and more cumbersome anyway.
Your naming convention method is probably your best option. You could think of some of the seperated words as directories if you want, which should keep it organized enough.

user864684,
I normally try to use heirarchy in my naming. If it is a layout for dialog, i will start with dialog_sharing or something like that.
As for graphics, I will start with btn or bg or txt or ic_menu depending on what they are for. Other than that, you just get used to it as you dev more for Android.
Make sure you also have a support folder on your drive for your local resources. I mimic the android layout so i keep my graphics sorted in res/... on my local drive too. I keep the psd and graphic files there and then just copy the pngs to eclipse.
Hope this kinda of helps. There is no real standard but you will pick up a style.

Specifically with respect to organizing drawable resources, I would advise choosing names that describe the drawables structurally, not functionally.
For example, if you have a gray circle shape resource that you are using as a placeholder for an image, I would avoid naming it *image_placeholder*. Instead, I would call it *gray_circle_1*.
My advice comes from having had the experience on multiple occasions of having given things names like *image_placeholder*, forgetting what they actually contained, and then rewriting the same exact thing in a separate resource file without realizing it.
Don't do what I did.

Related

android - automatically detect unused drawables

The matter: I used many icons and pictures while developing an android application, later I replaced many of them but kept the old ones in case I would need to use them again. Now I have a huge amount of .png's in my drawable folder, many are now unused and it would take ages to manually sort them out. Is there a way to detect drawables to which no references exist?
You can use Android Lint Tool, follow the link I provide , as an overview it states:
Missing translations (and unused translations)
Layout performance problems (all the issues the old layoutopt tool used to find, and more)
Unused resources
etc.
As an alternative this project might help you (pretty easy to use):
https://code.google.com/p/android-unused-resources/
Hope it's useful...
In my experience, Lint does not detect unreferenced PNGs. It finds unreferenced layouts and drawable XML files like shapes and selectors but not PNGs. I stumbled upon this Python script for removing unreferenced PNG drawables. It worked well with my project but, as the documentation for it says, make sure you are using source control just in case.
https://github.com/instructure/android-ImageSweep

Using large text files in different languages in Android

In the app I'm building, I'm using multiple languages. It's easy to add a different language into an app by adding a new folder (for example: values-fr) and adding a new strings.xml file in there. But I have pretty large text files (complete articles) that I need to add. These articles are also written in different languages. What is the best way to add them to my app?
I'd consider using res/raw-<qualifiers> as alternative to the assets. The raw folder can store arbitrary files in their - you guessed it - raw form. For example, a 'Hello World' article written in French and English, would be stored under:
res/raw-fr
res/raw-en
The raw resource can then be opened by calling openRawResource(int id) on a resources object, similar to how it works for other resources like drawables, strings etc. The id's generated by the framework will be in the familiar format of R.raw.filename (without file extension).
The benefit of using this approach is that you can fully leverage Android's localization system, meaning that as a developer you basically don't have to worry about any of that at all. You can also easily add more qualifiers to further filter on device characteristics (e.g. screen size, density, platform version etc etc - see here for a full overview). The downside is that it imposes some limitations in terms of the original file name/extension and doesn't support a proper folder/file hierarchy.
The 'better' approach (/raw vs /assets that is) will probably depend on your project's requirements.
I would probably use assets -- that is, create assets/data/fr/ and store the fr files there. Note that assets require explicit extraction -- which probably is good since you may save memory having only one set of articles installed.
Another possibility is to place everything on an http server, and thus make both keeping and accessing the articles somebody else's problem :) .
BTW, if you files are really big, you will have to install the application without them, and download the articles later. (There are restrictions on the apk size.)

Android R.java - "overloading" values

I've searched for information about this but it's hard when I don't really know what to search for.
I'm working on a few android projects which share a common base (that I put in a few libs). The libs works with both classes and xml-files, and some other resources too. The most common is that I have a drawable xml which gets color information from an xml-file which defines which colors to use, called something like "foobar_colors.xml" or so. The "foobar_colors.xml" in the lib itself defines default colors.
When I use the lib in the projects these colors needs to change (to use the client company colors or so), and I'm not sure how the best way is to accomplish this... The libs uses allot of resorces, their own xml-layouts and such, so I really can't always "declare things stylable".
The way I do it now is include the "foobar" lib in my project, copy the "foobar_colors.xml" to my project resources and change their values there. This works since R.java seems to "overload" values like the lib itself sets "foobar_color1" to be black and the project sets it again to be red (and the projects defines has higher priority).
Now I'm just wondering, is this safe? Is there any better way to do it? Possible problems that might come up in the future?

Is it possible to make use of the Android resource resolver on a self-created folder in the file system?

I'd like the ability to "overwrite" the Android resources packaged within my apk by having the app periodically download a zipped file containing overrides that follow the same naming convention as the source does. For example, the zip might consist of the following paths:
res/values/strings.json
res/values-land/strings.json
My code would parse those files and produce a Map> that would map the string resource id to a folder->value pair (or something along these lines). At this point I'm really only concerned with strings and images (maybe arrays), but not layouts, etc.
To the point: Is there any method available, that, given a list of folder names, would tell me which one the Android resolver would choose based on current state? I'm assuming this is difficult because the compiler breaks everything down to ids, but figured it was worth a shot. Any ideas?
Is there any method available, that, given a list of folder names, would tell me which one the Android resolver would choose based on current state?
No. You are welcome to roll this yourself based on Configuration, DeviceMetrics, and kin. You will also need to create your own parsers for your own files, as Android's resource system only works with resources, not arbitrary files retrieved from arbitrary locations.
The expectation in Android is that if you want to update resources, you update the app, probably because there were also code changes as well. Admittedly, this approach has its limitations.

determining if resources such as those in strings.xml are no longer used

I have done some significant re-coding on one of my Android programs and now I am unsure if certain xml strings are used anymore. In addition I have a few translations which makes the task even more difficult. Is there a tool to test this? This would be useful for drawables also.
I am using the eclipse plugin.
This question has been discussed in the irc channel before. There is no tool to test it, but I agree it would be useful. Note that resources can be referenced in xml, but they can also be referenced from code. Furthermore, resources can also be looked up by their identifier, and such lookup could be determined by runtime.
So actually you cannot determine 100% whether a resource is used or not anymore, but you can probably determine which resources are referenced in a static way (in xml or code). Depending on your code/app which you know best yourself, such approach might be sufficient in many cases.
The approach would be to write a tool that parses xml and java source files and also take the import statements into consideration. With that information you should be able to determine which resources you can get rid of.
The easiest way is to remove them all, attempt to compile, and re-add those the compiler says are lacking. It's a little tiresome, but it's certainly tractable.
Note, as Mathias already pointed out, that it's technically possible to access resources by name with a string at runtime, and the way I suggest here would remove such resources though they are, in fact, needed. However, this pattern should be really rarely seen in any application, and if you are the one who wrote it, you already know if/where you do such treatment.
Use grep to extract a list of resources to a file by way of sort
Use recursive grep through sort and uniq to create a list of those mentioned in any source file (make a copy of project without unused files or dispatch grep on a list of used ones, of course commented out code will be an issue)
Use diff on the two lists

Categories

Resources