Python学习与Android自动化打包

开发过程中常常需要开发一些小工具实现一些繁琐的事情,类似之前使用Winform开发读取Excel 内数据导出到自定义文件。当然为了实现多平台使用,Python当然成为了首选了。
Python 是最近几年比较火的开发语言,应用场景也比较广泛。技多不压身嘛,我们就一起学习一下。
在这里插入图片描述
引用一张图(侵删)简单介绍一下Python的基础语法。Python作为最好用的开发语言,有过开发语言基础的学习起来还是比较简单的。

利用Python 自动打包Apk

  1. 使用Apktool 解包
  2. 将资源文件 拷贝 到 安卓Asset文件夹下
  3. 重新打包
  4. 重新签名

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实现自动化打包,部署,热跟新等过程,解放生产力。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值