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 参数,便是循环反编译完成;
反编译
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/. https://blog.csdn.net/zhanghao_Hulk/article/details/test/res/drawable-xxhdpi
cp -rf custom_res/res/drawable-xxhdpi-v4/. https://blog.csdn.net/zhanghao_Hulk/article/details/test/res/drawable-xhdpi
cp -rf custom_res/res/drawable-xxhdpi-v4/. https://blog.csdn.net/zhanghao_Hulk/article/details/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 https://blog.csdn.net/zhanghao_Hulk/article/details/test/res/drawable-xhdpi-v4/ic_launcher_home.png
convert custom_res/res/drawable-xxhdpi-v4/ic_launcher_home.png -resize 72x72 https://blog.csdn.net/zhanghao_Hulk/article/details/test/res/drawable-hdpi-v4/ic_launcher_home.png
convert custom_res/res/drawable-xxhdpi-v4/ic_launcher_home.png -resize 48x48 https://blog.csdn.net/zhanghao_Hulk/article/details/test/res/drawable-mdpi-v4/ic_launcher_home.png
(2) 修改字符串
把string.txt文件里面的所有字符串,按照行替换到新包中
sed -i "s/${line_array[0]}/${line_array[1]}/g" https://blog.csdn.net/zhanghao_Hulk/article/details/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" https://blog.csdn.net/zhanghao_Hulk/article/details/"${sourcename%.*}"/res/values/strings.xml
else
continue
fi
done
2. 修改smali文件
这个复杂度比较大, 也比较简单. 可以自行研究
# 替换manifest 添加自定义权限
if [ -f "custom_res/permissions.txt" ]
then
#找到manifest的uses-permission数量
permissioncount=$(cat https://blog.csdn.net/zhanghao_Hulk/article/details/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" https://blog.csdn.net/zhanghao_Hulk/article/details/test/AndroidManifest.xml
fi
# 替换versionCode和VersionName
# $v 为 versionName, $c 为versionCode
ls -al https://blog.csdn.net/zhanghao_Hulk/article/details/test
if [ $c != "" ]
then
sed -i "s/versionCode.*/versionCode: '$c'/g" https://blog.csdn.net/zhanghao_Hulk/article/details/test/apktool.yml
fi
if [ $v != "" ]
then
sed -i "s/versionName.*/versionName: $v/g" https://blog.csdn.net/zhanghao_Hulk/article/details/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
https://blog.csdn.net/zhanghao_Hulk/article/details/apktool b https://blog.csdn.net/zhanghao_Hulk/article/details/test -o $targetPath
echo "apktool b target:"$targetPath
fi