gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:48 26 4
gpt4 key购买 nike

我正在为一个项目制作几个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com