Search code examples
androiddalvikinternalsart-runtime

What is the process android does when starting a app and how it interact with R class at runtime?


Recently when I was learning about resource management of Android, I ran into some interesting issues.

1) From the internal perspective of Android runtime, what steps are taken by android OS to start an installed application?

2) R class- R class is a container for static numeric values for resources. But at the end its a collection of integers. How android maps these integers to resources internally?

setContentView(R.layout.activity_main); 

Here what setContentView() passed is an int. But this maps to activity_main.xml file.

Same goes with resources like strings and etc. The connection between resource files, code and R class, at runtime seems to be bit confusing.


Solution

  • 1) From the internal perspective of Android runtime, what steps are taken by android OS to start an installed application?

    In case of app is getting launched for the very 1st time after install, android system has to perform below 3 steps
    1.Load and launch the app
    2.Display a blank starting window, immediately after the app launch &
    3.Create the process for the app.
    These are the responsibilities of android system.

    Once app process is created by the android run time,with above step, then the control is with app process itself, to execute further steps to fully make app & its components available for execution. These include following,
    1. Creating the app object.
    2. Launching the main thread or the UI thread.
    3. Creating the main activity instance. This activity typically has your application's UI which user sees after the launch
    4. Inflating views contained in the activity. This includes all the views in the ViewGroup/view hierarchy
    5. Laying out the screen.
    6. Performing the initial draw.

    2) R class- R class is a container for static numeric values for resources. But at the end its a collection of integers. How android maps these integers to resources internally?

    Android Build system has many tools and 1 of them is called aapt tool and this tool is the 1 which generate R class which contains resource IDs for all the resources in your res/ directory. For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources), and for each resource of that type, there is a static integer (for example, R.drawable.icon).

    So now all the non-code things of your app is in R.java. The Android resource system keeps track of all non-code assets associated with an application and this is the internal system which manages the mapping of all resource IDs and beside that Android framework has a class called Resources, and You can use this class to access your application's resources, which are packaged into R.java by the android build system.

    You can generally acquire the Resources instance associated with your application with getResources().