Android如何处理多个R.java? [英] How Android handle multiple R.java?

查看:185
本文介绍了Android如何处理多个R.java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个项目制作几个Android库应用程序.为了简化问题,假设我在该项目中有两个库( utilLib screenLib )(现在称为 app ).

I'm making couple of Android library apps for a project. To simplify the question, let's say I have two libraries(utilLib, screenLib) in this project(now will be referred to as app).

每个项目中都有一个名称相同但值不同的String资源.像这样:

There's String resource with the same name inside each project but with different values. Like this:

utilLib

<string name="app_version">1.0</string>
<string name="hello">UtilLib Hello</string>

screenLib

<string name="app_version">0.7a</string>
<string name="hello">ScreenLib Hello</string>

应用

<string name="app_version">0.1</string>

我意识到我可以使用 com.package.R 引用该字符串,但是如果我的代码看起来像这样,会显示什么呢?

I realized that I can refer to the string using com.package.R but what if my code looks like this what will show up?

<!-- language: java -->
import com.app.R; 
...

private void checkValue(){
    String version = getString(R.app_version);
    Log.d(TAG, "version: " + version); // 0.1 show be here
    String hello = getString(R.hello);
    Log.d(TAG, "Hello: " + hello); // <---- ? ('UtilLib Hello' or 'ScreenLib Hello')
}

我试图在此处进行模块化构建,但不完全了解Android如何确定其R.java使用的优先级.有人遇到过吗?

I am trying to make modular build here but don't fully understand how Android prioritize its R.java to use. Has anyone had experienced with this?

推荐答案

Log.d(TAG, "version: " + version); // 0.1 show be here

官方开发指南管理项目-图书馆项目:

在构建依赖于库项目的应用程序时,SDK工具会将库编译为一个临时JAR文件,并在主项目中使用它,然后使用结果生成.apk.如果在应用程序和库中都定义了资源ID,则这些工具可确保在应用程序中声明的资源具有优先权,并且确保库项目中的资源不会编译到应用程序.apk中.这使您的应用程序可以灵活地使用或重新定义任何库中定义的任何资源行为或值.

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.


Log.d(TAG, "Hello: " + hello); // <---- ? ('UtilLib Hello' or 'ScreenLib Hello')

这取决于图书馆项目的优先级.

引用官方开发指南管理项目-库项目 :

  • 资源冲突

  • Resource conflicts

由于这些工具将库项目的资源与从属应用程序项目的资源合并,因此可以在两个项目中定义给定的资源ID.在这种情况下,工具会从应用程序或具有最高优先级的库中选择资源,然后丢弃其他资源.在开发应用程序时,请注意,公共资源ID可能会在多个项目中定义,并且会被合并,其中来自应用程序或最高优先级库的资源优先.

Since the tools merge the resources of a library project with those of a dependent application project, a given resource ID might be defined in both projects. In this case, the tools select the resource from the application, or the library with highest priority, and discard the other resource. As you develop your applications, be aware that common resource IDs are likely to be defined in more than one project and will be merged, with the resource from the application or highest-priority library taking precedence.

引自官方开发指南使用ADT从Eclipse中引用-引用图书馆项目:

如果要添加对多个库的引用,请注意,可以通过选择一个库并使用向上"和向下"控件来设置它们的相对优先级(和合并顺序).这些工具从最低优先级(列表的底部)到最高优先级(列表的顶部)开始将引用的库与您的应用程序合并.如果多个库定义了相同的资源ID,则工具从库中选择优先级更高的资源.该应用程序本身具有最高优先级,其资源始终优先于库中定义的相同资源ID使用.

If you are adding references to multiple libraries, note that you can set their relative priority (and merge order) by selecting a library and using the Up and Down controls. The tools merge the referenced libraries with your application starting from lowest priority (bottom of the list) to highest (top of the list). If more than one library defines the same resource ID, the tools select the resource from the library with higher priority. The application itself has highest priority and its resources are always used in preference to identical resource IDs defined in libraries.

引自官方开发指南从命令行-引用图书馆计划:

如果要将引用添加到多个库,请注意,可以通过手动编辑project.properties文件并适当调整每个引用的.n索引来设置它们的相对优先级(和合并顺序).例如,假定这些引用:

If you are adding references to multiple libraries, note that you can set their relative priority (and merge order) by manually editing the project.properties file and adjusting the each reference's .n index as appropriate. For example, assume these references:

 android.library.reference.1=path/to/library_projectA
 android.library.reference.2=path/to/library_projectB
 android.library.reference.3=path/to/library_projectC

您可以通过以下方式对引用进行重新排序,以使library_projectC拥有最高优先级:

You can reorder the references to give highest priority to library_projectC in this way:

 android.library.reference.2=path/to/library_projectA
 android.library.reference.3=path/to/library_projectB
 android.library.reference.1=path/to/library_projectC

我找不到比官方开发指南更清楚地解释这一点的单词.

I can't find any word that can explain this more clear than official dev guide.

这篇关于Android如何处理多个R.java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