android R.java mapping to resource layouts when a library project is added

人盡茶涼 提交于 2019-12-09 03:23:02

问题


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

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

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

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

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.

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 !!

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.

How is this happening Please help

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

}

}

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

}


回答1:


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

Hope it answers your question



来源:https://stackoverflow.com/questions/9994940/android-r-java-mapping-to-resource-layouts-when-a-library-project-is-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!