jenkins配置360加固腳本(python版)

背景

公司開發的應用,有至關一部分被破解,出現了各類所謂的純淨版、破解版、無廣告版,對於公司的商業化形成了不利影響,通過調研,決定採起360加固的方案。java

可是有個問題,公司外發的應用每每須要多個渠道,加固以後的apkwalle打的渠道號以及簽名會丟失,須要從新簽名,打渠道,比較耗時耗力,做爲一名愛搞事情的程序員,這種複雜重複的工做就應該交給機器去處理,相似這種工做,python每每是不二選擇。python

正文

經過360加固官網知道,加固工具分爲windows、mac、以及linux版本,並且加固分爲登陸360加固保帳號而後加固兩個步驟。我這裏安裝的是win版本,安裝以後的目錄結構以下圖:linux


這裏面有咱們執行加固命令的環境,經過網上各類水,終於找到了360加固的命令,這就爲咱們python自動化加固提供了可能(這步是關鍵,耗費了我至關一部分的時間),上面提到加固分爲登陸以及加固步驟,因此命令也分兩條:程序員

登陸:java -jar -Dfile.encoding=UTF-8 jiagu_jar_path -login 用戶名 密碼shell

加固: java -jar -Dfile.encoding=UTF-8 jiagu_jar_path -jiagu apk_path apk_out_pathwindows

通過一番各類調試修改,在本地經過cmd執行命令成功以後,再經過python執行也成功了,接下來部署到jenkins服務器,你覺得這樣就OK了麼?噩夢纔剛剛開始,在本地調通的python腳本部署到jenkins上始終運行不成功,也沒有任何日誌,一開始還覺得我本地安裝的是pyhon3.0,而jenkins是2.0的緣由,結果我本地安裝2.0以後,本地仍是正常的。就在一籌莫展的時候,釗哥提出是否是jenkins是linux平臺,而我上傳的360加固環境是基於windows的,結果替換以後果真一切正常了,反正總而言之是能夠了。bash

接下來說講怎麼使用,公司的外發項目設計到多渠道walle打包的都有一個build.py的腳本,咱們從main函數看起,以下:服務器

if __name__ == '__main__':

    options = parse_options()

    path = options.origin_apk_dir
    number = options.number
    channels = options.channels.split(',')
    spec = options.spec
    walle_channel_jar = options.walle_channel_jar

    print_format("APK CHANNELS")
    for root, dirs, files in os.walk(path):
        for file in files:
            filePath = os.path.join(root, file)
            if filePath.endswith(".apk"):
                print "[APK CHANNELS]Start to generate channel package based on: " + filePath
                start_generate(filePath, number, channels, spec, walle_channel_jar)

    print "[APK CHANNELS]Channel package generation is complete."
複製代碼

start_generate()函數是walle打包的入口函數,咱們只須要在這個以前把grale生成的apk加固從新簽名,而後將生成好的apk做爲參數walle打包就行了,以下:
app

if __name__ == '__main__':

    options = parse_options()

    keystore_path = options.keystore_path
    print "keystore_path: " + keystore_path
    ks_key_alias = options.ks_key_alias
    print "ks_key_alias: " + ks_key_alias
    ks_pass = options.ks_pass
    print "ks_pass: " + ks_pass
    key_pass = options.key_pass
    print "key_pass: " + key_pass

    path = options.origin_apk_dir
    number = options.number
    channels = options.channels.split(',')
    spec = options.spec
    walle_channel_jar = options.walle_channel_jar
    print "jiagu_path: " + jiagu_path

    print_format("APK CHANNELS")
    for root, dirs, files in os.walk(path):
        for file in files:
            filePath = os.path.join(root, file)
            if filePath.endswith(".apk"):
                print "[APK CHANNELS]Start to generate channel package based on: " + filePath
                index = filePath.find("debug")
                #debug ingnore
                if filePath.find("debug")>0:
                    start_generate(filePath, number, channels, spec, walle_channel_jar)
                else:
                    #jiagu
                    temp_result = jiagu.jiagu(jiagu_path,"15267451420","lisj10659",filePath)
                    print "temp_result" + temp_result
                    signed_apk_path = temp_result[0:temp_result.rindex('/')] + temp_result[temp_result.rindex('/'):temp_result.rindex('.')]+"_sign.apk"
                    print "signed_apk_path" + signed_apk_path
                    #sign
                    sign_apk(jiagu_path,keystore_path,ks_key_alias,ks_pass,key_pass,signed_apk_path,temp_result)
                    #generate channel apk
                    start_generate(signed_apk_path, number, channels, spec, walle_channel_jar)


    print "[APK CHANNELS]Channel package generation is complete."
