Modify settings.gradle file of a project created from a custom template


I am creating a plugin for Android Studio to add a new project template. When a project is created from my custom template, I can add a repository to the settings.gradle file that has been generated. Instead of adding this repository to each project that I create, I want it to be added to the settings file by default when creating the project. With the Android API, I can add a dependency using RecipeExecutor.addIncludeToSettings method, but except for this, I can't modify the settings.gradle file.
How can I do this?

Comment actions Permalink

There is no easy-to-use API to do this. Please file a feature request to Android Studio bug tracker:

As a workaround you can try obtaining `GradleSettingsModel` for the project right from your recipe and modifying `GradleSettingsModel` directly without `RecipeExecutor`. You may need to commit document changes (`PsiDocumentManager#commitDocument`) before obtaining `GradleSettingsModel` and after you've made the changes to be in sync with the `RecipeExecutor`. E.g. see `DefaultRecipeExecutor#getBuildModel`

Reference implementation:

Comment actions Permalink

Thanks for your Answer.

To solve my issue, I chose to create a gradle init script in which I defined all repositories needed by my custom projects. I could remove the repositories present in the settings.gradle file by deleting the file and recreating it using method. This solution works fine, but thanks for your workaround.


Please sign in to leave a comment.