Android 发版的小工具

Android加固包签名

我们知道自己的apk在上传市场的时候, 为了更好的包含我们的代码需要加固服务, 加固后的apk是不能直接安装的, 需要我们手动签名.

关于Android签名的知识就不在赘述了,网上有很多相关内容. Android应用数字签名详解

重点我们需要知道:

  1. android的签名不是 google自定义的, 它是一个通用校验算法, 通用与java 应用程序发版中. Android的签名只是在该算法基础上提供了自己的工具;
  2. 我们可以使用android studio 方便的完成对apk的签名,同事也可以使用其他签名工具对apk进行签名.

那么如何多已加固的包进行签名呢? 我们可以使用jarsigner 工具, 这个事jdk自带的签名工具, 将keystore文件(或者jks文件) 作为输入的签名文件来对apk进行签名.
另一个是工具signapk工具进行签名, signapk是Android中提供的工具,其签名输入文件为xxx.pk8xxx.x509.pem. signapk签名apk

keystore 签名文件中是可以提取signapk工具需要的签名文件的 详见keystore文件转换格式为pk8+x509.pem

介绍了上述关于android签名的内容, 今天的重点是放出一个通过jarsigner 签名加固宝的python脚本.

#coding=utf-8

import os, sys

# 你的jks文件名称
jksname = "jskfilename.jks"

# 注意yourpass 需要配置为你自己的密码
signerformat = "jarsigner -verbose -sigalg SHA1withRSA -storepass yourpass -digestalg SHA1 -keystore '{jksfile}' -signedjar '{signedpath}' '{unsignedpath}' wujikeji"

zipalignformat = "zipalign -v -f 4 '{signedapk}' '{alignedapk}'"

if __name__ == "__main__":
    cur_path =  sys.path[0]
    for parent, dirnames, filenames in os.walk(cur_path):
        if parent == cur_path:
            for filename in filenames:
                print os.path.join(parent, filename)
                # 过滤非apk文件以及已签名apk
                if filename.endswith(".apk") and "signed" not in filename:

                    # 提取文件名标示
                    apktag = filename.replace("app","").replace("-","").\
                        replace(".","").replace("release","").\
                        replace("apk","").replace("_","").replace("encrypted","").replace("legu","")

                    jksfile = os.path.join(cur_path, jksname)

                    signedpath = os.path.join(cur_path, apktag+"_signed.apk")

                    # 签名apk
                    os.popen(signerformat.format(jksfile=jksfile, signedpath=signedpath, unsignedpath=os.path.join(parent,filename)))

                    alignedapk = os.path.join(cur_path, apktag+"_signed_zipaligned.apk")
                    # 对其apk
                    os.popen(zipalignformat.format(signedapk = signedpath, alignedapk = alignedapk))

                    # 删除中间文件
                    os.remove(signedpath)
                    pass

该脚本针对加固后的包进行批量签名, 将加固后apk包和签名文件(jks文件)放入该脚本文件相同目录下, 运行脚本即可得到签名并对其的apk包. 前提需要配置自己的密码.

渠道包测试

渠道生成之后按理说应该可以直接发不了,但对于一般的程序猿都会多多少少有点强迫症, 要每个渠道都安装运行一下, 不需要多么详细的测试, 只是看下每个应用能不能运行起来.

国内十多个市场, 每个市场对应一个渠道包, 仅这些渠道包的简单测试就话费了好长时间. 并且每次有不需要多么仔细的测试. 于是想到直接用adb 控制手机自动完成测试的方法.

使用python作为控制脚本, 直接上代码:


import os, sys, time

adb_start_cmd ="adb shell am start -n pkgname/launchActivity"

adb_install_format = "adb install '{apk_path}'"

adb_uninstall = "adb uninstall yourpackagename"

if __name__ == "__main__":
    cur_path =  sys.path[0]
    for parent, dirnames, filenames in os.walk(cur_path):
        if parent == cur_path:
            all_apk_num =  len(filenames) -2

            cur_count = 0;
            for filename in filenames:

                # 过滤非apk文件以及已签名apk
                if filename.endswith(".apk") :
                    cur_count +=1
                    print filename
                    # 提取文件名标示
                    apktag = filename.replace("app","").replace("-","").\
                        replace(".","").replace("release","").\
                        replace("apk","").replace("_","").replace("encrypted","").replace("legu","")

                    os.popen(adb_uninstall)
                    os.popen(adb_install_format.format(apk_path=os.path.join(cur_path, filename)))


                    print apktag + "\t" + str(cur_count)+"/"+str(all_apk_num)


                    print "monkey go:"
                    os.popen(adb_start_cmd)
                    # 通过adb shell input 命令,先将首次启动页跳过
                    time.sleep(5)
                    os.popen("adb shell input swipe 800 200 50 200")
                    time.sleep(1)
                    os.popen("adb shell input tap 300 300")
                    time.sleep(1)
                    os.popen("adb shell input keyevent 4")
                    os.popen("adb shell input keyevent 4")

                    # 真正放候啦
                    # os.popen("adb shell monkey -p com.rong.jieqian.daikuanba -v 20 --throttle 1000 --pct-touch 50% --pct-trackball 25% --pct-motion  25%")
                    os.popen("adb shell monkey -p com.rong.jieqian.daikuanba -v 2000")
                    raw_input("input anything to continue: ")

代码主要用到三方面知识:

  1. adb 控制应用程序, 实现自动 安装, 卸载和启动;
  2. android 系统中的input 命令工具; 能够实现一些事件的输入, 在上面代码中实现了一个input swipe用于模拟滑动事件 和一个input tap 用于模拟点击事件.
  3. android系统中 monkey 命令工具; 能够随机生成一些列的输入事件, 就像猴子乱按一样.

参考:

adb shell input 命令

Android自动化测试手段之Monkey

android压力测试命令monkey详解

转载于:https://www.cnblogs.com/fog2012/p/6906002.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值