Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different IDs are generated for the same resource in R classes

When a project with one of more apk libs is compiled, then more then one R.java is generated by the aapt tool. One for each library and one for the application itself.

Each of those R files would define the same IDs. This worked without problems for quite a while now. Years in fact. But suddenly not any more. Now the same resource has two different IDs.

target/generated-sources/r/com/viewpagerindicator/R.java:

public static int default_line_indicator_selected_color=0x7f04000b;

target/generated-sources/r/net/sourceforge/uiq3/fx602p/R.java:

public static final int default_line_indicator_selected_color=0x7f07000b;

Has anybody got an idea what might have gone wrong?

Update:

I double checked with other projects. There I noted that apart from the final the R.java files should be 100% identical. Especially: each R.java file should define all IDs — Even the IDs which are not part of a library.

This too is not the case in my troublesome project. Each library R.java only defines the IDs used by the library.

Quick and Dirty Fix

Until i find out what the real problem I use the following ant task as a quick and dirty fix:

            <copy
              encoding='${project.build.sourceEncoding}'
              file='target/generated-sources/r/net/sourceforge/uiq3/fx602p/R.java'
              overwrite='true'
              toFile='target/generated-sources/r/com/viewpagerindicator/R.java'
            >
              <filterchain>
                <tokenfilter>
                  <replacestring
                    from='net.sourceforge.uiq3.fx602p'
                    to='com.viewpagerindicator'
                  ></replacestring>
                </tokenfilter>
              </filterchain>
            </copy>

I wonder why aapt is called several times when a copy with search and replace can do the trick as well. And note that i don't remove the final as well.

like image 887
Martin Avatar asked Nov 10 '22 12:11

Martin


1 Answers

When you assemble the project, the library ressource are overwritten by the main project. I think this explain why the R ids in library aren't final.

All your references to default_line_indicator_selected_color will use the new value 0x7f07000b.

In what situation do you have problems with this?

When you build an application that depends on a library project, the SDK tools compile the library into a temporary JAR file and uses it in the main project, then uses the result to generate the .apk. In cases where a resource ID is defined in both the application and the library, the tools ensure that the resource declared in the application gets priority and that the resource in the library project is not compiled into the application .apk. This gives your application the flexibility to either use or redefine any resource behaviors or values that are defined in any library.

http://developer.android.com/tools/projects/index.html

like image 164
Kevin Robatel Avatar answered Nov 15 '22 11:11

Kevin Robatel