添加库项目时将android R.java映射到资源布局 [英] android R.java mapping to resource layouts when a library project is added

查看:81
本文介绍了添加库项目时将android R.java映射到资源布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下位置对我的问题进行了一些研究 Android R.java在库时的行为项目已添加

I did some research on my question at android R.java behavior when a library project is added

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

I observe that when a library project is added to any android project, there are two R.java files created.

project.R.java

project.R.java

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

lib.R.java

lib.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,看起来像

and the project which was set as library has its own R.java which looks like

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

示例库只有一个活动,我是从我的应用程序开始的,该活动设置了版面主要.现在,如果我们在应用程序和库项目中看到R.java中"main"的id不同.我试图从库中打印id的值,并给出0x7f030002,这是我的应用程序R.java文件中的值.

The sample library has just one activity which i am starting from my application and this activity sets the layout main. Now if we see that id for "main" in R.java is different in my application and in the library project. I tried to print the value of id from library and its giving 0x7f030002 which is the value in my application R.java file.

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

Now my application has no main layout and in library when i set content a smain , its setting the main.xml from library project !! If i add main layout to my application project, the lib will set this main as its layout !!

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

Ie the id for main is taken from R.java of my application and this id is different from the id for main in the library but the layout is correctly picked from library.

这是怎么回事请帮助

我的应用活动:

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

}

}

lib活动:

    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);
}

}

推荐答案

来自:

构建依赖于库项目的应用程序时,SDK工具将库编译为临时JAR文件,并在主项目,然后使用结果生成.apk.在案件在应用程序和库,这些工具可确保在应用程序获得优先级,并且该库项目中的资源未编译到应用程序.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.

希望它能回答您的问题

这篇关于添加库项目时将android R.java映射到资源布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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