Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio create project template for new projects

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?

like image 329
Mikhail Avatar asked Feb 04 '16 11:02

Mikhail


People also ask

What is project template in Android Studio?

Android Studio provides code templates that follow the Android design and development best practices to get you on the right track to creating beautiful, functional apps. You can use templates to create new app modules, individual activities, or other specific Android project components.

How do you create a new project in Android Studio?

If you don't have a project opened, Android Studio shows the Welcome screen, where you can create a new project by clicking Start a new Android Studio project. If you do have a project opened, create a new project by selecting File > New > New Project from the main menu.

How do I copy one Android project to another?

Select your project then go to Refactor -> Copy... . Android Studio will ask you the new name and where you want to copy the project. Provide the same. After the copying is done, open your new project in Android Studio.


2 Answers

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.

like image 83
AndroidMechanic - Viral Patel Avatar answered Oct 09 '22 04:10

AndroidMechanic - Viral Patel


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.

like image 21
JDOaktown Avatar answered Oct 09 '22 05:10

JDOaktown