How does the Fabric plugin edits the build.gradle file



I want to understand how the plugin is able to edit the build.gradle file so cleanly. I have been working on a project where you can manage your libraries with a checkbox, add delete or update them in your build.gradle.

But my parsing of the file is not reliable at the moment. I wanted to learn if I am missing any tricks to get it to work cleanly.