android - Android如何处理多个R.java?

标签 android modularity android-library r.java-file

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

每个项目中都有同名但值不同的字符串资源。像这样:

实用程序库

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

屏幕库

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

应用

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

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

<!-- 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。有没有人经历过这个?

最佳答案

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

引用自官方开发指南的原因 Managing Projects - Library Projects :

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')

这由图书馆项目优先级决定。

引自官方开发指南Managing Project -Library Projects :

  • Resource conflicts

    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.

引自官方开发指南From Eclipse with ADT - Referencing a library project :

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.

引自官方开发指南From the Command Line - Referencing a Library Project :

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

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

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

关于android - Android如何处理多个R.java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10278413/

相关文章:

Java android fragment 拍照

unit-testing - 如何运行依赖于 Android 库模块的单元测试?

android - 有没有办法将配置文件从客户端应用程序传递到库项目?

android - 创建来自屏幕顶部的 Toast

Android 服务未收到位置更新

java - 如何在Java中定义复选框?

objective-c - 在 Cocoa 中重用重要代码的机制?

graph - 模块化如何帮助网络分析

java - openCV 中 BROWN 颜色的 HSV 值范围是多少?

c++ - 使用多个 slider 在自定义 QWidget 中映射 QSlider::valueChanged 信号