本文介绍了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

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


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

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

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

由于这些工具将库项目的资源与从属应用程序项目的资源合并,因此可以在两个项目中定义给定的资源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中引用-引用图书馆项目:

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

 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:56
查看更多