简体   繁体   中英

dynamic class reloading in dalvik on Android

I am trying to understand if I can dynamically reload an existing class / load in a new class in the Dalvik vm running on Android. Searching online gives me a few mixed answers:

What can you not do on the Dalvik VM (Android's VM) that you can in Sun VM?

How to load a Java class dynamically on android/dalvik?

http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html

http://larshamren.blogspot.com/2012/02/android-dynamically-loading-classes.html

Can someone confirm if it is possible to do so on Android? And does it work the exact same way as Sun's vm, maybe except that Dalvik loads in .dex files and they need to be packaged in a jar? Is there any limitation on the Android platform for doing class loading? If there is official Dalvik documentation regarding this that would be great.

The android-developers article you listed shows how to use a "plugin" approach for loading classes in Dalvik with DexClassLoader .

The way it's supposed to work is that, if there are no more references to the ClassLoader or any classes loaded from it, the classes themselves can be garbage-collected. In practice, the current version of Dalvik (Android 4.2 "Jellybean") doesn't support class unloading, so the classes are there to stay.

You could define a new DexClassLoader and load a new version of those classes, but you will leak the previous copy. This may or may not be an issue depending on how often you expect to load a new set of classes and how large they are.

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