35

First of all, I'm aware of this question; however I cannot follow the answer because there are no directories mentioned there.

When creating new Android Studio project I want the following to be created automatically:

  1. Specific packages and directories;
  2. Gradle dependencies
  3. Several classes (might be imported from somewhere else)

Is it possible to create an Android Studio template for these tasks?

Community
  • 1
  • 1
Mikhail
  • 3,666
  • 4
  • 30
  • 43

2 Answers2

26

Check out this folder under your android SDK folder:

android-sdk\tools\templates\projects

enter image description here

There are three predefined templates as seen below, which we generally use.

Each template folder has different ftl files which are templates for each of the existing projects:

enter image description here

You can create a new template folder and design the ftl files as you want them to. And also define project structures.

Just open one of these and you'll get an idea of how the default templates are there. Extend those or create your own from scratch as you want.

With the above said, your questions can be answered as follows:

For creating packages create a folder structure in the root folder to match your package name.

For gradle dependencies add them to your *.gradle.ftl files, as required.

For copying/importing exiting classes/files look at recipe.xml.ftl to get an idea of how to copy.




UPDATE: After people reported it not to be working if changed in the above mentioned folder under Android SDK

I searched again and found another location where these templates are stored i.e. directly under the Android Studio installation folder and not the Android SDK folder.

Verified for Android Studio Version 1.5 & Android Studio Version 2.0 Beta:

<installation folder>\plugins\android\lib\templates

enter image description here

The file types and organization of them is same as mentioned earlier.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • Thanks a lot for your answer! Do you know if there are any decent resources on `xml.ftl` stuff? – Mikhail Feb 04 '16 at 12:40
  • See if this helps: http://freemarker.incubator.apache.org/docs/xgui_imperative_learn.html Also saw several nice tutorials with direct google search. Try those. – Viral Patel Feb 04 '16 at 12:43
  • For some reason, changing `NewAndroidApplication/root/build.gradle.ftl` did not help - when I created new project after the change, the `build.gradle` file did not contain any new dependencies I specified in the `.ftl` file. I've checked sdk location in Android Studio - it points to the correct folder (in which I changed the template) – Mikhail Feb 04 '16 at 15:25
  • Could you restart android studio and try? – Viral Patel Feb 04 '16 at 15:27
  • will do. Meanwhile, could you explain, if it is possible to create packages inside root that would have the package name I specify in the project wizard? – Mikhail Feb 04 '16 at 15:29
  • What you specify in the wizard will get created that time. If you want to add other fixed ones put the folder structure in root under src\Java – Viral Patel Feb 04 '16 at 15:31
  • I get that; however I would like to automatically create packages like `.network` or `.ui` – Mikhail Feb 04 '16 at 15:33
  • Restarting did not help :( Is it possible to see somewhere which files AS uses for the templates? – Mikhail Feb 04 '16 at 15:37
  • Let me test it at my end in a few hours. I'll let you know. – Viral Patel Feb 04 '16 at 15:46
  • @AndroidMechanic Any solution here? I'd attempted similar as the original asker and have had no luck. To be honest, it appears that the templates are pulled from somewhere else. If you look at the "New Activity Template" gallery, the icons appear to have [material coloring](http://i.stack.imgur.com/IILcb.png) whereas the templates folder contains images like [this](http://i.imgur.com/2gd3buS.png) – pula Feb 11 '16 at 02:59
  • @AndroidMechanic that seems to clear up the issue! Thanks a bunch. – pula Feb 11 '16 at 19:36
  • 1
    On Mac OSX right click on the "Android Studio.app" and then click show package contents. You will find the plugins folder inside there. – startoftext Feb 29 '16 at 21:15
  • 2
    In Android Studio 4.1 this doesn't work. The template folder doesn't exist in the Android Studio folder – A-Android UCG Oct 21 '20 at 13:43
5

re: default Gradle Dependencies None of the prev. answers worked for me. The key file is

C:\Program Files\Android\Android Studio\plugins\android\lib\templates\gradle-projects\NewAndroidModule\root\build.gradle.ftl

The important part is it's in NewAndroidModule, NOT NewAndroidProject

Here's what my file looks like:

<#if !(perModuleRepositories??) || perModuleRepositories>
buildscript {
    repositories {
        jcenter()
<#if mavenUrl != "mavenCentral">
        maven {
            url '${mavenUrl}'
        }
</#if>
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:${gradlePluginVersion}'
    }
}
</#if>
<#if isLibraryProject?? && isLibraryProject>
apply plugin: 'com.android.library'
<#else>
apply plugin: 'com.android.application'
</#if>
<#if !(perModuleRepositories??) || perModuleRepositories>

repositories {
        jcenter()
<#if mavenUrl != "mavenCentral">
        maven {
            url '${mavenUrl}'
        }
</#if>
}
</#if>

android {
    compileSdkVersion <#if buildApiString?matches("^\\d+$")>${buildApiString}<#else>'${buildApiString}'</#if>
    buildToolsVersion "${buildToolsVersion}"

    defaultConfig {
    <#if isLibraryProject?? && isLibraryProject>
    <#else>
    applicationId "${packageName}"
    </#if>
        minSdkVersion <#if minApi?matches("^\\d+$")>${minApi}<#else>'${minApi}'</#if>
        targetSdkVersion <#if targetApiString?matches("^\\d+$")>${targetApiString}<#else>'${targetApiString}'</#if>
        versionCode 1
        versionName "1.0"
    }
<#if javaVersion?? && (javaVersion != "1.6" && buildApi lt 21 || javaVersion != "1.7")>

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_${javaVersion?replace('.','_','i')}
        targetCompatibility JavaVersion.VERSION_${javaVersion?replace('.','_','i')}
    }
</#if>
<#if enableProGuard>
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
</#if>
}

