好久没有写博客了,并不是我不想写,而真是没有时间写。感谢这段时间大家的留言,如果我没有回复的请见谅。最近做了一段时间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:代码
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程序研究院 发表