添加库项目时 android R.java 映射到资源布局

标签 android r.java-file

我对我的问题做了一些研究 android R.java behavior when a library project is added

我观察到,当一个库项目被添加到任何 android 项目时,都会创建两个 R.java 文件。

项目.R.java

 public static final class layout {
    public static int capture=0x7f030000;
    public static int main=0x7f030001;
}

库.R.java

public static final class layout {
    public static final int add=0x7f030000;
    public static final int capture=0x7f030001;
    public static final int main=0x7f030002;
}

并且被设置为库的项目有自己的 R.java,看起来像

 public static final class layout {
    public static int capture=0x7f030000;
    public static int main=0x7f030001;
}

示例库只有一个 Activity ,我从我的应用程序开始,这个 Activity 设置主要布局。 现在,如果我们看到 R.java 中“main”的 id 在我的应用程序和库项目中是不同的。我试图从库中打印 id 的值及其给出的 0x7f030002,这是我的应用程序 R.java 文件中的值。

现在,当我将内容设置为 smain 时,我的应用程序没有主布局并且在库中,它设置库项目中的 main.xml! 如果我将主布局添加到我的应用程序项目中,库将把这个主布局设置为它的布局!!

即 main 的 id 是从我的应用程序的 R.java 中获取的,这个 id 与库中 main 的 id 不同,但布局是从库中正确选择的。

这是怎么回事 请帮忙

我的应用 Activity :

import com.idg.test.lib.TestLibActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class TestProjectActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i("starting","oncraete main id "+ R.layout.main);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add);
    startActivity(new Intent(this,TestLibActivity.class));

}

库 Activity :

    import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class TestLibActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("Library","Library main id" +R.layout.main );
    setContentView(R.layout.main);
}

最佳答案

发件人:Android developer site

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.

希望它能回答你的问题

关于添加库项目时 android R.java 映射到资源布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9994940/

相关文章:

java - 重命名生成 R.java 的包

android - 使用 Jeremy Feinstein 滑动条和操作栏 Sherlock 在 R.java 中缺少值

android - 使1个自定义 View 失效导致 View 组中所有自定义 View 失效?

android - 当 AppBarLayout 滚动时,RecyclerView fling 会导致延迟

java - 使用 FirebaseDatabase 检索数据

android - 创建新 Activity 时资源 NotfoundException

android - 为什么,android中的资源在R.java中被转换为HEXA-DECIMAL

Android "swipe"与 "fling"

android - 在 Eclipse 错误中创建 CVS(并发版本控制)时

android - R.java 生成问题