Modify settings.gradle file of a project created from a custom template
Answered
Hi,
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?
Please sign in to leave a comment.
There is no easy-to-use API to do this. Please file a feature request to Android Studio bug tracker: https://issuetracker.google.com/issues/new?component=192708&template=840533
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: https://cs.android.com/android-studio/platform/tools/adt/idea/+/mirror-goog-studio-main:android-templates/src/com/android/tools/idea/templates/recipe/DefaultRecipeExecutor.kt?q=DefaultRecipeExecutor
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 RecipeExecutor.save method. This solution works fine, but thanks for your workaround.