Typically it happens because something is prematurely resolving some configuration somehow.
What the problem is in your specific case is hard to tell, especially as the exception is just the symptom, not the problem.
But most probably update AGP and use the new DSL might help: Cannot apply plugins to subprojects from within a settings plugin