I uploaded local photos in drawable files. The photos are approximately 3 mb in size. Its showing this error. However The problem panel shows analizing for 15 minutes till now and it is still showing it. What could be the possible reason for such errors.
Resource name must start with a small case letter or an underscore('_').
For more rules regarding resource naming convention, you can refer to the below-mentioned medium article.
https://medium.com/#AkhilDad/a-designers-guide-for-naming-android-assets-f790359d11e5
There are a few conventions used in resources:
For resources that exist as separate files, they must be
lower_case_underscore_separated. The appt tool makes sure that your
files are only lower-case, because using mixed case can cause issues
on case-insensitive filesystems.
For resources declared only in values/... (attributes, strings, etc)
the convention is generally mixedCase.
There is a convention used sometimes to tag names with a
"classification" to have simple namespaces. This is for example
where you see things like layout_width and layout_alignLeft. In a
layout file the attributes for both the View and the parent layout
management are mixed together, even though they are different
owners. The "layout_*" convention ensures that there are no
conflicts between these names and it is easy to understand which
entity the name impacts.
For more information here is the complete discussion.
Are there conventions on how to name resources?
Resource name must start with letter. You can't start image name with digits.
The reason you can't have a resource with a numeric name is because variable names cannot start with numbers.
The error is actually because the resource name cannot start with a number. Resource name should start with a letter or underscore(_).
Related
I have a small confusion, when in drawable folder in android i try to copy same file it says that "Resource with this name already exists" which is absolutely correct. Again if try to copy an image with different .extension but with same file name it takes it. But in R.java file only 1 resource id is generated. What does this mean? Also ID will point to which resource?
It simply means that at the time of compilation it is only able to generate id from one of the available resource. Why can't it generate both id's if it has allowed to keep both resources in the same folder? Because for each resource, there is a static integer named after the name of file, excluding extension so naturally, there can't be more than one of these static integers in one file.
ID points to which resource? I think you can't be sure. I just checked by recreating the scenario (out of curiosity) and I found that it pointed to the resource I added in the end!
Still, it is irrelevant as to which resource it points to. Because it won't allow to run the app.
P.S: If you really want to use resources with same name (diff extension) then you can do so by using assets
I have two values directories - one for English and one default.
if the resource are located in any of those folders, i can use it.
But if there is no resource in default values directory and locale is other than English, it causes ResourceNotFoundException.
I understand why this happens and why Android was built this way (to prevent ambiguous resource usage in case there are more than one non-default resource files with this resource).
But is there any way to force Android use, for example, English resource bundle if resource couldn't be found?
The only option you have is to make sure the /res/values/strings.xml file contains every string in English but you must also have /res/values-no/strings.xml which has the Norwegian strings.
If /res/values-no/strings.xml doesn't have the resource it will drop back to /res/values/strings.xml. It's the only way to do it.
Is there a way to tag a strings resource folder with more than one language(values-en-es)?
My problem is that for Hebrew on some devices the language code "iw" and on others it is "he".
My current solution is to make two folders with the same content and only change their name
respectively.
I wonder if there is a more accurate way to do it?
Resource folder names can have multiple qualifiers but only one qualifier per type:
For example
values-en-rGB //Language + Region
is valid but
values-en-fr//Language + Language
is not valid, since it has multiple values for a single qualifier. So
values-iw-he
is not possible.
Source: Android Developers, Qualifier Name Rules.
However this doesn't mean you have to duplicate the files. Instead, you can create an Alias Resource.
Android Developers explains Alias Resouces like this:
Creating Alias Resources: When you have a resource that you'd like to use for more than one device configuration (but do not want to provide as a default resource), you do not need to put the same resource in more than one alternative resource directory. Instead, you can (in some cases) create an alternative resource that acts as an alias for a resource saved in your default resource directory.
For example, a String resource in one folder
<string name="app_name">My Awesome App</string>
can be referenced in another String resource in another folder as:
<string name="application_name">#string/app_name</string>
More about alias-resources on Android Developers.
You can make a File Link in eclipse, as described here.
So you have your values-iw/strings.xml with the real values and you make a File Link to that file in your values-he folder. This has the benefit that you do not have to edit 2 files, the linked 'file' gets updated automatically.
It is magical for Android to locate the proper resource just through the R.id.XXX.
AFAIK, the resources are compiled to binary format, so how does this mapping logic work under the hood?
Maybe it works like this:
For e.g., in the layout1.xml, we got:
<Button android:id="#+id/button1" >
and AAPT will generate this in the R.java:
public static final int button1=0x7f05000b;
When the *.apk is genrated, the #+id/button1 with be substituded with "0x7f05000b".
Thus, when we call:
findViewById(R.id.button1);
we are essentially still do the search based on the ID, though the ID is a number like 0x7f05000b.
Thanks!
ADD
What I really want to know, is how the resource id integer is parsed into the resource content? In other words, how does the Android runtime locate the resource content with resource id as the sole clue?
For example, how is a drawable picture found with a resource id? Or how is a string value is found with a resource id?
At build time, the aapt tool collects all of the resources you have defined (though separate files or explicit definitions in files) and assigns resource IDs to them.
A resource ID is a 32 bit number of the form: PPTTNNNN. PP is the package the resource is for; TT is the type of the resource; NNNN is the name of the resource in that type. For applications resources, PP is always 0x7f.
The TT and NNNN values are assigned by aapt arbitrarily -- basically for each new type the next available number is assigned and used (starting with 1); likewise for each new name in a type, the next available number is assigned and used (starting with 1).
So if we have these resource files handled by aapt in this order:
layout/main.xml
drawable/icon.xml
layout/listitem.xml
The first type we see is "layout" so that is given TT == 1. The first name under that type is "main" so that is given NNNN == 1. The final resource ID is 0x7f010001.
Next we see "drawable" so that is given TT == 2. The first name for that type is "icon" so that gets NNNN == 1. The final resource ID is 0x7f020001.
Last we see another "layout" which has TT == 1 as before. This has a new name "listitem" so that gets the next value NNNN == 2. The final resource ID is 0x7f010002.
Note that aapt by default makes no attempt to keep these identifiers the same between builds. Each time the resources change, they can all get new identifiers. Each time they are built, a new R.java is created with the current identifiers so your code gets the correct values. Because of this, you must never persist resource identifiers anywhere where they can be used across different builds of your app.
Once the resources are compiled and identifiers assigned, aapt generates the R.java file for your source code and a binary file called "resources.arsc" that contains all of the resource names, identifiers, and values (for resources that come from separate file, their value is the path to that file in the .apk), in a format that can easily mmapped and parsed on the device at runtime.
You can get a summary of the resources.arsc file in an apk with the command "aapt dump resources <path-to-apk>".
The format of the binary resource table is documented in the header file for the resource data structures here:
https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/include/androidfw/ResourceTypes.h
The full implementation for reading the resource table on the device is here:
https://github.com/android/platform_frameworks_base/blob/master/libs/androidfw/ResourceTypes.cpp
If you are interested in the internal implementation (device side) have a look at loadDrawable() in Resources.java. Refer to hackbod's excellent answer for information about extracting data from the resource table
To know how layouts are translated into View's from resource ID's check out LayoutInfater.java
From what I understand, aapt will auto-generate unique IDs for each of your resources and store them in a look-up table. This look-up table is persisted as the "resources.arsc" file located in "bin/resources.ap_" (this is just a ZIP file, so feel free to open using your favorite ZIP viewer). The look-up table is also persisted as R.java, which as you know allows you to reference your resources in Java.
If you want more information on the ARSC file, I would suggest Googling it, or reviewing the code of http://code.google.com/p/android-apktool/.
-Dan
One final note: for the longest time, I didn't use relative layouts because many items need to reference items further down in the xml file, and I didn't know how to reference an #id/foo that hadn't been defined yet.
<!-- doesn't work -->
<TextView android:layout_above="#id/foo">above</textview>
<TextView android:id="#+id/foo">below</textview>
Then one day I realized (duh) that you can define an id in the reference; it doesn't have to be in the element that bears the id:
<!-- works -->
<TextView android:layout_above="#+id/foo">above</textview>
<TextView android:id="#id/foo">below</textview>
The magic is in the Eclipse plug-in and the R.java file it autogenerates in an app's "gen" folder. If you peek into this file, you'll see static mappings for every XXX in R.xx.XXX where xx can be anim, array, color, and every other resource type.
I wonder if there is a way to read out the locale values of all existing values directories.
Let's say I've got the following directories under my res-Directory
[...]
values
values-de
values-nl
[...]
Now I need a method to get back the information that there is a locale of de and nl existing for the directory values.
Is there any way, if yes how?
Any help will be appreciated.
Regards,
Christian
Well, ideally, your app neither knows nor cares what resource sets you have. That is the whole point behind resource sets, after all -- to insulate your app from changes in resources.
That being said, one possibility is to write a script that is part of your build process that generates a file with your requested data (e.g., XML file containing the roster of resource sets) that you then read in at runtime.
Or, arrange to have a "magic value" in each set. For example, in res/values-de/strings.xml, you could have a lang_de string, and in res/values-nl/strings.xml, you could have a lang_nl string. Then, you can use reflection to iterate over your string resources and find those matching the lang_ pattern. This may be significantly slower than the first option, particularly if you have lots of string resources.
I know of no way to interrogate the system to find out what resource sets are defined.