自制工具:利用python批量获取apk中的库文件。


客制化软件时,经常遇到需要将客户提供的apk预置到系统内,但是预置apk需要将apk中的库文件预先提取出来放到system/lib目录中,因此提取apk的库文件成为了一种需求。
常见的方法是手动用rar打开apk,导出库,然后保存,但是遇到大量apk需要预置的时候,会有不小的工作量,因此按照手动导出的逻辑,简单编写一个工具,代码如下:

'''
TOOLS: tools for get apks lib.
AUTOR: whonee
DATA : 20160109
'''
import os
import sys
import shutil

'''
    MODIFY IT FOR YOU PATH
'''
RAR_CMD = 'D://progra~1/WinRAR/winrar.exe'
PATH = 'D://py_tools/get_apk_lib/apk'
LIBPATH = 'D://py_tools/get_apk_lib/lib'

'''
    MAIN FUNCTION
'''
def rename(dirname = PATH):    
    listFile = os.listdir(dirname)    
    print(listFile)
    for file in listFile:
        filename = os.path.splitext(file)
        if(filename[1] == '.apk'):
            name = filename[0] + '.zip'
            print(name)
            os.rename(os.path.join(dirname, file),os.path.join(dirname,name))
    
    
def unzip(dirname = PATH,cmd = RAR_CMD):
    listFile = os.listdir(dirname)
    CMD = cmd + ' x {} {}'
    os.chdir(dirname)
    for file in listFile:
        filename = os.path.splitext(file)
        if(filename[1] == '.zip'):
            rarCmd = CMD.format(file,(filename[0]+os.sep))
            print(rarCmd)
            os.system(rarCmd)

def restname(dirname = PATH):    
    listFile = os.listdir(dirname)
    for file in listFile:
        filename = os.path.splitext(file)
        if(filename[1] == '.zip'):
            name = filename[0] + '.apk'
            print(name)
            os.rename(os.path.join(dirname, file),os.path.join(dirname,name))      
    
def findlib(dirname = PATH):
    listFile = os.listdir(dirname)   
    libdir = dirname + os.sep + 'alllib'
    if not os.path.exists(libdir):
        os.mkdir(libdir)
    for file in listFile:  
        # Bill Z 指出错误修改,可能处理到了alllib文件夹。
        if ( file == "alllib" ):                                                           
           continue              
        fulldirfile=os.path.join(os.path.abspath(dirname),file)
        if os.path.isdir(fulldirfile):
            #print(file + ' is path')
            os.chdir(fulldirfile)
            if os.path.exists('lib'):
                print(file + ' have lib')
                directdir = '../alllib/{}_lib'.format(file)
                shutil.move('lib',directdir)
            os.chdir('..')
            try:
                shutil.rmtree(fulldirfile)
            except:
                pass
            
def sort(dirname = PATH):
    libdir = dirname + os.sep + 'alllib'
    os.chdir(libdir)
    libfolderlist = os.listdir(libdir)
    for folder in libfolderlist:
        fulldirfile = os.path.join(libdir,folder)
        print("APK NAME: ",fulldirfile)
        os.chdir(fulldirfile)
        listlib = os.listdir(fulldirfile)
        for libfolder in listlib:
            print('HAVE LIB FOLDER: ',libfolder)
            if not (libfolder == 'x86' and libfolder == 'x64'):
                rootlib = os.path.join(fulldirfile,libfolder)
                os.chdir(rootlib)
                alllib = os.listdir(rootlib) 
                for lib in alllib:
                    srclibname = os.path.join(rootlib,lib)
                    #print(srclibname)
                    tarlibname = os.path.join(LIBPATH,lib)
                    print('USERFUL LIB: ',srclibname)
                    #print(tarlibname)
                    shutil.copy(srclibname,tarlibname)
    print("all lib will be copy to lib folder ,\
    if you want to find the base lib ,please into 'apk/alllib'")
              
def main():
    if not os.path.exists(RAR_CMD):
        print("winrar.exe is not exists, please input rar path likes: 'c://Progra~1/WinRAR/winrar' \n")        
        cmd = input('rar path: ')
        if not os.path.exists(cmd):
            print('input error rar path exit !')
            exit()
    path = PATH
    rename(path)
    unzip(path)
    findlib(path)
    restname(path)
    sort(path)    
    anykey = input("Sucessful ! please input Enter key to exit!")
    if anykey:    
        print('quit')
        exit()
        
    
if __name__ == '__main__':
    main()

在对应的文件夹放上apk,执行此工具之后就可以到指定的文件夹去寻找已经导出OK的库了。

《自制工具:利用python批量获取apk中的库文件。》有5个想法

      1. 57 def findlib ( dirname = PATH ):
        58 listFile = os.listdir(dirname)
        59 libdir = dirname + os.sep + ‘alllib’
        60
        61 if not os.path.exists(libdir):
        62 os.mkdir(libdir)
        63 else:
        64 shutil.rmtree(libdir)
        65
        66 for file in listFile:
        67 if ( file == “alllib” ):
        68 continue
        69

        1. @Bill Z:
          哦,看到了,果然是的,处理的时候,可能会处理到存放库文件的文件夹。
          这个工具写的时间有点长了,居然有点忘记了。
          你这个修改不错,谢谢咯!

发表评论

电子邮件地址不会被公开。 必填项已用*标注