简体   繁体   中英

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

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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM