Unity3D研究院之Android使用ANT自动打包

好久没有写博客了,并不是我不想写,而真是没有时间写。感谢这段时间大家的留言,如果我没有回复的请见谅。最近做了一段时间Android渠道包的接入也算积累了点经验,希望可以帮助以后的朋友们,嘿嘿。
Unity打Android渠道包的传统步骤是,先把SDK放在plugins目录下,设置好证书后最后用unity直接生成.apk。 这是的确是正确的打包方式,但是这样做非常容易出错,SDK不能直接放在plugins目录下,必须要先编译。有些渠道的SDK TMD不是编译好的.jar 而是一个android工程,你必须要让你的unity工程去依赖它。。。所以这样做一不小心就会出错。。
为了彻底解决容易出错的问题,那么我们一定要使用Android的Ant来打包。结合到Unity的工作原理就是,先把unity工程导出成一个android工程,然后在把渠道的 assets lib 依赖 以及icon 拷贝进去,最后通过ant直接生成apk来。
1.先将unity导出成android工程,大家可以看我以前的文章。
http://www.xuanyusong.com/archives/3162
2.拷贝assets lib 依赖 以及icon
其实无非也就是shell脚本的 cp命令,多余的我也就不说了。这里我需要说一下splash启动图,unity不允许在android工程里面设置启动图,必须要在unity里设置。可以提前把一张启动图放在unity里面,然后在ProjectSetting里面关联上启动图,自动打包的时候根据不同的渠道将splash图片覆盖一下unity工程里面的图片,然后导出即可。
3.Android 与 ant 环境搭建
http://developer.android.com/sdk/index.html 建议你直接下载ADT ,这里不仅包含SDK也包含开发编辑器。如下图所示eclipse就是ADT开发编辑器,sdk就是android的sdk

如下图所示,记得要SDK都更新到最新,不然ant打包会报错。

打开ADT(Eclipse)在导航菜单栏中选择Help -> Install New Software
Work with中输入 http://download.eclipse.org/releases/juno
如下图所示,在下拉列表中选择General Purpose Tools 找到Eclipse Plug-in Development Environment (因为我已经装过了,所以这里不显示) 安装即可。

安装完毕后,打开终端直接输入 open -e .bash_profile
如下图所示,将ANT 和Android的环境变量都配置完毕。记得要关闭一下本文以及终端,方可生效。


到这一步ant的环境已经配置完毕。如下图所示,在ANT打包前我们要先把android工程生成出来。
AndroidManifest.xml 游戏名 包名 activity service都在这里,这里不做过多的解释了。。
assets:unity的.so放在这里。如果渠道包assets下面有文件也一并放在这里。
bin:就不用管了,他是android编译生成出来的。
build.xml:ant打包必备的资源文件,之前我们配置的环境变量就是为了它。下面我在贴出来内容。
gen:生成出来的,不用管了。
keystore.properties:证书的描述文件,证书的路径、密码啊啥的都在这里。
libs:将渠道libs文件夹下的拷贝机进来。
project.properties:如果渠道包是一个工程,那么就必须在这里进行依赖。这里写的就是依赖工程的路径。
res:资源
src:代码
Unity3D研究院之Android使用ANT自动打包(七十九) - 雨松MOMO程序研究院 - 5
Unity3D研究院之Android使用ANT自动打包(七十九) - 雨松MOMO程序研究院 - 5
build.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

<?xml version="1.0" encoding="UTF-8"?>
<project name="Game" default="help">

<!-- The local.properties file is created and updated by the 'android' tool.
     It contains the path to the SDK. It should *NOT* be checked into
     Version Control Systems. -->
<!--<property file="local.properties" />-->
<!--<condition property="sdk.dir" value="D:\\android\\android-sdk">

<os family="windows"/>
</condition>
<condition property="sdk.dir" value="/Users/dev/android-sdk">
<os family="mac"/>
</condition>-->

<!-- The ant.properties file can be created by you. It is only edited by the
     'android' tool to add properties to it.
     This is the place to change some Ant specific build properties.
     Here are some properties you may want to change/update:

     source.dir
         The name of the source directory. Default is 'src'.
     out.dir
         The name of the output directory. Default is 'bin'.

     For other overridable properties, look at the beginning of the rules
     files in the SDK, at tools/ant/build.xml

     Properties related to the SDK location or the project target should
     be updated using the 'android' tool with the 'update' action.

     This file is an integral part of the build system for your
     application and should be checked into Version Control Systems.

     -->
<property file="keystore.properties" />

<!-- if sdk.dir was not set from one of the property file, then
     get it from the ANDROID_HOME env var.
     This must be done before we load project.properties since
     the proguard config can use sdk.dir -->
<property environment="env" />
<condition property="sdk.dir" value="${env.ANDROID_SDK}">
    <isset property="env.ANDROID_SDK" />
</condition>

<!-- The project.properties file is created and updated by the 'android'
     tool, as well as ADT.

     This contains project specific properties such as project target, and library
     dependencies. Lower level build properties are stored in ant.properties
     (or in .classpath for Eclipse projects).

     This file is an integral part of the build system for your
     application and should be checked into Version Control Systems. -->
<loadproperties srcFile="project.properties" />

<!-- quick check on sdk.dir -->
<fail
        message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
        unless="sdk.dir"
/>

<!-- Import the actual build file.

     To customize existing targets, there are two options:
     - Customize only one target:
         - copy/paste the target into this file, *before* the
           <import> task.
         - customize it to your needs.
     - Customize the whole content of build.xml
         - copy/paste the content of the rules files (minus the top node)
           into this file, replacing the <import> task.
         - customize to your needs.

     ***********************
     ****** IMPORTANT ******
     ***********************
     In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
     in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml"/>

</project>

最后就是ant打包的指令了。
tempEclipsePath:是android 工程路径,先clean在release。
ant -buildfile ${tempEclipsePath}/build.xml cleanant -buildfile ${tempEclipsePath}/build.xml release
这样包就打在了bin目录下面,可以mv把apk拷贝到你需要的目录下面就好了。
mv -f ${tempEclipsePath}/bin/Game-release.apk ${out}
本文固定链接: http://www.xuanyusong.com/archives/3384
转载请注明: 雨松MOMO 2014年12月28日
雨松MOMO程序研究院 发表

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容