Android R.java mapping to resource layouts when adding library project

I've done some research on my question into android R.java behavior when adding a library project

I noticed that when a library project is added to any android project, two R.java files are 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 that was installed as a library has its own R.java which looks like

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

      

There is only one activity in the example library, which I start with my application, and this activity sets up the basic layout structure. Now if we see the id for "main" in R.java is different in my application and in the library project. I tried to print the id value from the library and its value is 0x7f030002 which is the value in my R.java application file.

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

Those. the id for main is taken from the R.java of my application, and this id is different from the id for the main one in the library, but the layout is correctly selected from the library.

How does this happen? Please, help

activity of my application:

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

}

      

}

Operation

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

      

}

+1


source to share


1 answer


From: Android developer site

When you create an application that depends on a library project, the SDK Tools will compile the library into a temporary JAR file and use it in the main project, then use the result to generate the .apk. In cases where a resource identifier is defined in both the application and the libraries, 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 .apk application. This gives your application the flexibility to use or override any resource behavior or values ​​defined in any library.



I hope he answers your question.

+7


source







All Articles