开发过程中常常需要开发一些小工具实现一些繁琐的事情,类似之前使用Winform开发读取Excel 内数据导出到自定义文件。当然为了实现多平台使用,Python当然成为了首选了。
Python 是最近几年比较火的开发语言,应用场景也比较广泛。技多不压身嘛,我们就一起学习一下。
引用一张图(侵删)简单介绍一下Python的基础语法。Python作为最好用的开发语言,有过开发语言基础的学习起来还是比较简单的。
利用Python 自动打包Apk
- 使用Apktool 解包
- 将资源文件 拷贝 到 安卓Asset文件夹下
- 重新打包
- 重新签名
Unpack.py 代码
# coding=utf-8
import os
setting = {'apkPath': "\\Apks"}
def GetFileList(dir):
dir = str(dir)
if dir == "":
return []
dir = dir.replace("/", "\\")
files = os.listdir(dir)
results = [x for x in files if os.path.isfile(dir + x)]
return results
def ExcuseCommand(arg):
if os.system(arg) == 0:
print("执行成功 ====" + arg)
else:
print("执行失败 ====" + arg)
def Main():
workPath = os.getcwd()
apkPath = workPath + setting['apkPath']
fileList = os.listdir(apkPath)
for file in fileList:
extern = os.path.splitext(file)
if extern[1] == ".apk":
apkFullName = apkPath + "\\" + file
outFullName = apkPath + "\\" + extern[0]
if not os.path.exists(outFullName):
os.makedirs(outFullName)
commands = "apktool d -r -s -f %s -o %s" % (apkFullName,
outFullName)
os.getcwd()
ExcuseCommand(commands)
Main()
MoveAssets.py 代码
# coding=utf-8
import os
import shutil
setting = {"apkPath": "\\Apks", "streamPath": "StreamPath"}
def GetFileList(dir):
dir = str(dir)
if dir == "":
return []
dir = dir.replace("/", "\\")
files = os.listdir(dir)
results = [x for x in files if os.path.isfile(dir + x)]
return results
def ExcuseCommand(arg):
if os.system(arg) == 0:
print("执行成功 ====" + arg)
else:
print("执行失败 ====" + arg)
def copytree(src, dst, symlinks=False, ignore=None):
print("src " + src)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
if not os.path.exists(d):
os.makedirs(d)
copytree(s, d, symlinks, ignore)
elif os.path.isfile(s) and os.path.exists(s):
if not os.path.exists(dst):
os.makedirs(d)
shutil.copy2(s, d)
def Main():
curPath = os.getcwd()
fullPath = curPath + "\\" + setting["streamPath"]
apkPath = curPath + "\\" + setting["apkPath"]
os.chdir(apkPath)
files = os.listdir(apkPath)
for file in files:
if os.path.isdir(file):
outPath = apkPath + "\\" + file + "\\assets\\"
if not os.path.exists(outPath):
os.makedirs(outPath)
copytree(fullPath, outPath)
pass
os.chdir(curPath)
Main()
Repack.py 代码
# coding=utf-8
import os
setting = {
"apkPath": "\\Apks",
"streamPath": "StreamPath",
"rebuildApks": "Rebuild"
}
def ExcuseCommand(arg):
if os.system(arg) == 0:
print(u"执行成功 ====" + arg)
else:
print(u"执行失败 ====" + arg)
def Main():
workPath = os.getcwd()
apkPath = workPath + setting['apkPath']
fileList = os.listdir(apkPath)
outFullName = workPath + "\\" + setting["rebuildApks"]
if not os.path.exists(outFullName):
os.makedirs(outFullName)
for folder in fileList:
if not os.path.isdir(folder):
apkComipleFolder = apkPath + "\\" + folder
apkFullName = outFullName + "\\" + folder + ".apk"
commond = "apktool b -f %s -o %s " % (apkComipleFolder,
apkFullName)
ExcuseCommand(commond)
pass
Main()
Resign.py 代码
import os
setting = {
"apkPath": "\\Apks",
"streamPath": "\\StreamPath",
"rebuildApks": "\\Rebuild"
}
def ExcuseCommand(arg):
if os.system(arg) == 0:
print(u"执行成功 ====" + arg)
else:
print(u"执行失败 ====" + arg)
def Main():
workPath = os.getcwd()
apkPath = workPath + setting['rebuildApks']
fileList = os.listdir(apkPath)
if not os.path.exists(apkPath):
os.makedirs(apkPath)
for folder in fileList:
extern = os.path.splitext(file)
if extern[1] == ".apk":
apkFullName = apkPath + "\\" + file
apkOutName = apkPath + "\\Now_" + file
command = "Java -jar signapk.jar platform.x509.pem platform.pk8 %s %s" % (
apkFullName, apkOutName)
ExcuseCommand(command)
Main()
创建bat文件依次执行Unpack.py,MoveAssets.py,Repack.py ,Resign.py ,便可以实现android包的重新打包过程。
当然Unity打包过程可以使用Jenkins实现自动化打包,部署,热跟新等过程,解放生产力。