Key clash with Android resources

I have two Android projects, one of which (package name com.adip.sampler ) and a library added to main (package name com.samples.projb ). In both of them in resources I have an integer-array with the same key: my_int_values :

In the main project:

 <integer-array name="my_int_values"> <item>10</item> <item>20</item> <item>30</item> <item>40</item> <item>50</item> <item>60</item> <item>70</item> <item>80</item> </integer-array> 

in library:

 <integer-array name="my_int_values"> <item>34</item> <item>35</item> <item>36</item> <item>37</item> </integer-array> 

In the main project from action, if I examine which values ​​from these arrays (both the main project and the library):

 protected void showLocalStrings() { Log.d("RESSampler", "In Main: " + Arrays.toString(getResources().getIntArray(com.adip.sampler.R.array.my_int_values))); Log.d("RESSampler", "In Libr: " + Arrays.toString(getResources().getIntArray(com.samples.projb.R.array.my_int_values))); } 

then I see this in Logcat:

 In Main: [10, 20, 30, 40, 50, 60, 70, 80] In Libr: [10, 20, 30, 40, 50, 60, 70, 80] 

It seems that the main project is overriding the values ​​defined in the library array ... I double-checked whether I was reading resources with the correct key, and that is fine. Until I looked in every generated class R In the main project, this is what I have for com.adip.sampler.R.array.my_int_values :

 public static final class array { public static final int my_int_values=0x7f060000; } 

and in the library project com.samples.projb.R.array.my_int_values :

 public static final class array { public static final int my_int_values = 0x7f060000; } 

The Android tool generated the same value, so it is not surprising that I get this behavior. I can get rid of this behavior if I changed the key from one of the integer arrays, but imagine that you have several large projects with a lot of resources, dependency libraries, and sooner or later you may encounter this problem: have the same type of resources with the same key value (I checked with string and with string-array and above, it also appears there). So the questions will be as follows:

  • Why does this problem appear? Or, if this is not a problem, explains this behavior?
  • How to avoid this best? I assume that trying to have some kind of uniqueness in defining keys will do the trick, but developers are usually lazy ...

Several versions of the latest ADT and Eclipse (Juno and Indigo) appear. Tested only on Windows.

+6
source share
2 answers

Reading from Library projects in Android Developers , there are many links where they clearly say that merging occurs during assembly, and resources with the same identifiers overwrite each other.

For resources with the same identifier from the library and application

In cases where the resource identifier is defined both in the application and in the library, the tools ensure that the resource declared in the application takes priority and that the resource in the library project is not compiled into the .apk application . This gives your application the flexibility to use or override any resource behavior or values ​​defined in any library.

For resources with the same identifier from two libraries

... your application can add links to several library projects, then indicate the relative priority of resources in each library. This allows you to create the resources actually used in your application in a cumulative manner. When two libraries refer to the application to determine the same resource identifier, the tools select the resource from the library with a higher priority and discard the other.

Solution offered in documentation

Use prefixes to avoid resource conflicts

To avoid resource conflicts for common resource identifiers, consider using a prefix or other sequential naming scheme that is unique to the project (or unique to all projects).

How to prioritize libraries from the command line

If you add links to several libraries, note that you can set their relative priority (and merge order) by manually editing project.properties and editing each .n link index as needed.

+8
source

Personally, I would change the names of the resources. If you read anything about naming conventions, this should be something meaningful, and "my_int_array" is not very useful, especially in a library that other people or the project could potentially use.

Ideally, you want to be able to forget about it for 6 months, go back and look at it and find out what this array for / contains, without having to read the code to determine why this array is with it.

This post; fooobar.com/questions/60873 / ... contains several different answers to naming conventions.

Finally, not sure of the conflict, he could not throw something away. I suppose this is due to how it is automatically generated, it might be worth writing it down as an error and see if you get a response from the development team.

+1
source

Source: https://habr.com/ru/post/955717/


All Articles