這篇教程會爲你展現如何建立屬於你本身的代碼模板。大多數的Android開發者都至少一次使用過Android代碼模板來新建工程。可是你有沒有嘗試過建立本身的Android代碼模板? 在這篇教程中,咱們將會爲你展現可用於從Android Studio IDE生成包含Google Analytics配置的Android工程模板。php
要建立本身的模板,請確保:android
這是你的模板目錄,在Windows上,${android studio 安裝路徑}/plugins/android/lib/templates/,其餘平臺Linux,Mac OS請自行查找; 在GoogleAnalyticApplication文件夾中建立根文件夾和其餘文件,以下所示: app
每一個模板目錄必須包含一個template.xml文件,這其中包含有關模板的一些元數據,包括名稱,描述,類別和用戶可見參數這些IDE將做爲選項呈現給用戶。 文件中還會在指明FreeMarker處理的recipe文件名稱和全局變量文件,若是除了模板參數值以外還有全局變量應該對全部FreeMarker處理的文件可見。 Template.xmlmaven
<?xml version="1.0"?>
<template
format="3"
revision="4"
name="Google Analytics Application"
minApi="7"
minBuildApi="14"
description="Creates a new application that has already Google Analytics configuration.">
<category value="Activity" />
<formfactor value="Mobile" />
<parameter
id="activityClass"
name="Activity Name"
type="string"
constraints="class|unique|nonempty"
suggest="${layoutToActivity(layoutName)}"
default="MainActivity"
help="The name of the activity class to create" />
<parameter
id="layoutName"
name="Layout Name"
type="string"
constraints="layout|unique|nonempty"
suggest="${activityToLayout(activityClass)}"
default="activity_main"
help="The name of the layout to create for the activity" />
<parameter
id="classApplication"
name="Class extends Application"
type="string"
constraints="nonempty|class"
help="The name of class that extends from Application" />
<parameter
constraints="nonempty|string"
id="googleAnalyticID"
name="Google Analytic ID"
type="string"
help="Id of Google Analytic" />
<parameter
id="dispatchPeriod"
name="Dispatch Period"
help="Frequency of automatic dispatch in seconds. Defaults to 30 minutes (1800 seconds)."
type="string"
default="1800"/>
<parameter
id="autoActivityTracking"
name="Auto Activity Tracking"
help="If true, views (Activities) will be tracked automatically. false by default."
type="boolean"
default="false"/>
<!-- 128x128 thumbnails relative to template.xml -->
<thumbs>
<!-- default thumbnail is required -->
<thumb>template_google_analytics.png</thumb>
</thumbs>
<globals file="globals.xml.ftl" />
<execute file="recipe.xml.ftl" />
</template>
複製代碼
其中的一些參數好比: activityClass, layoutName, classApplication, googleAnalyticID, dispatchPeriod, autoActivityTracking,將在建立項目時顯示在彈出窗口中。ide
這是一個可選的文件,裏面包含全局變量的定義,用於此模板的全部FreeMarker處理任務。 globals.xml.ftlpost
<?xml version="1.0"?>
<globals>
<global id="manifestOut" value="${manifestDir}" />
<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />
<global id="resOut" value="${resDir}" />
</globals>
複製代碼
root文件夾中包含模板源代碼ui
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="${packageName}">
<application
android:name="${packageName}.${classApplication}"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.gms.analytics.globalConfigResource"
android:resource="@xml/analytics_global_config" />
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
複製代碼
其中的packageName,classApplication是在template.xml文件中定義的。this
package ${packageName};
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
/**
* Created by TungDX on 5/29/2015.
*/
public class ${classApplication} extends Application {
private static GoogleAnalytics analytics;
private static Tracker tracker;
@Override
public void onCreate() {
analytics = GoogleAnalytics.getInstance(this);
tracker = analytics.newTracker("${googleAnalyticID}");
}
public static GoogleAnalytics getGoogleAnalytics() {
return analytics;
}
public static Tracker getTracker() {
return tracker;
}
}
複製代碼
其中的packageName,googleAnalyticID是在template.xml文件中定義的。google
package ${packageName}.activities;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import ${packageName}.${classApplication};
import ${packageName}.R;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
${classApplication}.getGoogleAnalytics().reportActivityStart(this);
}
@Override
protected void onStop() {
super.onStop();
${classApplication}.getGoogleAnalytics().reportActivityStop(this);
}
}
複製代碼
其中的packageName,classApplication是在template.xml文件中定義的。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView
android:text="@string/ready"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
複製代碼
<resources>
<#if !isNewProject>
<string name="title_${activityToLayout(activityClass)}">${escapeXmlString(activityTitle)}</string>
</#if>
<string name="ready">Google Analytic is ready!</string>
</resources>
複製代碼
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
複製代碼
recipe.xml文件中包含了從該模板生成代碼時應執行的各個命令。好比,你能夠複製某些文件或目錄,或者經過FreeMarker運行源文件,並要求IDE在代碼生成後打開一個文件。
<?xml version="1.0"?>
<recipe>
<dependency mavenUrl="com.android.support:support-v4:${targetApi}.+" />
<dependency mavenUrl="com.android.support:appcompat-v7:${targetApi}.+"/>
<dependency mavenUrl="com.google.android.gms:play-services:6+" />
<instantiate from="AndroidManifest.xml.ftl"to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />
<instantiate from="src/app_package/Application.java.ftl" to="${escapeXmlAttribute(srcOut)}/${classApplication}.java"/>
<instantiate from="src/app_package/activities/MainActivity.java.ftl" to="${escapeXmlAttribute(srcOut)}/activities/${activityClass}.java"/>
<instantiate from="res/xml/analytics_global_config.xml.ftl" to="${escapeXmlAttribute(resOut)}/xml/analytics_global_config.xml"/>
<instantiate from="res/layout/activity_main.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml"/>
<copy from="res/values/dimens.xml" to="${escapeXmlAttribute(resOut)}/values/dimens.xml"/>
<merge from="res/values/strings.xml.ftl"to="${escapeXmlAttribute(resOut)}/values/strings.xml" />
<open file="${escapeXmlAttribute(srcOut)}/activities/${activityClass}.java" />
<open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
</recipe>
複製代碼
<instantiate from="AndroidManifest.xml.ftl" to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />
複製代碼
<instantiate from="src/app_package/Application.java.ftl" to="${escapeXmlAttribute(srcOut)}/${classApplication}.java"/>
https://robusttechhouse.com/wp-admin/post.php?post=6937&action=edit&message=10#
複製代碼
<instantiate from="src/app_package/activities/MainActivity.java.ftl" to="${escapeXmlAttribute(srcOut)}/activities/${activityClass}.java"/>
複製代碼
<instantiate from="res/xml/analytics_global_config.xml.ftl" to="${escapeXmlAttribute(resOut)}/xml/analytics_global_config.xml"/>
複製代碼
<instantiate from="res/layout/activity_main.xml.ftl" to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml"/>
<copy from="res/values/dimens.xml" to="${escapeXmlAttribute(resOut)}/values/dimens.xml"/>
<merge from="res/values/strings.xml.ftl" to="${escapeXmlAttribute(resOut)}/values/strings.xml" />
複製代碼
<open file="${escapeXmlAttribute(srcOut)}/activities/${activityClass}.java" />
<open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
複製代碼
若是你的Android Studio IDE運行,請從新啓動它看到本身的模板以下圖
若是從模板生成項目時遇到此錯誤:AssertionError: Wrong line separators: ‘…plication;\r\n\r\nimport…’ at offset 29: 請檢查模板中全部文件的行分隔符是否正確,請確保使用正確的行分隔符在對應的的計算機的操做系統中。