安卓渠道打包自动部署脚本实现

APKTOOL 打包工具

安装java环境

查看最新版本

yum -y list java*

安装最新java版本(1.8.00)及其依赖(*)

yum -y install java-1.8.0-openjdk*

下载安装最新 apktool

https://ibotpeaches.github.io/Apktool/

wget https://bitbucket.org/iBotPeaches/apktool/downloads/apktool_2.3.4.jar
wget https://raw.githubusercontent.com/iBotPeaches/Apktool/master/scripts/linux/apktool
mv apktool_2.3.4.jar apktool.jar
ln -s /data/apktool/apktool /usr/local/bin/apktool
ln -s /data/apktool/apktool.jar /usr/local/bin/apktool.jar
chmod +x apktool
chmod +x apktool.jar 

使用apktool 解压再打包

apktool d -f -o /path-to-output-tmp-file/
cd /path-to-output-tmp-file/
sed -i 's/<meta-data android:name="ChannelInfo" android:value=".*"/>/<meta-data android:name="ChannelInfo" android:value="111111"/>/' /path-to-output-tmp-file/AndroidManifest.xml
apktool b /path-to-output-tmp-file/ -o /path/out.apk

给APK签名

jarsigner -storepass 123123 -keystore /data/html/user.keystore /out/001.apk dapianzi

远程文件拷贝 scp + authorized key

ssh-keygen -t rsa
ssh-keygen -t dsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

下载服务器mimeType支持(nginx)

vim /etc/nginx/mime.types

application/vnd.android.package-archive     apk; 
application/iphone                          pxl ipa;

完整shell脚本示例

只提供大概思路,不保证可以正确运行:)。GOOD LUCK

#!/bin/bash

# key config
key=123123
keyfile=/data/html/user.keystore
alias=dapianzi

# remote download server
dist_host="192.168.1.27"
dist_user="root"
dist_dir=/data/

# dir config
out_tmp=/out/tmp
out_dir=/out/apk

from=$1
to=$2

# cp current apk
# if new package
if [ -n "$3" ] ;then
    source=/data/html/source.apk
    rm -rf $out_tmp/*
    #  re-unpack 
    apktool d -f -o $out_tmp $source
fi

cd $out_tmp
echo "" > apk.lock
# loop
while [ $from -le $to ]  
do
    # change channle id
    sed -i 's/<meta-data android:name="ChannelInfo" android:value=".*"/>/<meta-data android:name="ChannelInfo" android:value="'$from'"/>/' $out_tmp/AndroidManifest.xml
    # build new package
    tmp_apk=$out_dir/dist_$from.apk
    apktool b -f $out_tmp -o $tmp_apk
    # sign new pak
    jarsigner -storepass $key -keystore $keyfile $tmp_apk $alias
    # copy to remote file server.
    scp $tmp_apk $dist_user@$dist_host:$dist_dir
    #cp $tmp_apk $dist_dir
    from=$((from+1))
done

# clear lock
rm apk.lock

远程驱动示例

<?php
$from = $_GET['from'];
$to = $_GET['to'];
$renew = $_GET['renew'];
function gf_ajax_error($msg, $code=-1) {
    gf_ajax_return([], $code, $msg);
}

function gf_ajax_success($data, $extra=[]) {
    gf_ajax_return($data, 0, '', $extra);
}

function gf_ajax_return($data, $code, $msg, $extra=[]) {
    header('Content-Type:application/json; charset=utf-8');
    return exit(json_encode(array_merge(array(
        'code' => $code,
        'msg' => $msg,
        'data' => $data,
    ), $extra), JSON_UNESCAPED_UNICODE));
}


function running_status() {
    if (file_exists('/data/apktool/lock')) {
        $cmd = 'ps axu | grep "apk_channel" | grep -v "grep"';
        if ('' == exec($cmd)) {
            return 'interrupt';
        } else {
            return 'pending';
        }
    }else {
        return 'finish';
    }
}

if ($_GET['query'] == 'res') {
    gf_ajax_success(running_status());
}

if (running_status() == 'pending') {
    gf_ajax_error('Already running');
}

if (empty($from) || empty($to)) {
    gf_ajax_error('Invalid Params.');
}

$exec = '/data/apktool/apk_channel.sh '.$from.' '.$to.' '.$renew.' > /data/apktool/log.log 2>&1 &';
exec($exec, $ret, $code);
gf_ajax_success($code);

原文地址:https://www.cnblogs.com/dapianzi/p/9675473.html