Android Studio为新项目创建项目模板

时间:2022-05-19 20:43:29

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:

在创建新的Android Studio项目时,我希望自动创建以下内容:

  1. Specific packages and directories;
  2. 具体的包和目录;

  3. Gradle dependencies
  4. Several classes (might be imported from somewhere else)
  5. 几个类(可能从其他地方导入)

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

是否可以为这些任务创建Android Studio模板?

2 个解决方案

#1


14  

Check out this folder under your android SDK folder:

查看android SDK文件夹下的这个文件夹:

android-sdk\tools\templates\projects

Android Studio为新项目创建项目模板

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:

每个模板文件夹都有不同的ftl文件,这些文件是每个现有项目的模板:

Android Studio为新项目创建项目模板

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

您可以创建新的模板文件夹并根据需要设计ftl文件。并定义项目结构。

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.

对于gradle依赖项,根据需要将它们添加到* .gradle.ftl文件中。

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

要复制/导入现有的类/文件,请查看recipe.xml.ftl以了解如何复制。




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

更新:人们报告说,如果在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.

我再次搜索并找到了存储这些模板的另一个位置,即直接在Android Studio安装文件夹下,而不是Android SDK文件夹。

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

已验证Android Studio版本1.5和Android Studio版本2.0 Beta:

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

Android Studio为新项目创建项目模板

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

它们的文件类型和组织与前面提到的相同。

#2


4  

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

re:默认Gradle Dependencies没有prev。答案对我有用。密钥文件是

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

重要的部分是它在NewAndroidModule中,而不是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:

这是模块应用程序的正确输出build.gradle:

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.

所以,最后,在build.gradle生成之后,我相应地取消注释。

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. :-{

这花了我一整天才搞清楚。我对Gradle doc'n和Android Studio Doc'n非常不满。这应该是一个简单的任务w /快速简单的doc'n。事实上,我仍然不太了解gradle confign。 : - {

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

上面的目录序列适用于我安装的Android Studio。您的Android Studio可能已安装在其他位置,但此答案仍然相关。

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.

警告:我刚刚尝试更新我的Android Studio,因为它注意到文件中的更改。因此,在修改之前,请确认您备份此文件。然后在更新Android Studio之前恢复文件。

#1


14  

Check out this folder under your android SDK folder:

查看android SDK文件夹下的这个文件夹:

android-sdk\tools\templates\projects

Android Studio为新项目创建项目模板

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:

每个模板文件夹都有不同的ftl文件,这些文件是每个现有项目的模板:

Android Studio为新项目创建项目模板

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

您可以创建新的模板文件夹并根据需要设计ftl文件。并定义项目结构。

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.

对于gradle依赖项,根据需要将它们添加到* .gradle.ftl文件中。

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

要复制/导入现有的类/文件,请查看recipe.xml.ftl以了解如何复制。




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

更新:人们报告说,如果在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.

我再次搜索并找到了存储这些模板的另一个位置,即直接在Android Studio安装文件夹下,而不是Android SDK文件夹。

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

已验证Android Studio版本1.5和Android Studio版本2.0 Beta:

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

Android Studio为新项目创建项目模板

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

它们的文件类型和组织与前面提到的相同。

#2


4  

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

re:默认Gradle Dependencies没有prev。答案对我有用。密钥文件是

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

重要的部分是它在NewAndroidModule中,而不是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:

这是模块应用程序的正确输出build.gradle:

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.

所以,最后,在build.gradle生成之后,我相应地取消注释。

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. :-{

这花了我一整天才搞清楚。我对Gradle doc'n和Android Studio Doc'n非常不满。这应该是一个简单的任务w /快速简单的doc'n。事实上,我仍然不太了解gradle confign。 : - {

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

上面的目录序列适用于我安装的Android Studio。您的Android Studio可能已安装在其他位置,但此答案仍然相关。

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.

警告:我刚刚尝试更新我的Android Studio,因为它注意到文件中的更改。因此,在修改之前,请确认您备份此文件。然后在更新Android Studio之前恢复文件。