6

In Java, the default system ClassLoader can be replaced using:

java -Djava.system.class.loader=com.test.MyClassLoader xxx

This way, the custom ClassLoader is used to load new classes, while having the default system ClassLoader as its parent.

Can the same system ClassLoader replacement be done in an Android application using a non-runtime method? (gradle/manifest or some other compilation flag)

I am not looking to replace the system ClassLoader at runtime, but set a different system ClassLoader to the whole application as it starts, before even the Application class is loaded.

The end goal is to run code before even the Application class's <clinit> is called (this would essentially allow me to load all classes in the application as I see fit).

Itay Bianco
  • 697
  • 6
  • 16
  • Possible duplicate of [custom class loader for android?](https://stackoverflow.com/questions/10832029/custom-class-loader-for-android) – Martin Zeitler Aug 11 '19 at 08:45
  • 1
    not really. the post you mentioned wants to do the replacement at runtime. i would like to know if the ClassLoader replacement can be done at the APK level (compile/link time). – Itay Bianco Aug 11 '19 at 08:50
  • But you have to consider, that the command you're showing replaces the class loader at runtime as well. Would the approach you find with `MultiDex` be sufficient for you? It would be at runtime though. – tynn Aug 11 '19 at 13:02
  • i edited my question to reflect that i need to have an entry point in the app start where i can run code before the Application class clinit. – Itay Bianco Aug 11 '19 at 13:21
  • @tynn what do you mean? how is MultiDex related to the question? also, according to an article i read, the command i wrote causes the bootstrap ClassLoader to load MyClassLoader as the system ClassLoader, with the default system ClassLoader as its parent. sure the actual change is at runtime, but it happens by the JVM (i think) well before any application user code is run. – Itay Bianco Aug 11 '19 at 13:24
  • ah, my mistake. according to https://stackoverflow.com/questions/11395074/what-loads-the-java-system-classloader in the above command, the default system classloader loads MyClassLoader, not the bootstrap classloader. – Itay Bianco Aug 11 '19 at 13:35

0 Answers0