複製代碼

注意這裏:
函數

keystore_path = options.keystore_path
    print "keystore_path: " + keystore_path
    ks_key_alias = options.ks_key_alias
    print "ks_key_alias: " + ks_key_alias
    ks_pass = options.ks_pass
    print "ks_pass: " + ks_pass
    key_pass = options.key_pass
    print "key_pass: " + key_pass
複製代碼

增長了四個用於簽名的參數,須要在jenkins上配置,以下圖:


配置內容以下:

-e ${WORKSPACE}/fasthdtv.jks -a fasthdtv -k 123456 -p 123456

分別對應gradle 簽名配置中的 storeFile、keyAlias、storePassword、keyPassword,而後在main函數調用的parse_options(),進行解析以下:


main函數中調用了jiagu.jagu()以下圖:


因此咱們須要在build.py的目錄下新建一個jiagu.py而且定義jiagu()函數,以下:

import os
import subprocess


def jiagu(jiagu_360_dir, username, password, apk_file):
    jiagu_360_jar = '{jiagu_360_dir}/jiagu.jar'.format(jiagu_360_dir=jiagu_360_dir)
    print "jiagu_360_jar"+jiagu_360_jar
    # login
    command_login = "java -jar -Dfile.encoding=UTF-8 {jiagu_360_jar} -login {username} {password}".format(
        jiagu_360_jar=jiagu_360_jar, username=username, password=password
    )
    print "command_login"+command_login
    login_result = subprocess.call(command_login, shell=True)

    # x86 com.dangbei.settingprovoder2.support
    command_config_x86 = 'java -jar -Dfile.encoding=UTF-8 {jiagu_360_jar} -config -x86'.format(
        jiagu_360_jar=jiagu_360_jar)
    subprocess.call(command_config_x86, shell=True)

    #dest dir
    jiagu_tmp = apk_file[0:apk_file.rindex('/')]


    subprocess.call("chmod -R 775 {jiagu_tmp}".format(jiagu_tmp=jiagu_tmp), shell=True)


    #jiagu
    command_jiagu = 'java -jar -Dfile.encoding=UTF-8 {jiagu_360_jar} -jiagu {apk_file} {jiagu_tmp}'.format(
        jiagu_360_jar=jiagu_360_jar, apk_file=apk_file, jiagu_tmp=jiagu_tmp)
    subprocess.call(command_jiagu, shell=True)

    print ("***************** command_jiagu"+command_jiagu)
    print ("***************** apk_file"+apk_file)
    result_file = jiagu_tmp+apk_file[apk_file.rindex('/'):apk_file.rindex('.')] + "_jiagu.apk"

    print ("***************** result_file"+result_file)

    for tmp in os.listdir(jiagu_tmp):
        if tmp.endswith(".apk"):
            full_path = jiagu_tmp + "/" + tmp
            print ("***************** full_path"+full_path)
            os.chmod(full_path, 775)
            os.rename(full_path, result_file)

    #subprocess.call("rm -R {jiagu_tmp}".format(jiagu_tmp=jiagu_tmp), shell=True)

    print ('[JIAGU]jiagu completed')
    return result_file




def zip_align(zip_aligned_apk, origin_apk):
    command_zipalign = 'zipalign -f -v 4 {origin_apk} {zip_aligned_apk}'.format(origin_apk=origin_apk,
                                                                                zip_aligned_apk=zip_aligned_apk)
    subprocess.call(command_zipalign, shell=True)

    os.remove(origin_apk)
    print ('[ZIP ALIGN]zipaligin completed')



def print_format(content):
    print ("#########################################################")
    print ("# -----> {content}".format(content=content))
    print ("#########################################################")

#jiagu("D:/AndroidStudioProjects/fasthdtv/app/build_script/channel/jiagu","15267451420","lisj10659","D:/AndroidStudioProjects/fasthdtv/app/build/outputs/apk/debug/app-debug.apk")
複製代碼

到此jenkins配置360加固腳本(python版)結束,附件爲完整的buIld.py以及jiagu.py,須要的自取,根據本身的項目狀況改動,手動比心!

相關文章
相關標籤/搜索