zipalign配合Android多渠道打包
环境准备
下载AndroidSdk,因为我们需要tools下面的zipalign可执行文件,其作用在于对包文件进行优化。
描述
接下来说明的多渠道打包是基于现有存在一个已打好的包的基础上,再往包内META-INF/目录下创建以渠道命名文件的方式进行对渠道包的标识。
上脚本
#coding=utf-8
import os
import sys
import zipfile
import shutil
if __name__ == '__main__':
apkFile = sys.argv[1]#传进来包文件的路径及文件
channel = sys.argv[2]#需要打的渠道
version = sys.argv[3]#版本,目的仅在于区分打包的版本
apk = 'YYYY'#APK名字前缀
emptyFile = '/data/package_shell/ZZZ.txt'
f = open(emptyFile, 'w')
f.close()
if (os.path.exists("/data/package_shell/release")):
print "/data/package_shell/release exists"
else:
os.mkdir('/data/package_shell/release')
channelDir = '/data/package_shell/release/%s' % (channel)
if (os.path.exists(channelDir)):
print "channelDir exists"
else:
os.mkdir(channelDir)
destfile = '/data/package_shell/release/%s/%s.apk' % (channel, apk)
shutil.copy(apkFile, destfile)
zipped = zipfile.ZipFile(destfile, 'a')
channelFileName = 'channel_' + channel
channelFile = "META-INF/{channelname}".format(channelname=channelFileName)
zipped.write(emptyFile, channelFile)
zipped.close()
zipalignFile = '/data/package_shell/release/%s/%s_%s_%s.apk' %(channel, apk, channel, version)
if (os.path.exists(zipalignFile)):
os.remove(zipalignFile)
os.system('/root/android-sdk-linux/tools/zipalign -v 4 %s /data/package_shell/release/%s/%s_%s_%s.apk' %(destfile, channel, apk, channel, version)
os.remove('/data/package_shell/ZZZ.txt')
注
如果在运行zipalign命令时出现以下问题,则代表需要的某些so库不存在,需要咱们自己进行安装,如下:
1./lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
2.error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
需要安装如下库:
yum -y install glibc.i686
yum install libstdc++.i686
yum install zlib.i686
如果仍然报so库找不到的错误,请使用命令ldd ./zipalign查看哪些so库找不到,直接安装就好!