自动化打包资源混淆集成python实践----资源混淆

前面自动化打包资源混淆集成python实践----打包一文讲述了四种打包方案,以及美团打包方案、apk注释添加渠道号方案的实现。这里讲集成资源混淆。

1、资源混淆带来的好处

   1)对资源文件起一定的保护作用,使其不能知其名不知意(如login.png ----a.png> )

   2)对压缩apk包大小,起一定的作用(混淆资源ID长度,对apk进行深度压缩)

2.方案种类

   1)美团资源混淆

        原理:干涉appt过程,改写appt 源码,在生成resources.arsc和*.ap_时把资源文件的名称进行替换

   2)微信资源混淆

        原理:对apk 中的resources.arsc进行内容修改,同时对apk进行深度压缩。

3.Python 集成微信资源混淆

    1)思路:gradle 打包 ---->微信资源打包 --->多渠道打包

    2)python实现

         (1)使用微信资源混淆方法

                  法1:定义gradle andResGuard  task, ./gradlew andresguard 

                  法2:直接用命令行执行  java -jar resourceproguard.jar input.apk -7zip /shwenzhang/tool/7za -zipalign /shwenzhang/sdk/tools/zipalign

                  ps:这里使用法2使用混淆

        (2)源码:

           

def func_andResGuard(apkPath,isChannels = False):
    #根据AndResGuard-cli-1.1.0.jar,config.xml 实际路径替换
    guardJarFile = workSpace + '/AndResGuard/AndResGuard-cli-1.1.0.jar'
    guardConfigFile = workSpace + '/AndResGuard/config.xml'

    outDir = workSpace + '/AndResGuard/build'
    cmd = 'java -jar ' + guardJarFile  + ' '+apkPath + ' -config ' + guardConfigFile + ' -out '+ outDir +' -zipalign ' + zipalignFile
    proc = subprocess.Popen(cmd,shell = True)
    if isChannels:
        func_listen_process(proc,AndResGuardProcessListener(apkPath,True))
    else:
        func_listen_process(proc,AndResGuardProcessListener(apkPath))


#andResGuard进程监听器        
class AndResGuardProcessListener(ProcessListener):
    curTime = 0
    isChannels = False
    apkPath = ''
    preApkSize = 0
    
    def __init__(self,apkPath,isChannels = False):
        self.isChannels = isChannels
        self.apkPath = apkPath
    def start(self):
        size =os.path.getsize(self.apkPath)
        self.preApkSize = size /float(1024)/1024
    
        self.curTime = time.time()
        print '
 ------>  AndResGuard start 
'    
    def doing(self):
        return
    def end(self):
        time.sleep(30)
        files = os.listdir(workSpace + '/AndResGuard/build')
        for f in files :
            if 'signed_7zip_aligned' in f:
                size = os.path.getsize(workSpace + '/AndResGuard/build/' + f)
                print '
 ------>  AndResGuard before apk size : ' + str(float('%0.2f'%(self.preApkSize))) + 'M
'
                size = size /float(1024)/1024
                print '
 ------>  AndResGuard after apk size : ' + str(float('%0.2f'%size)) + 'M
'
                os.chmod( self.apkPath, stat.S_IWRITE )
                os.remove(self.apkPath)
                shutil.copyfile(workSpace + '/AndResGuard/build/' + f,self.apkPath)
                break
        else :
            print ''
        self.curTime = time.time() - self.curTime - 10
        print '
 ------>  AndResGuard end , cost time '+ str(int(self.curTime)) +'s

'
        
        if self.isChannels:
            func_channelsReleasePack(workSpace)

4、混淆压缩效果:

ps:使用友盟分享功能时,需要在config.xml添加资源白名单(友盟根据资源名获取资源ID,而资源名被混淆了,导致资源找不到),填写正确的签名信息(否则微信分享调用 不起来)。

github:github:AndroidPackCi

 

参考资料:

美团Android资源混淆保护实践

安装包立减1M--微信Android资源混淆打包工具

https://github.com/shwenzhang/AndResGuard

Android应用程序资源的编译和打包过程分析

原文地址:https://www.cnblogs.com/sihaixuan/p/5326289.html