编程语言
首页 > 编程语言> > 添加库项目时将android R.java映射到资源布局

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

作者:互联网

我在
android R.java behavior when a library project is added

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

project.R.java

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

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

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

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

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

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

这是怎么回事
请帮忙

我的应用程式活动:

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

}

解决方法:

从: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.

希望它能回答您的问题

标签:r-java-file,android
来源: https://codeday.me/bug/20191009/1882521.html