androidandroid-multidexdexclassloader

How to check if the second dex was loaded completed or not after my application was installed?


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.


Solution

  • 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 :)