sekitaka
sekitaka

Reputation: 1032

How to create custom project template in Android Studio

Can I create custom project template in Android Studio ?

I find the directory "Android Studio.app/sdk/tools/templates/projects/NewAndroidApplication" and edit a little. But it is now work.

Do you have any idea ?

I want to create project from template like Xcode.

Upvotes: 17

Views: 19864

Answers (3)

JDOaktown
JDOaktown

Reputation: 4467

None of the prev. answers worked for me. Here's what I found: 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.

Upvotes: 7

Viral Patel
Viral Patel

Reputation: 33408

Probably late to answer but do check out this folder under your android SDK folder.

android-sdk\tools\templates\projects

This is where the existing new project templates are stored.

I've posted a detailed answer explaining in this post

Upvotes: 2

sekitaka
sekitaka

Reputation: 1032

Editing "gradle-projects/NewAndroidModule" . It works.

Upvotes: 0

Related Questions