How Gradle Build Works in Android
Explaining here , How Gradle LifeCycle Works during build phase:
1.) Initialization Phase (Setting.gradle file) -> All Projects are listed in this file since it is required to make new Instance for every module Project so that Gradle can make queue to run build.gradle of all module projects during build process.
2.) Configuration Phase ->Gradle will run through all build.gradle files of every Module and start making Directed Graph for All Dependencies present in individual build.gradle
3.) One more Important Point is that during Configuration phase if any child module is having larger dependency version then all parent will get that larger Dependency version during their configuration phase while if child having lower version dependencies then child will build up with lower version configuration and parent may run on higher dependendency version if defined in parent build.gradle file.
4.) Now During graph formation it checks whether there is any cycle in dependencies if yes then it stops execution and show gradle error with cyclic dependencies else it keep processed
5.) During formation of Graph with individual build.gradle script running from the queue , it also keep checking on dependencies version and it gives latest version found till that moment for every dependency to that module and if latest found , it updates latest version to Dependencies Graph node(This can be overriden if we define some force strategy to keep lower version)
6.) After Acyclic Directed Graph is formed with all version resolution then Task execution started with only one version of each dependency in the graph, hence during configuration phase you can see different version on Gradle dashboard but during execution phase only one version will be there for each dependencies in final build.
7.) You may have seen NoSuchMethodError at Runtime some time, That is because of different modules having same dependency with different versions and during configuration phase modules were compiled with their local version , but during final execution phase only one version wins after version conflict strategy and may be in new version some methods are not available which gives error at runtime like NoSuchMethodError.
8.) Consider Every Node of Graph as Map with key as dependency Name and Value as PriorityQueue of conflicting Dependencies Versions and Gradle pick the latest version from priorityQueue in the final apk.
I hope it can be helpful for some one , I know I have just written it theortical without diagram due to lack of time , But if someone facing any issue regarding gradle , he may have some hints from above pointers.Thanks.