2011年11月1日星期二

关于动态装载dex

Dalvik目前的实现中,每一个dex文件里最多只能有65536个方法引用。为了解决这个问题,有一种办法是把一个dex分成多个dex,其中一个dex作为APK的主dex,而其他的dex保存在assets中,通过DexClassLoader动态装载进来。
但我实际测试下来,发现并不实用。通过DexClassLoader装载后,每个类必须使用DexClassLoader.loadClass()取到后才能实例化。而代码中有很多使用new实例化的对象,这些类是系统的PathClassLoader来装载,仍然会抛出ClassNotFound异常。而系统的ClassLoader是无法被替换的。
所以看来只好等Google的Dalvik的更新了。

tip:
1. Android的ClassLoader一般是PathClassLoader,PathClassLoader的父classloader是BootClassLoader.
2. when are classes loaded? There are exactly two cases: when the new bytecode is executed (for example, FooClass f = new FooClass();) and when the bytecodes make a static reference to a class (for example, System.out).

没有评论:

发表评论