An access to a LoadedApk instance while application is already running is an issue to be solved. The Application5class holds a reference to the LoadedApk instance; see source code 5 . At the same time, the Application instance is available from any activity with the aid of a getApplication() method. An mLoadedApk field of the LoadedApk type is annotated by“@hide”, thus the field is not a part of the public API. For our purpose this is an undesirable restriction.
Newly created RefChanger class is responsible for the change of the references to the ClassLoader and Resources instances in the LoadedApk class. The RefChanger instance isinitialized right after starting the application and only one instance exists here (it is a singleton). DynamicDexClassLoader is the second key class of the whole solution. This ClassLoader is responsible for loading of the classes of modules
When attempting to run particular module the application will look up a DynamicDexClassLoader instance (intended for loading this module) in a hash map. If it is found, then the reference in the LoadedApk instance is changed to this DynamicDexClassLoader, which loads classes from the module’s package. The reference to the Resources is changed in the same way. If there is no DynamicDexClassLoader in the hash map yet, it is created. After that, the DynamicDexClassLoader is put into the map for future reuse and the mentioned references are changed. Finally, an I ntentdefining the Activity to be run (ModuleActivity.class) is created and the Activity of the module is run.