Android Apk 反编译和重打包说明 定制资源替换

35 篇文章 1 订阅

Android Apk 反编译和重打包说明

可参考网站: https://ibotpeaches.github.io/Apktool/install/  

测试环境: Ubuntu 18.04
按照上面网站的说明,下载了apktoolhe apktool.jar, 用法如下:

反编译


hulk@hulk-PC:~/byod/tools/repackage-tools$ apktool d -r demo-app-signed.apk -o test
I: Using Apktool 2.5.0 on demo-app-signed.apk
I: Copying raw resources...
I: Baksmaling classes.dex...
I: Copying assets and libs...
I: Copying unknown files...
I: Copying original files...

重打包


hulk@hulk-PC:~/byod/tools/repackage-tools$ apktool b test -o test.apk
I: Using Apktool 2.5.0
I: Checking whether sources has changed...
I: Smaling smali folder into classes.dex...
I: Checking whether resources has changed...
I: Copying raw resources...
I: Copying libs... (/lib)
I: Building apk file...
I: Copying unknown files/dir...
I: Built apk...
hulk@hulk-PC:~/byod/tools/repackage-tools$ 

问题说明

如果没有"-r"参数

配置到环境变量中, 可正常反编译,但是重打包事会提示找不到资源问题
hulk@hulk-PC:~/Downloads$ apktool b demo-app-signed
I: Using Apktool 2.5.0
I: Checking whether sources has changed...
I: Checking whether resources has changed...
I: Building resources...
W: /home/hulk/Downloads/demo-app-signed/AndroidManifest.xml:1: error: No resource identifier ....................................
W: 
brut.androlib.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [/tmp/brut_util_Jar_31932934467824414377604707760593060500.tmp, p, --forced-package-id, .............................signed/res, -M, /home/hulk/Downloads/demo-app-signed/AndroidManifest.xml]

找不到资源,丢东西了?

解决办法: 
修改反编译命令:  
apktool脚本命令:  apktool d -r demo-app-signed.apk -o test
原始java命令: java -jar apktool.jar -r d demo-app-signed.apk -o test   //反编译时增加 -r 参数,便是循环反编译完成;

原始 java -jar apktool.jar命令示例

反编译
hulk@hulk-PC:~/byod/tools/repackage-tools$ java -jar apktool.jar -r d demo-app-signed.apk -o test
I: Using Apktool 2.4.1 on demo-app-signed.apk
I: Copying raw resources...
I: Baksmaling classes.dex...
I: Copying assets and libs...
I: Copying unknown files...
I: Copying original files...

//重新打包
hulk@hulk-PC:~/byod/tools/repackage-tools$  java -jar apktool.jar b test
I: Using Apktool 2.4.1
I: Checking whether sources has changed...
I: Smaling smali folder into classes.dex...
I: Checking whether resources has changed...
I: Copying raw resources...
I: Copying libs... (/lib)
I: Building apk file...
I: Copying unknown files/dir...
I: Built apk...

资源定制

反编译之后, 肯定事需要修改资源文件

1. 替换定制项目的字符串和图片等等;

(1) 替换图片资源

cp -rf custom_res/drawable-xxhdpi-v4/. ./test/res/drawable-xxhdpi
cp -rf custom_res/res/drawable-xxhdpi-v4/. ./test/res/drawable-xhdpi
cp -rf custom_res/res/drawable-xxhdpi-v4/. ./test/res/drawable-hdpi
cp -rf custom_res/res/drawable-xxhdpi-v4/. .test/res/drawable-mdpi

还可以通过命令把原图片进行放缩之后在输出到新的apk:

convert custom_res/res/drawable-xxhdpi-v4/ic_launcher_home.png -resize 96x96 ./test/res/drawable-xhdpi-v4/ic_launcher_home.png
convert custom_res/res/drawable-xxhdpi-v4/ic_launcher_home.png -resize 72x72 ./test/res/drawable-hdpi-v4/ic_launcher_home.png
convert custom_res/res/drawable-xxhdpi-v4/ic_launcher_home.png -resize 48x48 ./test/res/drawable-mdpi-v4/ic_launcher_home.png

(2) 修改字符串

把string.txt文件里面的所有字符串,按照行替换到新包中

sed -i "s/${line_array[0]}/${line_array[1]}/g" ./test/res/values/strings.xml

# 替换/res/values/strings.xml中字符串
cat custom_res/string.txt | awk '{print $0}'| while read line
do
    echo $line
    OLD_IFS="$IFS"
    IFS=":"
    line_array=($line)
    IFS="$OLD_IFS"
    echo "line_array size:"${#line_array[*]}" line_array:"${line_array[*]}
    if [ ${#line_array[*]} = 2 ]
    then

   #实际替换strings.xml中的文字
    sed -i "s/${line_array[0]}/${line_array[1]}/g" ./"${sourcename%.*}"/res/values/strings.xml
    else
    continue
    fi
done

2. 修改smali文件
这个复杂度比较大, 也比较简单. 可以自行研究


# 替换manifest 添加自定义权限
if [ -f "custom_res/permissions.txt" ]
then

    #找到manifest的uses-permission数量
    permissioncount=$(cat ./test/AndroidManifest.xml  | grep '<uses-permission' -n | awk -F ':' '{print $1}' | head -n 1)
    permissioncount=$permissioncount"r"
    echo " permissioncount :"$permissioncount
    echo "replace manifest and permissionms"
    sed -i  "${permissioncount} custom_res/permissions.txt" ./test/AndroidManifest.xml
fi

# 替换versionCode和VersionName

#   $v  为 versionName, $c 为versionCode  
ls -al ./test
if [ $c != "" ]
then
    sed -i "s/versionCode.*/versionCode: '$c'/g" ./test/apktool.yml
fi

if [ $v != "" ]
then
    sed -i "s/versionName.*/versionName: $v/g" ./test/apktool.yml
fi
echo "replace versionName and versionCode"

# 重打包

$t为新包输出文件目录
if [ $targetPath = "" ]
then
echo "not give target path, need to rebuild package by apktool"
else
./apktool b ./test -o $targetPath
echo "apktool b target:"$targetPath
fi

附加说明

现成linux的apktool下载地址: https://download.csdn.net/download/zhanghao_Hulk/20500248,  最好下载最新版本https://ibotpeaches.github.io/Apktool/install/  有不同平台版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值