forked from sinkcup/AntDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_rules.xml
148 lines (129 loc) · 6.26 KB
/
custom_rules.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules">
<target name="auto-debug">
<property name="build.last.is.packaging.debug" value="true" />
<propertyfile file="auto.prop">
<entry key="build.last.is.packaging.debug" value="${build.last.is.packaging.debug}" />
</propertyfile>
<antcall target="-auto">
</antcall>
</target>
<target name="auto-release">
<antcall target="-auto">
</antcall>
</target>
<target name="-auto">
<!-- 复制项目到临时目录,避免替换打包影响本目录代码 -->
<dirname property="auto.here.dir" file="${ant.file}" />
<property name="auto.project.tmp.dir" value="${auto.here.dir}Tmp" />
<delete dir="${auto.project.tmp.dir}" />
<copy todir="${auto.project.tmp.dir}" overwrite="true">
<fileset dir="./">
<!-- 忽略隐藏文件 -->
<exclude name=".*" />
<exclude name=".*/*" />
</fileset>
</copy>
<!-- 解析AndroidManifest.xml 获得包名 -->
<xmlproperty file="AndroidManifest.xml" collapseAttributes="true" />
<!-- 写入配置文件 -->
<propertyfile file="auto.prop">
<entry key="auto.package" value="${manifest.package}" />
<entry key="auto.final.versionCode" value="${manifest.android:versionCode}" />
<entry key="auto.final.versionName" value="${manifest.android:versionName}" />
<entry key="auto.project.tmp.dir" value="${auto.project.tmp.dir}" />
</propertyfile>
<!-- 检查参数或配置中是否指定了包名 -->
<condition property="auto.has.package">
<isset property="package" />
</condition>
<!-- 修改包名 -->
<antcall target="-change-package-name">
</antcall>
<!-- 修改友盟渠道 -->
<antcall target="-change-umeng-channel">
</antcall>
<condition property="auto.version.is.time">
<equals arg1="${version}" arg2="time" />
</condition>
<!-- 修改版本号 -->
<antcall target="-change-version">
</antcall>
<condition property="build.last.target" value="debug" else="release">
<istrue value="${build.last.is.packaging.debug}" />
</condition>
<!-- 执行ant debug或者ant release进行打包 -->
<exec dir="${auto.project.tmp.dir}" executable="ant">
<arg value="${build.last.target}" />
</exec>
<!-- 复制打好的包 到 本目录下 -->
<antcall target="-cp-out-final-file">
</antcall>
<property file="auto.prop" />
<echo message="package: ${auto.package}" />
<echo message="UMENG_CHANNEL : ${auto.umeng.channel}" />
<echo message="versionCode: ${auto.final.versionCode}" />
<echo message="versionName: ${auto.final.versionName}" />
</target>
<target name="-change-package-name" if="auto.has.package">
<propertyfile file="auto.prop">
<entry key="auto.package" value="${package}" />
</propertyfile>
<echo message="old package: ${manifest.package}" />
<echo message="new package: ${package}" />
<replace dir="${auto.project.tmp.dir}" excludes="build.xml" token="${manifest.package}" value="${package}">
</replace>
</target>
<target name="-change-umeng-channel">
<condition property="UMENG_CHANNEL" value="debug" else="release">
<istrue value="${build.last.is.packaging.debug}" />
</condition>
<propertyfile file="auto.prop">
<entry key="auto.umeng.channel" value="${UMENG_CHANNEL}" />
</propertyfile>
<echo message="UMENG_CHANNEL : ${UMENG_CHANNEL}" />
<replaceregexp file="${auto.project.tmp.dir}/AndroidManifest.xml"
match="<meta\-data(\s+)android:name="UMENG_CHANNEL"(\s+)android:value="[a-zA-Z0-9]+""
replace="<meta\-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL}""
/>
</target>
<!-- 使用时间戳作为版本数字,使用日期作为版本名,比如Ubuntu 13.04使用yy.MM,MIUI 4.1.17使用年的最后一位.M.d
本脚本使用 yy.M.d.HHmm,比如14.1.1.1800 表示 14年1月1日18点00分。
由于开发时经常发布版本,所以精确到分钟。可能几分钟前刚打的包,改了字符串,又重新打包。
-->
<target name="-change-version" if="auto.version.is.time">
<script language="javascript">
<![CDATA[
property = project.setProperty("now",Math.floor((new Date()).getTime()/1000));
]]>
</script>
<tstamp>
<format property="auto.final.versionCode"
pattern="${now}" />
<format property="auto.final.versionName"
pattern="yy.M.d.HHmm" />
</tstamp>
<propertyfile file="auto.prop">
<entry key="auto.final.versionCode" value="${auto.final.versionCode}" />
<entry key="auto.final.versionName" value="${auto.final.versionName}" />
</propertyfile>
<echo message="versionCode: ${auto.final.versionCode}" />
<echo message="versionName: ${auto.final.versionName}" />
<replaceregexp file="${auto.project.tmp.dir}/AndroidManifest.xml"
match="android:versionCode="(\d+)""
replace="android:versionCode="${auto.final.versionCode}""
/>
<replaceregexp file="${auto.project.tmp.dir}/AndroidManifest.xml"
match="android:versionName="([a-zA-Z0-9\.]+)""
replace="android:versionName="${auto.final.versionName}""
/>
</target>
<target name="-cp-out-final-file">
<property file="auto.prop" />
<condition property="build.last.target" value="debug" else="release">
<istrue value="${build.last.is.packaging.debug}" />
</condition>
<copy file="${auto.project.tmp.dir}/${out.dir}/${ant.project.name}-${build.last.target}.apk" tofile="${out.absolute.dir}/${auto.package}-${auto.final.versionName}-${auto.umeng.channel}-${build.last.target}.apk" overwrite="true">
</copy>
</target>
</project>