Is it possible to intercept Android's default class loading and conditionally avoid loading a class ?
My motivation is to intercept Android's default class loading process and avoid loading the class based on a condition, something like
if (!fooScope) {
throw IllegalStateException("Trying to load class from FooScope");
}
I am not intending to change where the classes are loaded from, but to have fine grained control over what classes can get loaded at what point of time, which I call Strict class loading. In this, any class which is not supposed to be loaded until certain conditions are met, should not be loaded. I am looking for ways to intercept the class loading to achieve this.
You'll probably need to use some kind of Hooking library. like this library (https://github.com/cmzy/ZHookLib) which supports only up to Android 4.4
then you'll need to hook the loadClass(String name) method of ClassLoader.class in order to see all the loaded classes.
but still there is the problem of the order in which the classes are loaded. you could try to do it from a class which extends Application which might be loaded first but im not sure..
once you've hooked loadClass of ClassLoader you should be able to do what you like. remember that there is more than one ClassLoader and also im not sure what will happen if you throw an exception.
i hope it helps