top of page
Search

Class Loading Process in Android App Developments - CodeShoppy

Writer's picture: Code ShoppyCode Shoppy

The Google Android platform supports provisioning of packaged applications to an Android device. However, an existing approach requires user's interaction during the installation of a new application or its modules. We present a new approach to dynamic modules loading which enables provisioning of new modules to Android device dynamically without the interaction with the user. It will allow complex applications to adapt to the surrounding conditions and requirements of the user by downloading additional code from a server or a neighboring peer device. In our solution we propose to replace the default application class-loader with a custom one while employing some existing mechanisms of class-loading from APK packages at the Android platform.


Then the analysis of an ActivityClientRecord3 class follows. It is the inner class of the ActivityThread class. The PackageInfo variable is a field of the ActivityClientRecord class and is of a LoadedApk type (see source code 3). In the LoadedApk class a method named getClassLoader() is found. This method returns a ClassLoader, which is designated for loading of application classes





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 .


After start of the application, the RefChanger class is initialized and the following operations will be performed: •References to the original Resources and ClassLoader will be created•Reference to the LoadedApk instance will be obtained with the aid of the Java Reflection API•References to fields of ClassLoader and Resources type in the LoadedApk class will be obtained with the aid of the Java Reflection API•An object HashMap<String, DynamicDexClassLoader> will be created. It will hold references to already created DynamicDexClassLoaderinstances


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.




 
 
 

Commentaires


©2019 by Android-PHP. Proudly created with Wix.com

bottom of page