利用python为你集成三方apk里的lib时自动生成Android.bp

github: GitHub - fredhurui/GenerateAndroidMakefile: Generate Android.bp for the shared library in the apk, which is used to integrated apk to Android ROM

在某些时候需要将三方apk集成为android ROM内置应用,如果三方应用内部有大量的shared library文件,则需求大量时间为其编写Android.bp或者Android.mk。

为了提高工作效率,我特意用python实现了一个简易版本其功能如下:

1.将APK文件解压,比如APK文件名为Test.APK

2.在Test/lib目录下创建Android.bp文件

3.读取Test/lib目录的子目录列表

4.针对armeabi-v7a和arm64-v8a目录,分别读取其shared library列表并逐个生成对应的prebuild脚本,然后写入Android.bp(当前版本只处理了armeabi-v7a)


#file is the apk name with suffix, it should like my.apk
def unzipfileAndGenerateMakefile(file):
    uzfile = zipfile.ZipFile(path + file)
    apk_name = file.split('.')[0]
    print(apk_name)
    print "unzip apk file to " + apk_name
    unzipedFilePath = path + apk_name + "/"
    #apkFolder = Path(unzipedFilePath)
    if os.path.exists(unzipedFilePath):
        print unzipedFilePath + " folder is exist"
    else:
        print "do unzip"
        uzfile.extractall(unzipedFilePath)
    generateSharedLibraryPrebuildMakefile(unzipedFilePath + "lib/")
    
def generateSharedLibraryPrebuildMakefile(file):
    #create xxx/lib/Android.bp
    #makefilePath = Path(file + 'Android.bp')
    if os.path.exists(file + 'Android.bp'):
        print file + 'Android.bp' + " is exist, remove it first"
        os.remove(file + 'Android.bp')
        
    out = open(file + 'Android.bp', 'a')
    lib_dirs = os.listdir(file)
    #lib_dirs = glob.glob(file)
    #Only handle arm64-v8a and armeabi-v7a
    for dirname in lib_dirs:
        print "lib sub dir : " + dirname
        if cmp("armeabi-v7a", dirname) == 0:
            print "Handle armeabi-v7a libs"
            print "list " + file + dirname + "/*.so"
            #libs = os.listdir(file + dirname + "/*.so")
            libs = fnmatch.filter(os.listdir(file + dirname), "*.so")
            for shareLibName in libs:
                print shareLibName
                content = generatearmeabiv7aMakefile(shareLibName)
                out.write(content)
                out.write("\n")
        if cmp("arm64-v8a", dirname) == 0 and needHanldeArm64:
            print "Handle arm64-v8a libs"
            #libs = glob.glob(file + dirname + "/*.so")
            libs = fnmatch.filter(os.listdir(file + dirname), "*.so")
            for shareLibName in libs:
                print shareLibName
                ins = open(file + 'Android.bp', 'r')
                file_content = ins.read()
                print "old content:" + file_content
                findResult = re.findall(shareLibName, content)
                count = len(findResult)
                print findResult
                print "found count:%d" %count
                if count == 1:
                    #should merge conent
                    print "find it and do merge"
                    print "old content:" + file_content
                else:
                    content = generatearm64v8aMakefile(shareLibName)
                    out.write(content)
                    out.write("\n")
    #out.write()
    out.close()

def generatearmeabiv7aMakefile(shareLibName):
    #lib_name = shareLibName.split('.')[0]
    #lib_name = os.path.basename(shareLibName)
    #suffixIndex = shareLibName.find(".so")
    #print  suffixIndex
    lib_name = shareLibName[0 : -3]
    print  lib_name
    AndroidBpContent = """
cc_prebuilt_library_shared {{
    name: \"{}\",
    target: {{
        android_arm: {{
            srcs: [\"armeabi-v7a/{}.so\"],
        }},
    }},
    strip: {{
        none: true,
    }},
}}"""
    result = AndroidBpContent.format(lib_name, lib_name)
    #print("AndroidBpContent:\n" + result)
    return result

def generatearm64v8aMakefile(shareLibName):
    #lib_name = shareLibName.split('.')[0]
    #lib_name = os.path.basename(shareLibName)
    #suffixIndex = shareLibName.find(".so")
    #print  suffixIndex
    lib_name = shareLibName[0 : -3]
    print  lib_name
    #first check if already generated makefile for armeabi-v7a for current lib
    
    AndroidBpContent = """
cc_prebuilt_library_shared {{
    name: \"{}\",
    target: {{
        android_arm64: {{
            srcs: [\"arm64-v8a/{}.so\"],
        }},
    }},
    strip: {{
        none: true,
    }},
}}"""
    result = AndroidBpContent.format(lib_name, lib_name)
    #print("AndroidBpContent:\n" + result)
    return result

if __name__ == '__main__':
    #print ("argv len: %d" %len(sys.argv))
    if len(sys.argv) == 2:
        unzipfileAndGenerateMakefile(sys.argv[1])
    else:
        print "Bad input parameters"
        print 'Usage: python %s xxx.apk' %sys.argv[0]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值