Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 372
sekitaka Avatar asked Jul 03 '14 06:07

sekitaka


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.


2 Answers

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.

like image 148
JDOaktown Avatar answered Sep 19 '22 16:09

JDOaktown


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

like image 43
AndroidMechanic - Viral Patel Avatar answered Sep 22 '22 16:09

AndroidMechanic - Viral Patel