简体   繁体   English

为R类中的相同资源生成不同的ID

[英]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. 当编译具有一个或多个apk库的项目时, aapt工具会生成多个R.java。 One for each library and one for the application itself. 每个库一个,一个用于应用程序本身。

Each of those R files would define the same IDs. 这些R文件中的每一个都将定义相同的ID。 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. 现在,相同的资源有两个不同的ID。

target/generated-sources/r/com/viewpagerindicator/R.java: 目标/生成来源/ R / COM / viewpagerindicator / R.java:

public static int default_line_indicator_selected_color=0x7f04000b;

target/generated-sources/r/net/sourceforge/uiq3/fx602p/R.java: 目标/生成来源/ R /净/ 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. 在那里我注意到除了final的R.java文件应该100%相同。 Especially: each R.java file should define all IDs — Even the IDs which are not part of a library. 特别是:每个R.java文件都应该定义所有ID - 甚至是不属于库的ID。

This too is not the case in my troublesome project. 在我麻烦的项目中也不是这样。 Each library R.java only defines the IDs used by the library. 每个库R.java仅定义库使用的ID。

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. 我想知道为什么aapt被调用几次,当一个带有搜索和替换的副本也可以做到这一点。 And note that i don't remove the final as well. 请注意,我也不会删除final

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 . 我认为这解释了为什么库中的R id不是final

All your references to default_line_indicator_selected_color will use the new value 0x7f07000b . 您对default_line_indicator_selected_color所有引用都将使用新值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. 当您构建依赖于库项目的应用程序时,SDK工具会将库编译为临时JAR文件并在主项目中使用它,然后使用结果生成.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. 如果在应用程序和库中都定义了资源ID,则这些工具可确保应用程序中声明的资源获得优先级,并且库项目中的资源不会编译到应用程序.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 http://developer.android.com/tools/projects/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM