python一键打包

# -*- coding: UTF-8 -*-
#android package


from Tkinter import *
import shutil,zipfile,os,_subprocess,time
#python f:/python/pack/package.py


exeFile = "Des_Enctypt_AndPIC.exe"
xmlFile = "AndroidManifest.xml"


#batFile
debatFile = "decompression.bat"
gebatFile = "generate.bat"
sibatFile = "certificate.bat"
cobatFile = "Com.bat"
zibatFile = "zipalign.bat"


targetFile = ["fonts","res","script"]


def getVersionName(versionCode):
versionCode = versionCode < 10 and versionCode or 0
versionName = "1.0.%d" % versionCode
return versionName


def mergeDir(srcDir,dstDir):
if not os.path.exists(dstDir): os.mkdir(dstDir)
for f in os.listdir(srcDir):
srcFile = os.path.join(srcDir,f)
dstFile = os.path.join(dstDir,f)
if os.path.isdir(srcFile):
mergeDir(srcFile,dstFile)
else:
open(dstFile,"wb").write(open(srcFile,"rb").read())


def speMerge(src,dst):
for k in targetFile:
srcDir = os.path.join(src,k)
dstDir = os.path.join(dst,k)
print "wait ... " + k + " ... encrypt"
mergeDir(srcDir,dstDir)


def speRemoveDir(src):
for k in targetFile:
srcDir = os.path.join(src,k)
shutil.rmtree(srcDir,True)


def callExe(exePath,exeFile):
handle = _subprocess.CreateProcess(os.path.join(exePath,exeFile),"",None,None,0,_subprocess.CREATE_NEW_CONSOLE,None,exePath,STARTUPINFO())
run = True
while run:
rc = _subprocess.WaitForSingleObject(handle[0], 1000)
if rc == _subprocess.WAIT_OBJECT_0: run = False


def modBat(batFile,fileName,apkPath):
goalFile = os.path.join(apkPath,batFile)
goalStr,goalLine = "",1
if batFile[0:2] == "de": 
goalStr = "apktool d %s\n" % fileName
goalLine = 4
elif batFile[0:2] == "ge": 
goalStr = "apktool b %s new.apk\n" % fileName[:-4]
goalLine = 5
modFile(goalFile,goalLine,goalStr)


def modFile(goalFile,goalLine,goalStr):
contents = []
line = 0
tempFile = open(goalFile).readlines()
for L in tempFile:
line += 1
if line == goalLine: L = goalStr
contents.append(L)
open(goalFile,"w").write("")
for L in contents:
open(goalFile,"a").write(L)


class STARTUPINFO:
    dwFlags = 0
    hStdInput = None
    hStdOutput = None
    hStdError = None
    wShowWindow = 0


top = Tk()
top.title("one key to package in android")


# 创建框架
frame1st = Frame(top)
frame1st.pack()
frame2st = Frame(top)
frame2st.pack()
frame3st = Frame(top)
frame3st.pack()
frame4st = Frame(top)
frame4st.pack()
frame5st = Frame(top)
frame5st.pack()


width = 60
# 1、显示结果 ——lab
Label(frame1st,text = "结果",fg = "red",font = 50).pack(side = LEFT)
strResult = "Show result here!\nPlease wait.It may cost 4 mins to 5 mins,\nwhen you click pack btn!"
labResult = Label(frame1st,text = strResult,width = width,height = 15,font = 50)
labResult.pack(side = LEFT)
# 2、资源文件的路径 ——输入框
Label(frame2st,text = "资源文件目录: ",fg = "red",font = 50).pack(side = LEFT)
entryDir = Entry(frame2st,width = width,font = 40,bd = 3)
entryDir.pack(side = LEFT)
dirPath = "E:/Client/trunk/samples/MyMainGame/Resources"
entryDir.insert(0,dirPath)
# 3、tools的路径 ——输入框
Label(frame3st,text = "tools目录: ",fg = "red",font = 50).pack(side = LEFT)
entryDirTools = Entry(frame3st,width = width,font = 40,bd = 3)
entryDirTools.pack(side = LEFT)
dirPathTools = "E:/Client/tools"
entryDirTools.insert(0,dirPathTools)
# 4、apk文件名 ——输入框
Label(frame4st,text = "apk文件名(母包): ",fg = "red",font = 50).pack(side = LEFT)
entryApkName = Entry(frame4st,width = width,font = 40,bd = 3)
entryApkName.pack(side = LEFT)
apkFileName = "111.apk"
entryApkName.insert(0,apkFileName)
# 5、版本号 ——输入框
Label(frame5st,text = "版本号(versionCode): ",fg = "red",font = 50).pack(side = LEFT)
entryVersionCode = Entry(frame5st,width = width,font = 40,bd = 3)
entryVersionCode.pack(side = LEFT)
versionCode = "1"
entryVersionCode.insert(0,versionCode)
# 6、pack按钮 ——btn
#dirFile
def packEvent():
srcPath = entryDir.get()
toolPath = entryDirTools.get()
apkFileName = entryApkName.get()
versionCode = entryVersionCode.get()


exePath = toolPath + "/DES_Enctypt/Exe"
apkPath = toolPath + "/AndroidPack/ApkTools"
signPath = toolPath + "/AndroidPack/Auto-sign"
zipPath = toolPath + "/AndroidPack/Zipalign"


dstPath = os.path.join(exePath,"sourFile")
pngPath = os.path.join(dstPath,"res")


apkTemp = "new.apk"
apkTemp2 = "new2.apk"


s = "脚本开始执行,共9步:"
print s.decode('utf-8').encode('gbk')


timeBegin = time.time()


#copy file to exe --1
print "1-- copy file to exe"
speMerge(srcPath,dstPath)


# 图片压缩 --1.1
print "1.1-- compress .png file"
os.chdir(exePath)
os.system(cobatFile)


#exe encrypt --2
print "2-- exe encrypt"
callExe(exePath,exeFile)
#merge outfile to srcfile --3
print "3-- merge outfile to srcfile"
fileTemp = os.path.join(exePath,"outFile")
speMerge(fileTemp,os.path.join(exePath,"sourFile"))
#删除中间文件
speRemoveDir(fileTemp)


#modefied bat --4
print "4-- modefied bat"
fileName = apkFileName
modBat(debatFile,fileName,apkPath)
modBat(gebatFile,fileName,apkPath)


#decompression mother package --5
print "5-- decompression mother package"
os.chdir(apkPath)
os.system(debatFile)


#move file to asserts --6
print "6-- move file to asserts"
fileTemp = os.path.join(exePath,"sourFile")
speMerge(fileTemp,os.path.join(apkPath,fileName[:-4]) + "/assets")
#删除中间文件
speRemoveDir(fileTemp)
#mod xmlfile
fileTemp = os.path.join(apkPath,apkFileName[:-4])
goalFile = os.path.join(fileTemp,xmlFile)
goalLine = 2
goalStr = '<manifest android:versionCode="%s" android:versionName="%s" android:installLocation="auto" package="com.shy.idlegame"\n' % (versionCode,getVersionName(versionCode))
modFile(goalFile,goalLine,goalStr)


#generate new package --7
print "7-- generate new package"
os.system(gebatFile)
#删除中间文件
shutil.rmtree(fileTemp,True)


#certificate --8
print "8-- certificate"
os.chdir(signPath)
apkFile = os.path.join(apkPath,apkTemp)
newApkFile = os.path.join(signPath,apkTemp)
open(newApkFile,"wb").write(open(apkFile,"rb").read())
#删除中间文件
os.remove(apkFile)
os.system(sibatFile)
#删除中间文件
os.remove(newApkFile)


#zipalign --9
print "9-- zipalign"
os.chdir(zipPath)
apkFile = os.path.join(signPath,apkTemp2)
newApkFile = os.path.join(zipPath,apkTemp2)
open(newApkFile,"wb").write(open(apkFile,"rb").read())
#删除中间文件
os.remove(apkFile)
os.system(zibatFile)
#删除中间文件
os.remove(newApkFile)


timeEnd = time.time()
cost = timeEnd - timeBegin
print "cost = ",cost


labResult["text"] = "恭喜,你成功啦,耗时" + str("%.2f"%(cost/60)) + "分!" + "\n————生成的新包————\n" + os.path.join(zipPath,"优化后发布包.apk")


btnPack = Button(top,command = packEvent,text = "打\t包",fg = "purple",font = 80)
btnPack.pack()


top.mainloop()


#解压zip文件
#copy zip to dstpath
# name = "proj.android_Original"
# srcZipFile = "E:/Client/trunk_new/samples/MyMainGame/" + name + ".zip"
# dstPath = "F:/Program Files"
# shutil.copy(srcZipFile,dstPath)
# fileName = os.path.basename(srcZipFile)
# dstFile = os.path.join(dstPath,fileName)
# z = zipfile.ZipFile(dstFile, "r")
# for k in z.namelist():
# if not k.endswith("/"):
# f = os.path.join(dstPath,k)
# direct = os.path.dirname(f)
# if not os.path.exists(direct): os.makedirs(direct)
# file(f, "wb").write(z.read(k))
# z.close()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 Unity 一打包 Android Export Project 并编译 il2cpp,可以分为以下几个步骤: 1. 配置 Android 环境:在 Unity 中打包 Android 项目,需要配置好 Android SDK、NDK 和 JDK 环境变量。 2. 配置 Unity 设置:在 Unity 中,需要配置好 Android 平台的 Build Settings,设置好所需的 Android 版本和架构,同时也需要设置好 il2cpp 编译选项。 3. 编写自动化脚本:将上述配置步骤封装成自动化脚本,可以使用 Python、Shell 或其他脚本语言来实现。 4. 执行自动化脚本:执行自动化脚本,自动完成 Android Export Project 和 il2cpp 编译。 下面是一个 Python 脚本的示例,可以实现 Unity 一打包 Android Export Project 并编译 il2cpp: ```python import os # 配置 Android 环境 ANDROID_HOME = "/path/to/android/sdk" ANDROID_NDK_HOME = "/path/to/android/ndk" JAVA_HOME = "/path/to/java" os.environ["ANDROID_HOME"] = ANDROID_HOME os.environ["ANDROID_NDK_HOME"] = ANDROID_NDK_HOME os.environ["JAVA_HOME"] = JAVA_HOME # 配置 Unity 设置 UNITY_PATH = "/path/to/unity/editor" UNITY_PROJECT_PATH = "/path/to/unity/project" UNITY_VERSION = "2019.4.0f1" UNITY_BUILD_TARGET = "android" UNITY_BUILD_ARCHITECTURE = "arm64" UNITY_IL2CPP = "1" UNITY_IL2CPP_CONFIG = "Release" # 执行 Unity 命令行打包 unity_cmd = f"{UNITY_PATH} -batchmode -quit " \ f"-projectPath {UNITY_PROJECT_PATH} " \ f"-logFile /dev/stdout " \ f"-executeMethod BuildPipeline.BuildAndroid " \ f"-buildTarget {UNITY_BUILD_TARGET} " \ f"-androidVersion {ANDROID_VERSION} " \ f"-androidArchitecture {UNITY_BUILD_ARCHITECTURE} " \ f"-il2cpp {UNITY_IL2CPP} " \ f"-il2cppConfig {UNITY_IL2CPP_CONFIG}" os.system(unity_cmd) # 编译 il2cpp il2cpp_cmd = f"{UNITY_PATH} -batchmode -quit " \ f"-projectPath {UNITY_PROJECT_PATH} " \ f"-logFile /dev/stdout " \ f"-executeMethod UnityEditor.Il2Cpp.BuildIl2Cpp " \ f"-buildOutputPath /path/to/output " \ f"-il2cppCompilerConfiguration {UNITY_IL2CPP_CONFIG} " \ f"-androidPlatformVersion {ANDROID_VERSION} " \ f"-androidTargetArch {UNITY_BUILD_ARCHITECTURE}" os.system(il2cpp_cmd) ``` 需要注意的是,上述脚本仅供参考,实际应用中需要根据具体情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值