3

Our application has three dexes in apk which was built by gradle with multidex feature.

After the application was installed,

  1. Were the main dex and the second dex loaded by the same class loader?

  2. How to check if the second( classes2.dex ) and the third was loaded completed or not? Can I check it in code dynamically?

  3. Or can you guys introduce any related document about the procedure of class( or dex ) loading in Android? I am not familiar with this.

Thank you.

Zachary
  • 127
  • 1
  • 2
  • 15
  • There's a resource here: https://developer.android.com/tools/building/multidex.html – Fabian Tamp Sep 24 '15 at 08:35
  • Also, it depends on whether you're using ART (default on API20+) or Dalvik (default up to and including API19). – Fabian Tamp Sep 24 '15 at 09:01
  • 1
    For the 2nd question, I read ClassLoader.java and found that I can check if anyone class in 2nd dex was loaded by ClassLoader#findLoadedClass() with reflection. – Zachary Sep 24 '15 at 13:46

1 Answers1

4

I haven't dug into the ART code, but you can read what the Multidex support lib does for Dalvik here:

  1. Yes. The classes in all dexes are loaded by the same ClassLoader. When your Application loads, the MultiDex instrumentation:
    • extracts the extra dex files from the .apk and puts them in their own .zip files in a data directory (so that they're indistinguishable from APKs to the ClassLoader),
    • Obtains the ClassLoader from your Application's context, and adds those new .zip files to the list that the ClassLoader knows about reflectively.

Not only are the separate dexes loaded by the same ClassLoader, they're loaded by the same ClassLoader that would be used without MultiDex.

  1. The extra dexes should be setup automatically when your Application loads. It's not easy to check this in-code, but you can verify locally by checking the logs for your device.

  2. There's some Android docs about the architecture of ART and Dalvik. But, there's nothing better than reading the source :)

Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42
  • Thanks for you answer Fabian! I still have a question, our application has two processes named UI process and service process. Are their class loaders the same? If not, is it possible one class will be loaded twice? – Zachary Sep 24 '15 at 13:49
  • @Zachary separate processes within the same APK are entirely isolated from each other. So the classes in your 'UI' process and your 'service' have no knowledge of each other. `Application` will be created for each process, which means that multidex support will be installed for both processes. Both processes will be using the same type of Classloader, but they will be different instances, and will be completely unaware of each other. Does that make sense? – Fabian Tamp Sep 25 '15 at 00:39
  • yes it makes sense. So is it possible one class will be loaded twice since these two processes belong to one application ? One vm for one process, so one class will be loaded separately in each process, is It? – Zachary Sep 25 '15 at 06:18
  • Yes, that's correct! One VM per process is a great way of explaining it. – Fabian Tamp Sep 25 '15 at 08:27