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 examplehttps://geosoft.no/development/android.html. I am trying to get information about how gradle use Support libraries .aar file and integrate dependancies to create R.java 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 R.java, Java compilation of source code, creating dex file and furthur into APK file.
Thanks.