How to create new template projects in Android Studio?

Summary

The question asks about creating custom Android Studio project templates (including dependencies like Hilt, Retrofit, etc.) in the current Android Studio environment (Electric Eel to Giraffe). The direct answer is that Android Studio does not have a built-in, user-friendly UI for creating new template projects similar to the “New Project” wizard. Instead, developers must use the Android Studio Plugin SDK to create a template directory, define a recipe.gradle file, and structure XML UI and FreeMarker templates (.ftl) to generate the project. For dependency injection (Hilt, Retrofit), you must define them in the recipe.gradle.kts or build.gradle templates within the module.

Root Cause

The root cause of this confusion is a mismatch between user expectation and tooling design. Android Studio is built on IntelliJ IDEA, which uses a templating system based on IntelliJ Platform Plugin SDK.

  • Missing GUI: There is no “Export as Template” button in the Android Studio UI for end-users.
  • Steep Learning Curve: Creating a template requires writing Groovy/Kotlin DSL and understanding the FreeMarker templating engine, rather than just configuring a Gradle project.
  • Dependency Configuration: Adding libraries like Hilt or Retrofit requires modifying the template’s build.gradle files programmatically, which is not exposed in the standard “New Project” dialog.

Why This Happens in Real Systems

In professional software development, project scaffolding is often automated to enforce architectural standards (e.g., MVVM, Clean Architecture).

  • Standardization: Companies need every new module to have pre-configured Hilt modules, Retrofit interfaces, and DI graphs.
  • Tooling Limitation: Android Studio treats these templates as plugins. To share them, they must be installed via a .jar file or hosted in a custom plugin repository.
  • Evolution: With the introduction of build.gradle.kts (Kotlin DSL), older templates (written for Groovy) break or require conversion, adding friction to the process.

Real-World Impact

Without custom templates, engineering teams face:

  • Boilerplate Fatigue: Developers manually copy-paste files, leading to inconsistent project structures.
  • Onboarding Delay: New hires spend hours setting up dependencies (Hilt, Retrofit) instead of writing feature code.
  • Configuration Drift: Different team members might use different versions of libraries if they aren’t standardized in the template.
  • Error Proneness: Manual setup increases the risk of missing critical DI annotations or AndroidManifest.xml entries.

Example or Code

To create a custom template, you typically need a directory structure inside the Android Studio plugins folder. Below is a minimal example of a recipe.gradle.kts (or recipe.gradle for Groovy) which is the script that runs when a project is generated. This script defines how dependencies (like Hilt and Retrofit) are added.

// recipe.gradle
// This script is executed during project generation to add dependencies.

// 1. Add Hilt and Dagger dependencies to the root build.gradle
// (This is a simplified logic; actual templates parse XML/DSL files)
def rootBuildGradle = new File(projectDir, '../../build.gradle')
rootBuildGradle.text = rootBuildGradle.text.replace(
    '// DETECTED_APPLY_PLUGINS',
    '// DETECTED_APPLY_PLUGINS\n    classpath "com.google.dagger:hilt-android-gradle-plugin:2.44"'
)

// 2. Add Hilt and Retrofit dependencies to the module build.gradle
def moduleBuildGradle = new File(projectDir, 'app/build.gradle')
moduleBuildGradle.text = moduleBuildGradle.text.replace(
    '// DEPENDENCIES',
    '''
    implementation "com.google.dagger:hilt-android:2.44"
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    kapt "com.google.dagger:hilt-compiler:2.44"
    '''
)

How Senior Engineers Fix It

Senior engineers avoid reinventing the wheel and look for standardized solutions:

  1. Use GitHub Templates: Create a clean “seed” repository on GitHub containing a fully configured project (Hilt, Retrofit, Navigation, etc.). Use git clone --depth=1 to start new projects instantly.
  2. Use Android Studio Plugins: Look for plugins like “Android Studio Template” or “Template Manager” from the marketplace, which provide a UI wrapper around the raw template system.
  3. Custom CLI Tools: Write a simple CLI script (using Python or Shell) that clones a base repo and replaces package names/placeholders.
  4. IntelliJ Plugin Development: For large organizations, develop an internal Android Studio plugin that provides a custom “New Project” wizard, fully integrated with CI/CD configurations.

Why Juniors Miss It

Junior developers often struggle because they look for features that don’t exist in the UI:

  • UI Assumption: They assume Android Studio has a “Save as Template” button similar to Visual Studio or Xcode.
  • Lack of IDE Internals Knowledge: They are unfamiliar with the IntelliJ Platform SDK, which governs how dialogs, wizards, and file templates work.
  • Gradle Understanding: They may not know how to manipulate build.gradle files programmatically, which is required to add custom dependencies like Retrofit or Hilt into the template itself.
  • Search Keywords: They often search for “Android project template” instead of “Android Studio plugin template,” leading them to outdated forum threads.