Stranger things in Android class resolution

本小妞迷上赌 提交于 2019-12-06 04:18:56

问题


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 instantiating M should not load Foo or at 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!