问题
I observe quite a few behaviors on Android (I am working on a multidex issue, so I use an emulator in 4.4.4) that leave me speechless about Android class loading:
On Android classes are not supposed to be resolved when being loaded by the class loader. But if I create a class:
public class M {
public Foo m(String i) {
switch (i) {
case "0":
return new Foo();
case "1":
return new Foo2();
}
return null;
}
}
and debug my app, adding watches:
getClass().getClassLoader().findLoadedClass("Foo")
getClass().getClassLoader().findLoadedClass("Foo2")
Then I can see that loading M
does load Foo
and Foo2
!
So the class is resolved.
Strangely enough, and that is almost a second question, but if I add M extends Application
, then M
is not resolved at all, even when instantiating it. Foo
and Foo2
only get loaded into the JVM when m(X)
is called, (Foo
is loaded when X
is "0"
, Foo2
for X="1"
).
Does anyone here have a rational explanations for these underlying questions:
- Why is
M
resolved, classes should not be resolved. Even instantiatingM
should not loadFoo
orat least Foo2
. - Why does a class extending
Application
behave differently? - Are there other Android classes that behave in a different way?
来源:https://stackoverflow.com/questions/38796215/stranger-things-in-android-class-resolution