How AAPT2 Works with Gradle

I wanted to create APK file independently without gradle with the help of build tools(i.e AAPT, dx.bat, ApkSigner etc), reference article example I am trying to get information about how gradle use Support libraries .aar file and integrate dependancies to create file for each of the support and core libraries added in build.gradle(app module). Also I wanted to Know the actual parameters used By AAPT2 by Gradle, I am also very curious to know the workflow of How resource files are being created and compiled into dex and further to APK. So that I can done all of it on command line. It would be very helpful If I get even a litle information about it. I have tried assembleDebug command of Gradle and it works like a charm, but I wanted to go more deeper about the working of Gradle for creating, Java compilation of source code, creating dex file and furthur into APK file.