dependencies {
    <#if dependencyList?? >
    <#list dependencyList as dependency>
    compile '${dependency}'
    </#list>
    </#if>
    compile fileTree(dir: 'libs', include: ['*.jar'])
<#if WearprojectName?has_content && NumberOfEnabledFormFactors?has_content && NumberOfEnabledFormFactors gt 1 && Wearincluded>
    wearApp project(':${WearprojectName}')
    compile 'com.google.android.gms:play-services:+'
</#if>
<#if unitTestsSupported>
    testCompile 'junit:junit:${junitVersion}'
</#if>

//from C:\Program Files\Android\Android Studio\plugins\android\lib\templates\gradle-projects\NewAndroidModule\root\build.gradle.ftl
//logback
//    compile 'MyAwesomeDependency:1.1.+' 
//    compile 'MyOtherAwesomeDependency:1.1.+'
//    compile 'org.slf4j:slf4j-api:1.7.+'  
//end logback
//    compile 'com.google.code.gson:gson:2.+'

}

and here's the correct output build.gradle for module app:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.ntier.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

//from C:\Program Files\Android\Android Studio\plugins\android\lib\templates\gradle-projects\NewAndroidModule\root\build.gradle.ftl
//logback
//    compile 'MyAwesomeDependency:1.1.+' 
//    compile 'MyOtherAwesomeDependency:1.1.+'
//    compile 'org.slf4j:slf4j-api:1.7.+' 
//end logback
//    compile 'com.google.code.gson:gson:2.+'

    compile 'com.android.support:appcompat-v7:23.4.0'
}

So, finally, after the build.gradle is gen'd I uncomment accordingly.

This took me all day to figger out. I'm pretty displeased w/ the Gradle doc'n and the Android Studio Doc'n. This should have been an easy task w/ quick easy doc'n. As it is, I still don't understand gradle confign very well. :-{

The directory sequence above is for my installed Android Studio. Your Android Studio may be installed elsewhere but this answer is still relevant.

WARNING: I just tried to update my Android Studio and it balks because it's noticed the change in the file. So, MAKE SURE YOU BACKUP THIS FILE before you modify it. and then RESTORE THE FILE prior to updating Android Studio.

JDOaktown
  • 4,262
  • 7
  • 37
  • 52