GOOGLE告诉我说, 后续Eclipse将不再维护了, 所以, 赶紧下了个Android Studio。

刚开始使用, 各种不习惯,这里就不说了,今天讲讲,如何修改项目代码生成模板。

* 需说明, 我使用的是ubuntu下的开发环境。

相关目录:

安装目录/android-studio/plugins/android/lib/templates

drwxrwxr-x 18 anson anson 4096 9月 3 22:42 activities/ 默认Activity生成模版

-rw-r--r-- 1 anson anson 310 6月 3 11:25 build.gradle

drwxrwxr-x 6 anson anson 4096 7月 6 10:51 eclipse/

drwxrwxr-x 4 anson anson 4096 6月 3 13:57 gradle/

drwxrwxr-x 10 anson anson 4096 9月 4 15:30 gradle-projects/ 项目生成模板

-rw-r--r-- 1 anson anson 10695 6月 3 11:25 NOTICE

drwxrwxr-x 28 anson anson 4096 8月 1 09:38 other/

目前我只更改上面两个目录,其它的内容修改起来都相似。

* 修改过程,建议先将原有的模板进行备份, 以免出了问题后改不回来。

/plugins/android/lib/templates/gradle-projects/NewAndroidModule/

1. 不使用com.android.support:appcompat-v7, 不引用appcompat

|--globals.xml.ftl

<global id="appCompat" type="boolean" value="${(hasDependency('com.android.support:appcompat-v7'))?string}" />

改为:

<global id="appCompat" type="boolean" value="false" />

2. 去掉test 部分的代码

|-- recipe.xml.ftl

删除以下代码

    <instantiate from="test/app_package/ApplicationTest.java.ftl"                   to="${testOut}/ApplicationTest.java" />

以后生成的项目中就不再存在***.****.****(test)这个包和ApplicationTest.java了

3. 修改AndroidManifest.xml

|-- root/AndroidManifest.xml.ftl

<application <#if minApiLevel gte 4 && buildApi gte 4>android:allowBackup="true"</#if>        android:label="@string/app_name"<#if copyIcons && !isLibraryProject>        android:icon="@mipmap/ic_launcher"<#elseif assetName??>        android:icon="@drawable/${assetName}"</#if>        <#if baseTheme != "none" && !isLibraryProject>        android:theme="@style/AppTheme"</#if>>

我想删除theme的声明,所以,删除

<#if baseTheme != "none" && !isLibraryProject>        android:theme="@style/AppTheme"</#if>

* 判断语句: <#if ..........> xxx </#if>

接下来,修改Activity生成模板,我采用方法是增加一个模板。

/plugins/android/lib/templates/activities

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 AlwaysOnWearActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 AndroidTVActivity/

drwxrwxr-x 3 anson anson 4096 9月 3 22:58 ASActivity/

drwxrwxr-x 3 anson anson 4096 9月 4 14:43 BlankActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 BlankActivityWithFragment/

drwxrwxr-x 3 anson anson 4096 6月 3 13:57 BlankWearActivity/

drwxrwxr-x 3 anson anson 4096 6月 3 13:57 EmptyActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 FullscreenActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 GoogleAdMobAdsActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 GoogleMapsActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 GooglePlayServicesActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 LoginActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 MasterDetailFlow/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 NavigationDrawerActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 SettingsActivity/

drwxrwxr-x 3 anson anson 4096 8月 1 09:38 TabbedActivity/

每一个文件夹对就一个模板,ASActivity/ 是我的模板, 拷贝自:BlankActivity/

剩下的,就是修改ASActivity/中的内容

4. 修改布局文件:

|--ASActivity/root/res/layout/activity_simple.xml.ftl

<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"    tools:context="${relativePackage}.${activityClass}">

删除:

xmlns:tools="http://schemas.android.com/tools"    tools:context="${relativePackage}.${activityClass}">

*实在是用不惯这些代码...

其它资源文件类似, 默认都在这几个文件目录下改。

5. 修改Activity的JAVA源码:

|--ASActivity/root/src/app_package/SimpleActivity.java.ftl

package ${packageName};import ${superClassFqcn};import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;<#if applicationPackage??>import ${applicationPackage}.R;</#if>public class ${activityClass} extends ${superClass} {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.${layoutName});    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.${menuName}, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

该删除的删除, 该增加的增加....

更多相关文章

  1. android编译系统的makefile文件Android.mk写法
  2. android文件系统挂载分析
  3. 如何在Android上编写高效的Java代码
  4. Android冷启动实现app秒开的实现代码
  5. Android基于多触控的图片缩放和拖动代码实现
  6. Android中使用代码控制Wifi及数据连接网络开关
  7. Android WebView中的JavaScript代码使用
  8. Android 自定义文件路径选择器

随机推荐

  1. 零知识证明应用到区块链中的技术挑战
  2. JS跳转引发浏览器NS_BINDING_ABORTED
  3. 信息与数据科学国际会议——与众位大咖共
  4. 4-8(继承的概念)
  5. Jenkins 之 安装部署与汉化
  6. ClearLinux包管理器swupd使用入门
  7. Exa – 替代ls命令的超好用工具
  8. 教你 2 种从 Linux 终端下载文件的方法
  9. 第四届大数据科学与工程国际会议(2019)
  10. 面向边缘计算的资源优化技术研究进展